From 29ce7385bb631ac04dc8b965dd0c6fab488c770d Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Thu, 28 Oct 2010 21:26:09 -0400 Subject: [PATCH] Add g_get_environ(): portable access to 'environ' Return a copy of 'environ' on platforms where that is possible, or do something else on other platforms. --- docs/reference/glib/glib-sections.txt | 1 + glib/glib.symbols | 1 + glib/gutils.c | 39 +++++++++++++++++++++++++++ glib/gutils.h | 1 + 4 files changed, 42 insertions(+) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index eefb3915a..e3d4d591e 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -1597,6 +1597,7 @@ g_get_application_name g_set_application_name g_get_prgname g_set_prgname +g_get_environ g_getenv g_setenv g_unsetenv diff --git a/glib/glib.symbols b/glib/glib.symbols index c4211872c..b421788f4 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -1642,6 +1642,7 @@ g_get_host_name g_setenv PRIVATE #endif g_listenv +g_get_environ #ifdef G_OS_WIN32 g_find_program_in_path_utf8 g_get_current_dir_utf8 diff --git a/glib/gutils.c b/glib/gutils.c index 4395a52dd..94e395102 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -1453,6 +1453,45 @@ g_listenv (void) #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: 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 +} + G_LOCK_DEFINE_STATIC (g_utils_global); static gchar *g_tmp_dir = NULL; diff --git a/glib/gutils.h b/glib/gutils.h index 90281579c..2809e9056 100644 --- a/glib/gutils.h +++ b/glib/gutils.h @@ -258,6 +258,7 @@ gboolean g_setenv (const gchar *variable, gboolean overwrite); void g_unsetenv (const gchar *variable); gchar** g_listenv (void); +gchar** g_get_environ (void); /* private */ const gchar* _g_getenv_nomalloc (const gchar *variable,