SF.net SVN: geany:[4538] trunk

eht16 at users.sourceforge.net eht16 at xxxxx
Sun Jan 24 14:18:00 UTC 2010


Revision: 4538
          http://geany.svn.sourceforge.net/geany/?rev=4538&view=rev
Author:   eht16
Date:     2010-01-24 14:18:00 +0000 (Sun, 24 Jan 2010)

Log Message:
-----------
Add utils_copy_environment() and make use of it.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/utils.c
    trunk/src/utils.h
    trunk/src/vte.c

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2010-01-22 16:41:04 UTC (rev 4537)
+++ trunk/ChangeLog	2010-01-24 14:18:00 UTC (rev 4538)
@@ -1,3 +1,9 @@
+2010-01-24  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>
+
+ * src/utils.c, src/utils.h, src/vte.c:
+   Add utils_copy_environment() and make use of it.
+
+
 2010-01-22  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
 
  * src/build.c:

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2010-01-22 16:41:04 UTC (rev 4537)
+++ trunk/src/utils.c	2010-01-24 14:18:00 UTC (rev 4538)
@@ -1894,3 +1894,86 @@
 	return uri;
 }
 
+
+static gboolean str_in_array(const gchar **haystack, const gchar *needle)
+{
+	const gchar **p;
+
+	for (p = haystack; *p != NULL; ++p)
+	{
+		if (utils_str_equal(*p, needle))
+			return TRUE;
+	}
+	return FALSE;
+}
+
+
+/* Copies the current environment into a new array.
+ * exclude_vars is a NULL-terminated array of variable names which should be not copied.
+ * All further arguments are key, value pairs of variables which should be added to
+ * the environment.
+ *
+ * The argument list must be terminated with NULL. */
+gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_varname, ...)
+{
+	gchar **result;
+	gchar **p;
+	gchar **env;
+	va_list args;
+	const gchar *key, *value;
+	guint n, o;
+
+	/* get all the environ variables */
+	env = g_listenv();
+
+	/* count the additional variables */
+	va_start(args, first_varname);
+	for (o = 1; va_arg(args, gchar*) != NULL; o++);
+	va_end(args);
+	/* the passed arguments should be even (key, value pairs) */
+	g_return_val_if_fail(o % 2 == 0, NULL);
+
+	o /= 2;
+
+	/* create an array large enough to hold the new environment */
+	n = g_strv_length(env);
+	/* 'n + o + 1' could leak a little bit when exclude_vars is set */
+	result = g_new(gchar *, n + o + 1);
+
+	/* copy the environment */
+	for (n = 0, p = env; *p != NULL; ++p)
+	{
+		/* copy the variable */
+		value = g_getenv(*p);
+		if (G_LIKELY(value != NULL))
+		{
+			/* skip excluded variables */
+			if (exclude_vars != NULL && str_in_array(exclude_vars, *p))
+				continue;
+
+			result[n++] = g_strconcat(*p, "=", value, NULL);
+		}
+	}
+	g_strfreev(env);
+
+	/* now add additional variables */
+	va_start(args, first_varname);
+	key = first_varname;
+	value = va_arg(args, gchar*);
+	while (key != NULL)
+	{
+		result[n++] = g_strconcat(key, "=", value, NULL);
+
+		key = va_arg(args, gchar*);
+		if (key == NULL)
+			break;
+		value = va_arg(args, gchar*);
+	}
+	va_end(args);
+
+	result[n] = NULL;
+
+	return result;
+}
+
+

Modified: trunk/src/utils.h
===================================================================
--- trunk/src/utils.h	2010-01-22 16:41:04 UTC (rev 4537)
+++ trunk/src/utils.h	2010-01-24 14:18:00 UTC (rev 4538)
@@ -229,4 +229,6 @@
 
 gchar *utils_str_remove_chars(gchar *string, const gchar *chars);
 
+gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_varname, ...) G_GNUC_NULL_TERMINATED;
+
 #endif

Modified: trunk/src/vte.c
===================================================================
--- trunk/src/vte.c	2010-01-22 16:41:04 UTC (rev 4537)
+++ trunk/src/vte.c	2010-01-24 14:18:00 UTC (rev 4538)
@@ -56,7 +56,6 @@
 VteInfo vte_info;
 VteConfig *vc;
 
-extern gchar **environ;
 static pid_t pid = 0;
 static gboolean clean = TRUE;
 static GModule *module = NULL;
@@ -155,43 +154,11 @@
 };
 
 
-/* taken from anjuta, thanks */
 static gchar **vte_get_child_environment(void)
 {
-	/* code from gnome-terminal, sort of. */
-	gchar **p;
-	gint i;
-	gchar **retval;
-#define EXTRA_ENV_VARS 5
+	const gchar *exclude_vars[] = {"COLUMNS", "LINES", "TERM", NULL};
 
-	/* count env vars that are set */
-	for (p = environ; *p; p++);
-
-	i = p - environ;
-	retval = g_new0(gchar *, i + 1 + EXTRA_ENV_VARS);
-
-	for (i = 0, p = environ; *p; p++)
-	{
-		/* Strip all these out, we'll replace some of them */
-		if ((strncmp(*p, "COLUMNS=", 8) == 0) ||
-		    (strncmp(*p, "LINES=", 6) == 0)   ||
-		    (strncmp(*p, "TERM=", 5) == 0))
-		{
-			/* nothing: do not copy */
-		}
-		else
-		{
-			retval[i] = g_strdup(*p);
-			++i;
-		}
-	}
-
-	retval[i] = g_strdup("TERM=xterm");
-	++i;
-
-	retval[i] = NULL;
-
-	return retval;
+	return utils_copy_environment(exclude_vars, "TERM", "xterm", NULL);
 }
 
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list