mirror of
https://github.com/systemd/systemd.git
synced 2024-12-23 21:35:11 +03:00
env-util: introduce strv_env_get_merged()
This commit is contained in:
parent
e4d477efc6
commit
7633001cdd
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
|
@ -581,4 +581,15 @@ TEST(getenv_path_list) {
|
||||
assert_se(unsetenv("TEST_GETENV_PATH_LIST") >= 0);
|
||||
}
|
||||
|
||||
TEST(strv_env_get_merged) {
|
||||
char **l = STRV_MAKE("ONE", "1", "TWO", "2", "THREE", "3", "FOUR", "4", "FIVE", "5"),
|
||||
**expected = STRV_MAKE("ONE=1", "TWO=2", "THREE=3", "FOUR=4", "FIVE=5");
|
||||
_cleanup_strv_free_ char **m = NULL;
|
||||
|
||||
ASSERT_OK(strv_env_get_merged(NULL, &m));
|
||||
ASSERT_NULL(m);
|
||||
ASSERT_OK(strv_env_get_merged(l, &m));
|
||||
ASSERT_TRUE(strv_equal(m, expected));
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_DEBUG);
|
||||
|
Loading…
Reference in New Issue
Block a user