diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2024-10-25 19:01:19 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2024-10-31 03:02:35 +0100 |
commit | 7633001cddc2ddd906f39826be35cff20a327543 (patch) | |
tree | e2e22f38d29178020f2cc86b17c0068be08ee4ec /src/basic | |
parent | env-util: replace 'char **' with 'char**' (diff) | |
download | systemd-7633001cddc2ddd906f39826be35cff20a327543.tar.xz systemd-7633001cddc2ddd906f39826be35cff20a327543.zip |
env-util: introduce strv_env_get_merged()
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/env-util.c | 28 | ||||
-rw-r--r-- | src/basic/env-util.h | 1 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/basic/env-util.c b/src/basic/env-util.c index 3b4b570b9c..99ce1a1842 100644 --- a/src/basic/env-util.c +++ b/src/basic/env-util.c @@ -564,6 +564,34 @@ char* strv_env_pairs_get(char **l, const char *name) { return result; } +int strv_env_get_merged(char **l, char ***ret) { + _cleanup_strv_free_ char **v = NULL; + size_t n = 0; + int r; + + assert(ret); + + /* This converts a strv with pairs of environment variable name + value into a strv of name and + * value concatenated with a "=" separator. E.g. + * input : { "NAME", "value", "FOO", "var" } + * output : { "NAME=value", "FOO=var" } */ + + STRV_FOREACH_PAIR(key, value, l) { + char *s; + + s = strjoin(*key, "=", *value); + if (!s) + return -ENOMEM; + + r = strv_consume_with_size(&v, &n, s); + if (r < 0) + return r; + } + + *ret = TAKE_PTR(v); + return 0; +} + char** strv_env_clean_with_callback(char **e, void (*invalid_callback)(const char *p, void *userdata), void *userdata) { int k = 0; diff --git a/src/basic/env-util.h b/src/basic/env-util.h index f5ceba453a..203ed65bd1 100644 --- a/src/basic/env-util.h +++ b/src/basic/env-util.h @@ -60,6 +60,7 @@ static inline char* strv_env_get(char * const *x, const char *n) { } char* strv_env_pairs_get(char **l, const char *name) _pure_; +int strv_env_get_merged(char **l, char ***ret); int getenv_bool(const char *p); int secure_getenv_bool(const char *p); |