Add utils_copy_environment() and make use of it.

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@4538 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
Enrico Tröger 2010-01-24 14:18:00 +00:00
parent 56eb9267bb
commit fa49065678
4 changed files with 93 additions and 35 deletions

View File

@ -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:

View File

@ -1894,3 +1894,86 @@ gchar *utils_get_help_url(const gchar *suffix)
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;
}

View File

@ -229,4 +229,6 @@ gchar *utils_str_middle_truncate(const gchar *string, guint truncate_length);
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

View File

@ -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 @@ static const GtkTargetEntry dnd_targets[] =
};
/* 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);
}