env-util: introduce strv_env_get_merged()

This commit is contained in:
Yu Watanabe
2024-10-31 11:02:35 +09:00
parent e4d477efc6
commit 7633001cdd
3 changed files with 40 additions and 0 deletions
+28
View File
@@ -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;
+1
View File
@@ -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);