Add g_setenv() and g_unsetenv(). (#100763)
authorMatthias Clasen <matthiasc@src.gnome.org>
Mon, 28 Jul 2003 19:24:22 +0000 (19:24 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Mon, 28 Jul 2003 19:24:22 +0000 (19:24 +0000)
* glib/gutils.[ch]: Add g_setenv() and g_unsetenv().  (#100763)

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
docs/reference/ChangeLog
docs/reference/glib/glib-sections.txt
glib/gutils.c
glib/gutils.h

index 98792d0..ae67153 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2003-07-28  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gutils.h: 
+       * glib/gutils.c: New functions g_setenv() and g_unsetenv().  (#100763)
+
 2003-07-26  Matthias Clasen  <maclas@gmx.de>
 
        * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am
index 98792d0..ae67153 100644 (file)
@@ -1,3 +1,8 @@
+2003-07-28  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gutils.h: 
+       * glib/gutils.c: New functions g_setenv() and g_unsetenv().  (#100763)
+
 2003-07-26  Matthias Clasen  <maclas@gmx.de>
 
        * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am
index 98792d0..ae67153 100644 (file)
@@ -1,3 +1,8 @@
+2003-07-28  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gutils.h: 
+       * glib/gutils.c: New functions g_setenv() and g_unsetenv().  (#100763)
+
 2003-07-26  Matthias Clasen  <maclas@gmx.de>
 
        * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am
index 98792d0..ae67153 100644 (file)
@@ -1,3 +1,8 @@
+2003-07-28  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gutils.h: 
+       * glib/gutils.c: New functions g_setenv() and g_unsetenv().  (#100763)
+
 2003-07-26  Matthias Clasen  <maclas@gmx.de>
 
        * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am
index 98792d0..ae67153 100644 (file)
@@ -1,3 +1,8 @@
+2003-07-28  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gutils.h: 
+       * glib/gutils.c: New functions g_setenv() and g_unsetenv().  (#100763)
+
 2003-07-26  Matthias Clasen  <maclas@gmx.de>
 
        * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am
index 98792d0..ae67153 100644 (file)
@@ -1,3 +1,8 @@
+2003-07-28  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/gutils.h: 
+       * glib/gutils.c: New functions g_setenv() and g_unsetenv().  (#100763)
+
 2003-07-26  Matthias Clasen  <maclas@gmx.de>
 
        * tests/printf-test.c: New test, tests printf behaviour. This was already mentioned in Makefile.am
index aa8474e..616fb2e 100644 (file)
@@ -1,3 +1,7 @@
+2003-07-28  Matthias Clasen  <maclas@gmx.de>
+
+       * glib/glib-sections.txt: Add g_setenv() and g_unsetenv().  (#100763)
+
 2003-07-26  Matthias Clasen  <maclas@gmx.de>
 
        * glib/tmpl/arrays.sgml: 
index 06ec352..379383d 100644 (file)
@@ -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
 
index 973bb98..d534288 100644 (file)
@@ -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);
 
index b6af703..d80702a 100644 (file)
@@ -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