execute: drop empty assignments from env blocks on execution but keep them around otherwise to make them visible

This commit is contained in:
Lennart Poettering 2011-01-06 20:38:02 +01:00
parent 7fc942b29e
commit a6ff950e71
5 changed files with 47 additions and 3 deletions

View file

@ -1303,6 +1303,8 @@ int exec_spawn(ExecCommand *command,
goto fail;
}
final_env = strv_env_clean(final_env);
execve(command->path, final_argv, final_env);
r = EXIT_EXEC;

View file

@ -1399,7 +1399,7 @@ static int config_parse_env_file(
goto finish;
}
t = strv_env_set(*env, u);
t = strv_append(*env, u);
free(u);
if (!t) {

View file

@ -380,7 +380,7 @@ static int env_append(char **r, char ***k, char **a) {
/* Add the entries of a to *k unless they already exist in *r
* in which case they are overriden instead. This assumes
* there is enough space in the r */
* there is enough space in the r array. */
for (; *a; a++) {
char **j;
@ -556,3 +556,24 @@ char *strv_env_get_with_length(char **l, const char *name, size_t k) {
char *strv_env_get(char **l, const char *name) {
return strv_env_get_with_length(l, name, strlen(name));
}
char **strv_env_clean(char **l) {
char **r, **ret;
for (r = ret = l; *l; l++) {
const char *equal;
equal = strchr(*l, '=');
if (equal && equal[1] == 0) {
free(*l);
continue;
}
*(r++) = *l;
}
*r = NULL;
return ret;
}

View file

@ -63,6 +63,8 @@ char **strv_env_set(char **x, const char *p);
char *strv_env_get_with_length(char **l, const char *name, size_t k);
char *strv_env_get(char **x, const char *n);
char **strv_env_clean(char **l);
#define STRV_FOREACH(s, l) \
for ((s) = (l); (s) && *(s); (s)++)

View file

@ -47,7 +47,7 @@ int main(int argc, char *argv[]) {
NULL
};
char **i, **r, *t;
char **i, **r, *t, **a, **b;
r = replace_env_argv((char**) line, (char**) env);
@ -96,5 +96,24 @@ int main(int argc, char *argv[]) {
printf("%s\n", t);
free(t);
a = strv_new("FOO=BAR", "WALDO=WALDO", "WALDO=", "PIEP", "SCHLUMPF=SMURF", NULL);
b = strv_new("FOO=KKK", "FOO=", "PIEP=", "SCHLUMPF=SMURFF", "NANANANA=YES", NULL);
r = strv_env_merge(2, a, b);
strv_free(a);
strv_free(b);
STRV_FOREACH(i, r)
printf("%s\n", *i);
printf("CLEANED UP:\n");
r = strv_env_clean(r);
STRV_FOREACH(i, r)
printf("%s\n", *i);
strv_free(r);
return 0;
}