[monkey-bubble: 655/753] Add portable setenv, unsetenv, clearenv implementations since they still



commit 2e18b5e4c4b319eb4375119b23f5c8c051732afa
Author: George Lebl <jirka 5z com>
Date:   Fri Aug 24 10:09:36 2001 +0000

    Add portable setenv, unsetenv, clearenv implementations since they still
    
    Fri Aug 24 09:59:14 2001  George Lebl <jirka 5z com>
    
    	* acconfig.h, configure.in, libgnome/gnome-utils.[ch]:  Add portable
    	  setenv, unsetenv, clearenv implementations since they still didn't
    	  get into glib.  If they get there these can be easily replaced
    	  by macros or just punted.  They are simple yet very useful to
    	  have to write portable code
    
    	* libgnome/gnome-utils.c:  make gnome_is_program_in_path use 'access'
    	  with X_OK to check only for actually executable files and it also
    	  regets the PATH every time so that it follows the current PATH
    	  setting.  Note that this cannot possibly be the bottleneck in this
    	  function and the "access" is.
    
    	* configure.in: get rid of this elitist evil CERTIFIED_GNOMIE
    	  bullshit

 ChangeLog             |   17 ++++++++
 acconfig.h            |    3 +
 configure.in          |    7 +--
 libgnome/gnome-util.c |  110 +++++++++++++++++++++++++++++++++++++++++++++----
 libgnome/gnome-util.h |    8 ++++
 5 files changed, 131 insertions(+), 14 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 051928a..1addc2e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+Fri Aug 24 09:59:14 2001  George Lebl <jirka 5z com>
+
+	* acconfig.h, configure.in, libgnome/gnome-utils.[ch]:  Add portable
+	  setenv, unsetenv, clearenv implementations since they still didn't
+	  get into glib.  If they get there these can be easily replaced
+	  by macros or just punted.  They are simple yet very useful to
+	  have to write portable code
+
+	* libgnome/gnome-utils.c:  make gnome_is_program_in_path use 'access'
+	  with X_OK to check only for actually executable files and it also
+	  regets the PATH every time so that it follows the current PATH
+	  setting.  Note that this cannot possibly be the bottleneck in this
+	  function and the "access" is.
+
+	* configure.in: get rid of this elitist evil CERTIFIED_GNOMIE
+	  bullshit
+
 2001-08-14  Michael Meeks  <michael ximian com>
 
 	* configure.in: add a bonobo-config dependency.
diff --git a/acconfig.h b/acconfig.h
index 06ae7e6..630aa45 100644
--- a/acconfig.h
+++ b/acconfig.h
@@ -8,3 +8,6 @@
 #undef GNOME_EXCLUDE_DEPRECATED_SOURCE
 #undef GNOME_EXCLUDE_DEPRECATED
 #undef HAVE_GNOMESUPPORT
+#undef HAVE_SETENV
+#undef HAVE_UNSETENV
+#undef HAVE_CLEARENV
diff --git a/configure.in b/configure.in
index 62fc73b..5a7bb47 100644
--- a/configure.in
+++ b/configure.in
@@ -6,11 +6,6 @@ AM_INIT_AUTOMAKE(libgnome2, 1.102.0)
 
 AM_MAINTAINER_MODE
 
-if test -z "$CERTIFIED_GNOMIE"; then
-  cat $srcdir/message-of-doom
-  exit 1
-fi
-
 dnl
 dnl Due to the sed scripts being split on 90 line
 dnl blocks, this macro needs to be right at the beggining.
@@ -30,6 +25,8 @@ AM_PROG_LIBTOOL
 AC_PROG_YACC
 AC_PATH_PROGS(PATH_TO_XRDB, "xrdb")
 
+AC_CHECK_FUNCS([setenv unsetenv clearenv])
+
 dnl utility conditional
 AM_CONDITIONAL(FALSE, test "x" = "y")
 
