summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--glib/Makefile.am4
-rw-r--r--glib/glib.h1
-rw-r--r--glib/gslice.c20
-rw-r--r--glib/gutils.c591
-rw-r--r--glib/gutils.h38
5 files changed, 30 insertions, 624 deletions
diff --git a/glib/Makefile.am b/glib/Makefile.am
index f18a54202..8c0c24d15 100644
--- a/glib/Makefile.am
+++ b/glib/Makefile.am
@@ -139,6 +139,7 @@ libglib_2_0_la_SOURCES = \
gdate.c \
gdatetime.c \
gdir.c \
+ genviron.c \
gerror.c \
gfileutils.c \
ghash.c \
@@ -260,8 +261,9 @@ glibsubinclude_HEADERS = \
gconvert.h \
gdataset.h \
gdate.h \
- gdatetime.h \
+ gdatetime.h \
gdir.h \
+ genviron.h \
gerror.h \
gfileutils.h \
ghash.h \
diff --git a/glib/glib.h b/glib/glib.h
index 886fd8098..546f2d1b8 100644
--- a/glib/glib.h
+++ b/glib/glib.h
@@ -43,6 +43,7 @@
#include <glib/gdate.h>
#include <glib/gdatetime.h>
#include <glib/gdir.h>
+#include <glib/genviron.h>
#include <glib/gerror.h>
#include <glib/gfileutils.h>
#include <glib/ghash.h>
diff --git a/glib/gslice.c b/glib/gslice.c
index 2cd44f5d8..f89a3cdb4 100644
--- a/glib/gslice.c
+++ b/glib/gslice.c
@@ -278,12 +278,30 @@ g_slice_get_config_state (GSliceConfig ckey,
}
}
+static const gchar *
+getenv_nomalloc (const gchar *variable,
+ gchar buffer[1024])
+{
+ const gchar *retval = getenv (variable);
+ if (retval && retval[0])
+ {
+ gint l = strlen (retval);
+ if (l < 1024)
+ {
+ strncpy (buffer, retval, l);
+ buffer[l] = 0;
+ return buffer;
+ }
+ }
+ return NULL;
+}
+
static void
slice_config_init (SliceConfig *config)
{
/* don't use g_malloc/g_message here */
gchar buffer[1024];
- const gchar *val = _g_getenv_nomalloc ("G_SLICE", buffer);
+ const gchar *val = getenv_nomalloc ("G_SLICE", buffer);
const GDebugKey keys[] = {
{ "always-malloc", 1 << 0 },
{ "debug-blocks", 1 << 1 },
diff --git a/glib/gutils.c b/glib/gutils.c
index c6ac0b484..a3bcde5c7 100644
--- a/glib/gutils.c
+++ b/glib/gutils.c
@@ -100,6 +100,7 @@
#include "gutils.h"
#include "glib-init.h"
+#include "genviron.h"
#include "gfileutils.h"
#include "ghash.h"
#include "gslist.h"
@@ -1084,596 +1085,6 @@ g_get_current_dir (void)
#endif /* !Win32 */
}
-/**
- * g_getenv:
- * @variable: the environment variable to get, in the GLib file name encoding.
- *
- * Returns the value of an environment variable. The name and value
- * are in the GLib file name encoding. On UNIX, this means the actual
- * bytes which might or might not be in some consistent character set
- * and encoding. On Windows, it is in UTF-8. On Windows, in case the
- * environment variable's value contains references to other
- * environment variables, they are expanded.
- *
- * Return value: the value of the environment variable, or %NULL if
- * the environment variable is not found. The returned string may be
- * overwritten by the next call to g_getenv(), g_setenv() or
- * g_unsetenv().
- **/
-const gchar *
-g_getenv (const gchar *variable)
-{
-#ifndef G_OS_WIN32
-
- g_return_val_if_fail (variable != NULL, NULL);
-
- return getenv (variable);
-
-#else /* G_OS_WIN32 */
-
- GQuark quark;
- gchar *value;
- wchar_t dummy[2], *wname, *wvalue;
- int len;
-
- g_return_val_if_fail (variable != NULL, NULL);
- g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
-
- /* On Windows NT, it is relatively typical that environment
- * variables contain references to other environment variables. If
- * so, use ExpandEnvironmentStrings(). (In an ideal world, such
- * environment variables would be stored in the Registry as
- * REG_EXPAND_SZ type values, and would then get automatically
- * expanded before a program sees them. But there is broken software
- * that stores environment variables as REG_SZ values even if they
- * contain references to other environment variables.)
- */
-
- wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
-
- len = GetEnvironmentVariableW (wname, dummy, 2);
-
- if (len == 0)
- {
- g_free (wname);
- return NULL;
- }
- else if (len == 1)
- len = 2;
-
- wvalue = g_new (wchar_t, len);
-
- if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
- {
- g_free (wname);
- g_free (wvalue);
- return NULL;
- }
-
- if (wcschr (wvalue, L'%') != NULL)
- {
- wchar_t *tem = wvalue;
-
- len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
-
- if (len > 0)
- {
- wvalue = g_new (wchar_t, len);
-
- if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
- {
- g_free (wvalue);
- wvalue = tem;
- }
- else
- g_free (tem);
- }
- }
-
- value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
-
- g_free (wname);
- g_free (wvalue);
-
- quark = g_quark_from_string (value);
- g_free (value);
-
- return g_quark_to_string (quark);
-
-#endif /* G_OS_WIN32 */
-}
-
-/* _g_getenv_nomalloc
- * this function does a getenv() without doing any kind of allocation
- * through glib. it's suitable for chars <= 127 only (both, for the
- * variable name and the contents) and for contents < 1024 chars in
- * length. also, it aliases "" to a NULL return value.
- **/
-const gchar*
-_g_getenv_nomalloc (const gchar *variable,
- gchar buffer[1024])
-{
- const gchar *retval = getenv (variable);
- if (retval && retval[0])
- {
- gint l = strlen (retval);
- if (l < 1024)
- {
- strncpy (buffer, retval, l);
- buffer[l] = 0;
- return buffer;
- }
- }
- return NULL;
-}
-
-/**
- * g_setenv:
- * @variable: the environment variable to set, must not contain '='.
- * @value: the value for to set the variable to.
- * @overwrite: whether to change the variable if it already exists.
- *
- * Sets an environment variable. Both the variable's name and value
- * should be in the GLib file name encoding. On UNIX, this means that
- * they can be arbitrary byte strings. On Windows, they should be in
- * UTF-8.
- *
- * Note that on some systems, when variables are overwritten, the memory
- * used for the previous variables and its value isn't reclaimed.
- *
- * <warning>
- * Environment variable handling in UNIX is not thread-safe, and your
- * program may crash if one thread calls g_setenv() while another
- * thread is calling getenv(). (And note that many functions, such as
- * gettext(), call getenv() internally.) This function is only safe to
- * use at the very start of your program, before creating any other
- * threads (or creating objects that create worker threads of their
- * own).
- *
- * If you need to set up the environment for a child process, you can
- * use g_get_environ() to get an environment array, modify that with
- * g_environ_setenv() and g_environ_unsetenv(), and then pass that
- * array directly to execvpe(), g_spawn_async(), or the like.
- * </warning>
- *
- * Returns: %FALSE if the environment variable couldn't be set.
- *
- * Since: 2.4
- */
-gboolean
-g_setenv (const gchar *variable,
- const gchar *value,
- gboolean overwrite)
-{
-#ifndef G_OS_WIN32
-
- gint result;
-#ifndef HAVE_SETENV
- gchar *string;
-#endif
-
- g_return_val_if_fail (variable != NULL, FALSE);
- g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
-
-#ifdef HAVE_SETENV
- result = setenv (variable, value, overwrite);
-#else
- if (!overwrite && getenv (variable) != NULL)
- return TRUE;
-
- /* This results in a leak when you overwrite existing
- * settings. It would be fairly easy to fix this by keeping
- * our own parallel array or hash table.
- */
- string = g_strconcat (variable, "=", value, NULL);
- result = putenv (string);
-#endif
- return result == 0;
-
-#else /* G_OS_WIN32 */
-
- gboolean retval;
- wchar_t *wname, *wvalue, *wassignment;
- gchar *tem;
-
- g_return_val_if_fail (variable != NULL, FALSE);
- g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
- g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
- g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
-
- if (!overwrite && g_getenv (variable) != NULL)
- return TRUE;
-
- /* We want to (if possible) set both the environment variable copy
- * kept by the C runtime and the one kept by the system.
- *
- * We can't use only the C runtime's putenv or _wputenv() as that
- * won't work for arbitrary Unicode strings in a "non-Unicode" app
- * (with main() and not wmain()). In a "main()" app the C runtime
- * initializes the C runtime's environment table by converting the
- * real (wide char) environment variables to system codepage, thus
- * breaking those that aren't representable in the system codepage.
- *
- * As the C runtime's putenv() will also set the system copy, we do
- * the putenv() first, then call SetEnvironmentValueW ourselves.
- */
-
- wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
- wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
- tem = g_strconcat (variable, "=", value, NULL);
- wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
-
- g_free (tem);
- _wputenv (wassignment);
- g_free (wassignment);
-
- retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
-
- g_free (wname);
- g_free (wvalue);
-
- return retval;
-
-#endif /* G_OS_WIN32 */
-}
-
-#ifdef HAVE__NSGETENVIRON
-#define environ (*_NSGetEnviron())
-#elif !defined(G_OS_WIN32)
-
-/* According to the Single Unix Specification, environ is not in
- * any system header, although unistd.h often declares it.
- */
-extern char **environ;
-#endif
-
-/**
- * g_unsetenv:
- * @variable: the environment variable to remove, must not contain '='.
- *
- * Removes an environment variable from the environment.
- *
- * Note that on some systems, when variables are overwritten, the memory
- * used for the previous variables and its value isn't reclaimed.
- *
- * <warning>
- * Environment variable handling in UNIX is not thread-safe, and your
- * program may crash if one thread calls g_unsetenv() while another
- * thread is calling getenv(). (And note that many functions, such as
- * gettext(), call getenv() internally.) This function is only safe to
- * use at the very start of your program, before creating any other
- * threads (or creating objects that create worker threads of their
- * own).
- *
- * If you need to set up the environment for a child process, you can
- * use g_get_environ() to get an environment array, modify that with
- * g_environ_setenv() and g_environ_unsetenv(), and then pass that
- * array directly to execvpe(), g_spawn_async(), or the like.
- * </warning>
- *
- * Since: 2.4
- **/
-void
-g_unsetenv (const gchar *variable)
-{
-#ifndef G_OS_WIN32
-
-#ifdef HAVE_UNSETENV
- g_return_if_fail (variable != NULL);
- g_return_if_fail (strchr (variable, '=') == NULL);
-
- unsetenv (variable);
-#else /* !HAVE_UNSETENV */
- g_return_if_fail (variable != NULL);
- g_return_if_fail (strchr (variable, '=') == NULL);
-
- /* Mess directly with the environ array.
- * This seems to be the only portable way to do this.
- */
- g_environ_unsetenv (environ, variable);
-#endif /* !HAVE_UNSETENV */
-
-#else /* G_OS_WIN32 */
-
- wchar_t *wname, *wassignment;
- gchar *tem;
-
- g_return_if_fail (variable != NULL);
- g_return_if_fail (strchr (variable, '=') == NULL);
- g_return_if_fail (g_utf8_validate (variable, -1, NULL));
-
- wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
- tem = g_strconcat (variable, "=", NULL);
- wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
-
- g_free (tem);
- _wputenv (wassignment);
- g_free (wassignment);
-
- SetEnvironmentVariableW (wname, NULL);
-
- g_free (wname);
-
-#endif /* G_OS_WIN32 */
-}
-
-/**
- * g_listenv:
- *
- * Gets the names of all variables set in the environment.
- *
- * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated list of strings which must be freed
- * with g_strfreev().
- *
- * Programs that want to be portable to Windows should typically use
- * this function and g_getenv() instead of using the environ array
- * from the C library directly. On Windows, the strings in the environ
- * array are in system codepage encoding, while in most of the typical
- * use cases for environment variables in GLib-using programs you want
- * the UTF-8 encoding that this function and g_getenv() provide.
- *
- * Since: 2.8
- */
-gchar **
-g_listenv (void)
-{
-#ifndef G_OS_WIN32
- gchar **result, *eq;
- gint len, i, j;
-
- len = g_strv_length (environ);
- result = g_new0 (gchar *, len + 1);
-
- j = 0;
- for (i = 0; i < len; i++)
- {
- eq = strchr (environ[i], '=');
- if (eq)
- result[j++] = g_strndup (environ[i], eq - environ[i]);
- }
-
- result[j] = NULL;
-
- return result;
-#else
- gchar **result, *eq;
- gint len = 0, j;
- wchar_t *p, *q;
-
- p = (wchar_t *) GetEnvironmentStringsW ();
- if (p != NULL)
- {
- q = p;
- while (*q)
- {
- q += wcslen (q) + 1;
- len++;
- }
- }
- result = g_new0 (gchar *, len + 1);
-
- j = 0;
- q = p;
- while (*q)
- {
- result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
- if (result[j] != NULL)
- {
- eq = strchr (result[j], '=');
- if (eq && eq > result[j])
- {
- *eq = '\0';
- j++;
- }
- else
- g_free (result[j]);
- }
- q += wcslen (q) + 1;
- }
- result[j] = NULL;
- FreeEnvironmentStringsW (p);
-
- return result;
-#endif
-}
-
-/**
- * g_get_environ:
- *
- * Gets the list of environment variables for the current process. The
- * list is %NULL terminated and each item in the list is of the form
- * 'NAME=VALUE'.
- *
- * This is equivalent to direct access to the 'environ' global variable,
- * except portable.
- *
- * The return value is freshly allocated and it should be freed with
- * g_strfreev() when it is no longer needed.
- *
- * Returns: (array zero-terminated=1) (transfer full): the list of
- * environment variables
- *
- * Since: 2.28
- */
-gchar **
-g_get_environ (void)
-{
-#ifndef G_OS_WIN32
- return g_strdupv (environ);
-#else
- gunichar2 *strings;
- gchar **result;
- gint i, n;
-
- strings = GetEnvironmentStringsW ();
- for (n = 0; strings[n]; n += wcslen (strings + n) + 1);
- result = g_new (char *, n + 1);
- for (i = 0; strings[i]; i += wcslen (strings + i) + 1)
- result[i] = g_utf16_to_utf8 (strings + i, -1, NULL, NULL, NULL);
- FreeEnvironmentStringsW (strings);
- result[i] = NULL;
-
- return result;
-#endif
-}
-
-static gint
-g_environ_find (gchar **envp,
- const gchar *variable)
-{
- gint len, i;
-
- len = strlen (variable);
-
- for (i = 0; envp[i]; i++)
- {
- if (strncmp (envp[i], variable, len) == 0 &&
- envp[i][len] == '=')
- return i;
- }
-
- return -1;
-}
-
-/**
- * g_environ_getenv:
- * @envp: (array zero-terminated=1) (transfer none): an environment
- * list (eg, as returned from g_get_environ())
- * @variable: the environment variable to get, in the GLib file name
- * encoding
- *
- * Returns the value of the environment variable @variable in the
- * provided list @envp.
- *
- * The name and value are in the GLib file name encoding.
- * On UNIX, this means the actual bytes which might or might not
- * be in some consistent character set and encoding. On Windows,
- * it is in UTF-8. On Windows, in case the environment variable's
- * value contains references to other environment variables, they
- * are expanded.
- *
- * Return value: the value of the environment variable, or %NULL if
- * the environment variable is not set in @envp. The returned
- * string is owned by @envp, and will be freed if @variable is
- * set or unset again.
- *
- * Since: 2.32
- */
-const gchar *
-g_environ_getenv (gchar **envp,
- const gchar *variable)
-{
- gint index;
-
- g_return_val_if_fail (envp != NULL, NULL);
- g_return_val_if_fail (variable != NULL, NULL);
-
- index = g_environ_find (envp, variable);
- if (index != -1)
- return envp[index] + strlen (variable) + 1;
- else
- return NULL;
-}
-
-/**
- * g_environ_setenv:
- * @envp: (array zero-terminated=1) (transfer full): an environment
- * list (eg, as returned from g_get_environ())
- * @variable: the environment variable to set, must not contain '='
- * @value: the value for to set the variable to
- * @overwrite: whether to change the variable if it already exists
- *
- * Sets the environment variable @variable in the provided list
- * @envp to @value.
- *
- * Both the variable's name and value should be in the GLib
- * file name encoding. On UNIX, this means that they can be
- * arbitrary byte strings. On Windows, they should be in UTF-8.
- *
- * Return value: (array zero-terminated=1) (transfer full): the
- * updated environment
- *
- * Since: 2.32
- */
-gchar **
-g_environ_setenv (gchar **envp,
- const gchar *variable,
- const gchar *value,
- gboolean overwrite)
-{
- gint index;
-
- g_return_val_if_fail (envp != NULL, NULL);
- g_return_val_if_fail (variable != NULL, NULL);
- g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
-
- index = g_environ_find (envp, variable);
- if (index != -1)
- {
- if (overwrite)
- {
- g_free (envp[index]);
- envp[index] = g_strdup_printf ("%s=%s", variable, value);
- }
- }
- else
- {
- gint length;
-
- length = g_strv_length (envp);
- envp = g_renew (gchar *, envp, length + 2);
- envp[length] = g_strdup_printf ("%s=%s", variable, value);
- envp[length + 1] = NULL;
- }
-
- return envp;
-}
-
-/**
- * g_environ_unsetenv:
- * @envp: (array zero-terminated=1) (transfer full): an environment
- * list (eg, as returned from g_get_environ())
- * @variable: the environment variable to remove, must not contain '='
- *
- * Removes the environment variable @variable from the provided
- * environment @envp.
- *
- * Return value: (array zero-terminated=1) (transfer full): the
- * updated environment
- *
- * Since: 2.32
- */
-gchar **
-g_environ_unsetenv (gchar **envp,
- const gchar *variable)
-{
- gint len;
- gchar **e, **f;
-
- g_return_val_if_fail (envp != NULL, NULL);
- g_return_val_if_fail (variable != NULL, NULL);
- g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
-
- len = strlen (variable);
-
- /* Note that we remove *all* environment entries for
- * the variable name, not just the first.
- */
- e = f = envp;
- while (*e != NULL)
- {
- if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
- {
- *f = *e;
- f++;
- }
- e++;
- }
- *f = NULL;
-
- return envp;
-}
-
G_LOCK_DEFINE_STATIC (g_utils_global);
static gchar *g_tmp_dir = NULL;
diff --git a/glib/gutils.h b/glib/gutils.h
index 47d4b16ff..2e7c76740 100644
--- a/glib/gutils.h
+++ b/glib/gutils.h
@@ -254,38 +254,6 @@ gchar* g_path_get_dirname (const gchar *file_name) G_GNUC_MALLO
/* Set the pointer at the specified location to NULL */
void g_nullify_pointer (gpointer *nullify_location);
-/* return the environment string for the variable. The returned memory
- * must not be freed. */
-#ifndef __GTK_DOC_IGNORE__
-#ifdef G_OS_WIN32
-#define g_getenv g_getenv_utf8
-#define g_setenv g_setenv_utf8
-#define g_unsetenv g_unsetenv_utf8
-#define g_find_program_in_path g_find_program_in_path_utf8
-#endif
-#endif
-
-const gchar * g_getenv (const gchar *variable);
-gboolean g_setenv (const gchar *variable,
- const gchar *value,
- gboolean overwrite);
-void g_unsetenv (const gchar *variable);
-gchar ** g_listenv (void);
-
-gchar ** g_get_environ (void);
-const gchar * g_environ_getenv (gchar **envp,
- const gchar *variable);
-gchar ** g_environ_setenv (gchar **envp,
- const gchar *variable,
- const gchar *value,
- gboolean overwrite) G_GNUC_WARN_UNUSED_RESULT;
-gchar ** g_environ_unsetenv (gchar **envp,
- const gchar *variable) G_GNUC_WARN_UNUSED_RESULT;
-
-/* private */
-const gchar* _g_getenv_nomalloc (const gchar *variable,
- gchar buffer[1024]);
-
/**
* GVoidFunc:
*
@@ -319,6 +287,12 @@ int atexit (void (*)(void));
#define g_atexit(func) atexit(func)
#endif
+#ifndef __GTK_DOC_IGNORE__
+#ifdef G_OS_WIN32
+#define g_find_program_in_path g_find_program_in_path_utf8
+#endif
+#endif
+
/* Look for an executable in PATH, following execvp() rules */
gchar* g_find_program_in_path (const gchar *program);