From f7234027082a4cbc019438bb9101a0d0b85ceddd Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 28 Jul 2003 19:24:22 +0000 Subject: [PATCH] Add g_setenv() and g_unsetenv(). (#100763) * glib/gutils.[ch]: Add g_setenv() and g_unsetenv(). (#100763) --- ChangeLog | 5 ++ ChangeLog.pre-2-10 | 5 ++ ChangeLog.pre-2-12 | 5 ++ ChangeLog.pre-2-4 | 5 ++ ChangeLog.pre-2-6 | 5 ++ ChangeLog.pre-2-8 | 5 ++ docs/reference/ChangeLog | 4 ++ docs/reference/glib/glib-sections.txt | 2 + glib/gutils.c | 83 ++++++++++++++++++++++++++- glib/gutils.h | 4 ++ 10 files changed, 122 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 98792d0f1..ae6715334 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2003-07-28 Matthias Clasen + + * glib/gutils.h: + * glib/gutils.c: New functions g_setenv() and g_unsetenv(). (#100763) + 2003-07-26 Matthias Clasen * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 98792d0f1..ae6715334 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,8 @@ +2003-07-28 Matthias Clasen + + * glib/gutils.h: + * glib/gutils.c: New functions g_setenv() and g_unsetenv(). (#100763) + 2003-07-26 Matthias Clasen * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 98792d0f1..ae6715334 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,8 @@ +2003-07-28 Matthias Clasen + + * glib/gutils.h: + * glib/gutils.c: New functions g_setenv() and g_unsetenv(). (#100763) + 2003-07-26 Matthias Clasen * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 98792d0f1..ae6715334 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,8 @@ +2003-07-28 Matthias Clasen + + * glib/gutils.h: + * glib/gutils.c: New functions g_setenv() and g_unsetenv(). (#100763) + 2003-07-26 Matthias Clasen * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 98792d0f1..ae6715334 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,8 @@ +2003-07-28 Matthias Clasen + + * glib/gutils.h: + * glib/gutils.c: New functions g_setenv() and g_unsetenv(). (#100763) + 2003-07-26 Matthias Clasen * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 98792d0f1..ae6715334 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,8 @@ +2003-07-28 Matthias Clasen + + * glib/gutils.h: + * glib/gutils.c: New functions g_setenv() and g_unsetenv(). (#100763) + 2003-07-26 Matthias Clasen * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index aa8474e02..616fb2e73 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,7 @@ +2003-07-28 Matthias Clasen + + * glib/glib-sections.txt: Add g_setenv() and g_unsetenv(). (#100763) + 2003-07-26 Matthias Clasen * glib/tmpl/arrays.sgml: diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 06ec35254..379383de2 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -1161,6 +1161,8 @@ g_set_application_name g_get_prgname g_set_prgname g_getenv +g_setenv +g_unsetenv g_get_user_name g_get_real_name diff --git a/glib/gutils.c b/glib/gutils.c index 973bb98b0..d534288e6 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -635,7 +635,7 @@ g_getenv (const gchar *variable) if (!environs) environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct)); - /* First we try to find the envinronment variable inside the already + /* First we try to find the environment variable inside the already * found ones. */ @@ -685,6 +685,87 @@ g_getenv (const gchar *variable) #endif } +/** + * g_setenv: + * @variable: the environment variable to set. + * @value: the value for to set the variable to. + * @overwrite: whether to change the variable if it already exists. + * + * Sets an environment variable. + * + * Note that on some systems, the memory used for the variable and its value + * can't be reclaimed later. + * + * Returns: %FALSE if the environment variable couldn't be set. + * + * Since: 2.4 + */ +gboolean +g_setenv (const gchar *variable, + const gchar *value, + gboolean overwrite) +{ + gint result; +#ifdef HAVE_SETENV + result = setenv (variable, value, overwrite); +#else + gchar *string; + + if (!overwrite && g_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; +} + +/** + * g_unsetenv: + * @name: the environment variable to remove. + * + * Removes an environment variable from the environment. + * + * Note that on some systems, the memory used for the variable and its value + * can't be reclaimed. Furthermore, this function can't be guaranteed to operate in a + * threadsafe way. + * + * Since: 2.4 + **/ +void +g_unsetenv (const gchar *variable) +{ +#ifdef HAVE_UNSETENV + unsetenv (variable); +#else + int i, len; + gchar **e, **f; + + len = strlen (variable); + + /* Mess directly with the environ array. + * This seems to be the only portable way to do this. + * + * Note that we remove *all* environment entries for + * the variable name, not just the first. + */ + e = f = environ; + while (*e != NULL) + { + if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=') + { + *f = *e; + f++; + } + e++; + } + *f = NULL; +#endif +} G_LOCK_DEFINE_STATIC (g_utils_global); diff --git a/glib/gutils.h b/glib/gutils.h index b6af7030e..d80702ae7 100644 --- a/glib/gutils.h +++ b/glib/gutils.h @@ -177,6 +177,10 @@ void g_nullify_pointer (gpointer *nullify_location); /* return the environment string for the variable. The returned memory * must not be freed. */ G_CONST_RETURN gchar* g_getenv (const gchar *variable); +gboolean g_setenv (const gchar *variable, + const gchar *value, + gboolean overwrite); +void g_unsetenv (const gchar *variable); /* we try to provide a usefull equivalent for ATEXIT if it is