From a6ff950e71ea665fff99740f7b3e0137d451a79e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 6 Jan 2011 20:38:02 +0100 Subject: [PATCH] execute: drop empty assignments from env blocks on execution but keep them around otherwise to make them visible --- src/execute.c | 2 ++ src/load-fragment.c | 2 +- src/strv.c | 23 ++++++++++++++++++++++- src/strv.h | 2 ++ src/test-env-replace.c | 21 ++++++++++++++++++++- 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/execute.c b/src/execute.c index 05abd5aaa..8f486e200 100644 --- a/src/execute.c +++ b/src/execute.c @@ -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; diff --git a/src/load-fragment.c b/src/load-fragment.c index 334dd6814..261180d15 100644 --- a/src/load-fragment.c +++ b/src/load-fragment.c @@ -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) { diff --git a/src/strv.c b/src/strv.c index d9aef9822..d1c7b2c32 100644 --- a/src/strv.c +++ b/src/strv.c @@ -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; +} diff --git a/src/strv.h b/src/strv.h index 1103e194c..5af84ee41 100644 --- a/src/strv.h +++ b/src/strv.h @@ -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)++) diff --git a/src/test-env-replace.c b/src/test-env-replace.c index 37dd7ff59..4188c67dd 100644 --- a/src/test-env-replace.c +++ b/src/test-env-replace.c @@ -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; } -- 2.30.2