diff --git a/libgnome/gnome-util.c b/libgnome/gnome-util.c
index 3f18e5e..80058b7 100644
--- a/libgnome/gnome-util.c
+++ b/libgnome/gnome-util.c
@@ -191,20 +191,112 @@ g_copy_vector (const char **vec)
 char *
 gnome_is_program_in_path (const gchar *program)
 {
-	static gchar **paths = NULL;
+	gchar **paths = NULL;
 	gchar **p;
 	gchar *f;
 	
-	if (!paths)
-	  paths = g_strsplit(g_getenv("PATH"), ":", -1);
+	paths = g_strsplit (g_getenv ("PATH"), ":", -1);
 
-	p = paths;
-	while (*p){
-		f = g_strconcat (*p,"/",program, NULL);
-		if (g_file_test (f, G_FILE_TEST_EXISTS))
+	for (p = paths; *p != NULL; p++) {
+		f = g_strconcat (*p,"/", program, NULL);
+		if (access (f, X_OK) == 0) {
+			g_strfreev (paths);
 			return f;
+		}
 		g_free (f);
-		p++;
 	}
-	return 0;
+
+	g_strfreev (paths);
+	return NULL;
+}
+
+/**
+ * gnome_setenv:
+ * 
+ * Description: Adds "@name= value" to the environment
+ * Note that on systems without setenv, this leaks memory
+ * so please do not use inside a loop or anything like that.
+ * semantics are the same as the glibc setenv.  The @overwrite
+ * flag says that existing @name in the environment should be
+ * overwritten.
+ *
+ * Returns: 0 on success -1 on error
+ * 
+ **/
+int
+gnome_setenv (const char *name, const char *value, gboolean overwrite)
+{
+#if defined (HAVE_SETENV)
+	return setenv (name, value, overwrite);
+#else
+	char *string;
+	
+	if (! overwrite && g_getenv (name) != NULL) {
+		return 0;
+	}
+	
+	/* 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 (name, "=", value, NULL);
+	return putenv (string);
+#endif
+}
+
+/**
+ * gnome_unsetenv:
+ * @name: 
+ * 
+ * Description: Removes @name from the environment.
+ * In case there is no native implementation of unsetenv,
+ * this could cause leaks depending on the implementation of
+ * enviroment.
+ * 
+ **/
+void
+gnome_unsetenv (const char *name)
+{
+#if defined (HAVE_SETENV)
+	unsetenv (name);
+#else
+	extern char **environ;
+	int i, len;
+
+	len = strlen (name);
+	
+	/* Mess directly with the environ array.
+	 * This seems to be the only portable way to do this.
+	 */
+	for (i = 0; environ[i] != NULL; i++) {
+		if (strncmp (environ[i], name, len) == 0
+		    && environ[i][len + 1] == '=') {
+			break;
+		}
+	}
+	while (environ[i] != NULL) {
+		environ[i] = environ[i + 1];
+		i++;
+	}
+#endif
+}
+
+/**
+ * gnome_clearenv:
+ * 
+ * Description: Clears out the environment completely.
+ * In case there is no native implementation of clearenv,
+ * this could cause leaks depending on the implementation
+ * of enviroment.
+ * 
+ **/
+void
+gnome_clearenv (void)
+{
+#ifdef HAVE_CLEARENV
+	clearenv ();
+#else
+	extern char **environ;
+	environ[0] = NULL;
+#endif
 }
diff --git a/libgnome/gnome-util.h b/libgnome/gnome-util.h
index d5fd571..e2deda4 100644
--- a/libgnome/gnome-util.h
+++ b/libgnome/gnome-util.h
@@ -73,6 +73,14 @@ char ** g_copy_vector    (const char ** vec);
 /* Find the name of the user's shell.  */
 char *gnome_util_user_shell (void);
 
+/* Portable versions of setenv/unsetenv */
+
+/* Note: setenv will leak on some systems (those without setenv) so
+ * do NOT use inside a loop.  Semantics are the same as those in glibc */
+int	gnome_setenv (const char *name, const char *value, gboolean overwrite);
+void	gnome_unsetenv (const char *name);
+void	gnome_clearenv (void);
+
 G_END_DECLS
 
 #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]