summaryrefslogtreecommitdiff
path: root/src/basic/env-util.c
diff options
context:
space:
mode:
authorRay Strode <rstrode@redhat.com>2016-08-04 12:00:00 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-02-20 18:49:14 -0500
commit37f3ffca273e5238794019caede7b7cd33a5de3a (patch)
treee33c5556d5cb2dd89064d8c3344b904fd8853f47 /src/basic/env-util.c
parentd8ad241f54b8c4ac76aafd960d89b47b0ed87fb6 (diff)
basic: add new merge_env_file function
merge_env_file is a new function, that's like load_env_file, but takes a pre-existing environment as an input argument. New environment entries are merged. Variable expansion is performed. Falling back to the process environment is supported (when a flag is set). Alternatively this could be implemented as passing an additional fallback environment array, but later on we're adding another flag to allow braceless expansion, and the two flags can be combined in one arg, so there's less stuff to pass around.
Diffstat (limited to 'src/basic/env-util.c')
-rw-r--r--src/basic/env-util.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/basic/env-util.c b/src/basic/env-util.c
index 86ac07e1b6..99a130008b 100644
--- a/src/basic/env-util.c
+++ b/src/basic/env-util.c
@@ -454,7 +454,7 @@ fail:
return NULL;
}
-char *strv_env_get_n(char **l, const char *name, size_t k) {
+char *strv_env_get_n(char **l, const char *name, size_t k, unsigned flags) {
char **i;
assert(name);
@@ -467,13 +467,20 @@ char *strv_env_get_n(char **l, const char *name, size_t k) {
(*i)[k] == '=')
return *i + k + 1;
+ if (flags & REPLACE_ENV_USE_ENVIRONMENT) {
+ const char *t;
+
+ t = strndupa(name, k);
+ return getenv(t);
+ };
+
return NULL;
}
char *strv_env_get(char **l, const char *name) {
assert(name);
- return strv_env_get_n(l, name, strlen(name));
+ return strv_env_get_n(l, name, strlen(name), 0);
}
char **strv_env_clean_with_callback(char **e, void (*invalid_callback)(const char *p, void *userdata), void *userdata) {
@@ -512,7 +519,7 @@ char **strv_env_clean_with_callback(char **e, void (*invalid_callback)(const cha
return e;
}
-char *replace_env(const char *format, char **env) {
+char *replace_env(const char *format, char **env, unsigned flags) {
enum {
WORD,
CURLY,
@@ -563,7 +570,7 @@ char *replace_env(const char *format, char **env) {
if (*e == '}') {
const char *t;
- t = strv_env_get_n(env, word+2, e-word-2);
+ t = strv_env_get_n(env, word+2, e-word-2, flags);
k = strappend(r, t);
if (!k)
@@ -643,7 +650,7 @@ char **replace_env_argv(char **argv, char **env) {
}
/* If ${FOO} appears as part of a word, replace it by the variable as-is */
- ret[k] = replace_env(*i, env);
+ ret[k] = replace_env(*i, env, 0);
if (!ret[k]) {
strv_free(ret);
return NULL;