From 6489e6a5d5bd7d6e0a844a77db153a25f2edc3c0 Mon Sep 17 00:00:00 2001 From: Tor Lillqvist Date: Sun, 10 Aug 2003 08:04:10 +0000 Subject: [PATCH] Don't use a cache of variable name to value mappings on Win32, as that 2003-08-10 Tor Lillqvist * glib/gutils.c (g_getenv): Don't use a cache of variable name to value mappings on Win32, as that breaks g_setenv() and g_unsetenv(). Only call ExpandEnvironmentStrings() if necessary, and in that case return a quarkified string. It is still questionable how necessary expanding embedded environment variable references is. Possibly the whole Win32-specific g_getenv() implementation could be removed. (#119520) --- ChangeLog | 10 +++++++ ChangeLog.pre-2-10 | 10 +++++++ ChangeLog.pre-2-12 | 10 +++++++ ChangeLog.pre-2-4 | 10 +++++++ ChangeLog.pre-2-6 | 10 +++++++ ChangeLog.pre-2-8 | 10 +++++++ glib/gutils.c | 83 +++++++++++++++++++----------------------------------- 7 files changed, 89 insertions(+), 54 deletions(-) diff --git a/ChangeLog b/ChangeLog index 002d167..ec843b5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2003-08-10 Tor Lillqvist + + * glib/gutils.c (g_getenv): Don't use a cache of variable name to + value mappings on Win32, as that breaks g_setenv() and + g_unsetenv(). Only call ExpandEnvironmentStrings() if necessary, + and in that case return a quarkified string. It is still + questionable how necessary expanding embedded environment variable + references is. Possibly the whole Win32-specific g_getenv() + implementation could be removed. (#119520) + 2003-08-08 Tor Lillqvist * glib/glib.def: Add g_once_impl. diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 002d167..ec843b5 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,13 @@ +2003-08-10 Tor Lillqvist + + * glib/gutils.c (g_getenv): Don't use a cache of variable name to + value mappings on Win32, as that breaks g_setenv() and + g_unsetenv(). Only call ExpandEnvironmentStrings() if necessary, + and in that case return a quarkified string. It is still + questionable how necessary expanding embedded environment variable + references is. Possibly the whole Win32-specific g_getenv() + implementation could be removed. (#119520) + 2003-08-08 Tor Lillqvist * glib/glib.def: Add g_once_impl. diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 002d167..ec843b5 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,13 @@ +2003-08-10 Tor Lillqvist + + * glib/gutils.c (g_getenv): Don't use a cache of variable name to + value mappings on Win32, as that breaks g_setenv() and + g_unsetenv(). Only call ExpandEnvironmentStrings() if necessary, + and in that case return a quarkified string. It is still + questionable how necessary expanding embedded environment variable + references is. Possibly the whole Win32-specific g_getenv() + implementation could be removed. (#119520) + 2003-08-08 Tor Lillqvist * glib/glib.def: Add g_once_impl. diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 002d167..ec843b5 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,13 @@ +2003-08-10 Tor Lillqvist + + * glib/gutils.c (g_getenv): Don't use a cache of variable name to + value mappings on Win32, as that breaks g_setenv() and + g_unsetenv(). Only call ExpandEnvironmentStrings() if necessary, + and in that case return a quarkified string. It is still + questionable how necessary expanding embedded environment variable + references is. Possibly the whole Win32-specific g_getenv() + implementation could be removed. (#119520) + 2003-08-08 Tor Lillqvist * glib/glib.def: Add g_once_impl. diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 002d167..ec843b5 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,13 @@ +2003-08-10 Tor Lillqvist + + * glib/gutils.c (g_getenv): Don't use a cache of variable name to + value mappings on Win32, as that breaks g_setenv() and + g_unsetenv(). Only call ExpandEnvironmentStrings() if necessary, + and in that case return a quarkified string. It is still + questionable how necessary expanding embedded environment variable + references is. Possibly the whole Win32-specific g_getenv() + implementation could be removed. (#119520) + 2003-08-08 Tor Lillqvist * glib/glib.def: Add g_once_impl. diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 002d167..ec843b5 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,13 @@ +2003-08-10 Tor Lillqvist + + * glib/gutils.c (g_getenv): Don't use a cache of variable name to + value mappings on Win32, as that breaks g_setenv() and + g_unsetenv(). Only call ExpandEnvironmentStrings() if necessary, + and in that case return a quarkified string. It is still + questionable how necessary expanding embedded environment variable + references is. Possibly the whole Win32-specific g_getenv() + implementation could be removed. (#119520) + 2003-08-08 Tor Lillqvist * glib/glib.def: Add g_once_impl. diff --git a/glib/gutils.c b/glib/gutils.c index c98f362..6ea3b71 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -627,71 +627,46 @@ g_getenv (const gchar *variable) return getenv (variable); #else - G_LOCK_DEFINE_STATIC (getenv); - struct env_struct - { - gchar *key; - gchar *value; - } *env; - static GArray *environs = NULL; + GQuark quark; gchar *system_env; - guint length, i; + gchar *expanded_env; + guint length; gchar dummy[2]; g_return_val_if_fail (variable != NULL, NULL); - G_LOCK (getenv); - - if (!environs) - environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct)); - - /* First we try to find the environment variable inside the already - * found ones. - */ - - for (i = 0; i < environs->len; i++) - { - env = &g_array_index (environs, struct env_struct, i); - if (strcmp (env->key, variable) == 0) - { - g_assert (env->value); - G_UNLOCK (getenv); - return env->value; - } - } - - /* If not found, we ask the system */ - system_env = getenv (variable); if (!system_env) - { - G_UNLOCK (getenv); - return NULL; - } - - /* On Windows NT, it is relatively typical that environment variables - * contain references to other environment variables. Handle that by - * calling ExpandEnvironmentStrings. + return NULL; + + /* On Windows NT, it is relatively typical that environment + * variables contain references to other environment variables. If + * so, use ExpandEnvironmentStrings(). (If all software was written + * in the best possible way, such environment variables would be + * stored in the Registry as REG_EXPAND_SZ type values, and would + * then get automatically expanded before the 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. */ - g_array_set_size (environs, environs->len + 1); - - env = &g_array_index (environs, struct env_struct, environs->len - 1); + if (strchr (system_env, '%') == NULL) + { + /* No reference to other variable(s), return value as such. */ + return system_env; + } /* First check how much space we need */ length = ExpandEnvironmentStrings (system_env, dummy, 2); - - /* Then allocate that much, and actualy do the expansion and insert - * the new found pair into our buffer - */ - - env->value = g_malloc (length); - env->key = g_strdup (variable); - - ExpandEnvironmentStrings (system_env, env->value, length); - - G_UNLOCK (getenv); - return env->value; + + expanded_env = g_malloc (length); + + ExpandEnvironmentStrings (system_env, expanded_env, length); + + quark = g_quark_from_string (expanded_env); + g_free (expanded_env); + + return g_quark_to_string (quark); #endif } @@ -765,7 +740,7 @@ g_unsetenv (const gchar *variable) unsetenv (variable); #else - int i, len; + int len; gchar **e, **f; g_return_if_fail (strchr (variable, '=') == NULL); -- 2.7.4