]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
env-util: add strv_env_pairs_get helper
authorLuca Boccassi <luca.boccassi@microsoft.com>
Wed, 27 Jan 2021 12:51:17 +0000 (12:51 +0000)
committerLuca Boccassi <luca.boccassi@microsoft.com>
Wed, 17 Feb 2021 21:24:23 +0000 (21:24 +0000)
src/basic/env-util.c
src/basic/env-util.h
src/test/test-env-util.c

index 25c4b7c5a4198b49ba82ebc2687ea2b0ebdf7a77..137d6b1f3cbca650f20019d0ffd148be55ddd008 100644 (file)
@@ -455,6 +455,18 @@ char *strv_env_get(char **l, const char *name) {
         return strv_env_get_n(l, name, strlen(name), 0);
 }
 
+char *strv_env_pairs_get(char **l, const char *name) {
+        char **key, **value, *result = NULL;
+
+        assert(name);
+
+        STRV_FOREACH_PAIR(key, value, l)
+                if (streq(*key, name))
+                        result = *value;
+
+        return result;
+}
+
 char **strv_env_clean_with_callback(char **e, void (*invalid_callback)(const char *p, void *userdata), void *userdata) {
         char **p, **q;
         int k = 0;
index 347ea33e666f2384ff294663861f126e680a778b..79307a4a5fe62b7d72b3a0a8dd8d90cdbd2fcf2f 100644 (file)
@@ -50,6 +50,7 @@ int strv_env_assign(char ***l, const char *key, const char *value);
 
 char *strv_env_get_n(char **l, const char *name, size_t k, unsigned flags) _pure_;
 char *strv_env_get(char **x, const char *n) _pure_;
+char *strv_env_pairs_get(char **l, const char *name) _pure_;
 
 int getenv_bool(const char *p);
 int getenv_bool_secure(const char *p);
index fd121d975ef8660784d1bd78652df7cf23ccd709..77b8952f83c92cffb4f2edff38881e93d0621f74 100644 (file)
@@ -44,6 +44,17 @@ static void test_strv_env_get(void) {
         assert_se(streq(strv_env_get(l, "FOUR"), "4"));
 }
 
+static void test_strv_env_pairs_get(void) {
+        log_info("/* %s */", __func__);
+
+        char **l = STRV_MAKE("ONE_OR_TWO", "1", "THREE", "3", "ONE_OR_TWO", "2", "FOUR", "4", "FIVE", "5", "SIX", "FIVE", "SEVEN", "7");
+
+        assert_se(streq(strv_env_pairs_get(l, "ONE_OR_TWO"), "2"));
+        assert_se(streq(strv_env_pairs_get(l, "THREE"), "3"));
+        assert_se(streq(strv_env_pairs_get(l, "FOUR"), "4"));
+        assert_se(streq(strv_env_pairs_get(l, "FIVE"), "5"));
+}
+
 static void test_strv_env_unset(void) {
         log_info("/* %s */", __func__);
 
@@ -390,6 +401,7 @@ int main(int argc, char *argv[]) {
 
         test_strv_env_delete();
         test_strv_env_get();
+        test_strv_env_pairs_get();
         test_strv_env_unset();
         test_strv_env_merge();
         test_strv_env_replace_strdup();