summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2023-03-27 18:21:06 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2023-03-28 02:51:54 +0900
commit409759634e7fefc8120fca3f69b1a9aa0a68f396 (patch)
treeb08ee9780c040e7d21656d0dc6e18faeb8854786 /src/basic
parent23428bb19e49cf510c65e2896f1a7e4b12ca1dbc (diff)
downloadsystemd-409759634e7fefc8120fca3f69b1a9aa0a68f396.tar.gz
env-util: introduce strv_env_assign_many()
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/env-util.c42
-rw-r--r--src/basic/env-util.h2
2 files changed, 44 insertions, 0 deletions
diff --git a/src/basic/env-util.c b/src/basic/env-util.c
index 55ac11a512..41fad1d1b9 100644
--- a/src/basic/env-util.c
+++ b/src/basic/env-util.c
@@ -459,6 +459,48 @@ int strv_env_assign(char ***l, const char *key, const char *value) {
return strv_env_replace_consume(l, p);
}
+int _strv_env_assign_many(char ***l, ...) {
+ va_list ap;
+ int r;
+
+ assert(l);
+
+ va_start(ap, l);
+ for (;;) {
+ const char *key, *value;
+
+ key = va_arg(ap, const char *);
+ if (!key)
+ break;
+
+ if (!env_name_is_valid(key)) {
+ va_end(ap);
+ return -EINVAL;
+ }
+
+ value = va_arg(ap, const char *);
+ if (!value) {
+ strv_env_unset(*l, key);
+ continue;
+ }
+
+ char *p = strjoin(key, "=", value);
+ if (!p) {
+ va_end(ap);
+ return -ENOMEM;
+ }
+
+ r = strv_env_replace_consume(l, p);
+ if (r < 0) {
+ va_end(ap);
+ return r;
+ }
+ }
+ va_end(ap);
+
+ return 0;
+}
+
char *strv_env_get_n(char **l, const char *name, size_t k, unsigned flags) {
assert(name);
diff --git a/src/basic/env-util.h b/src/basic/env-util.h
index b927ac7a48..b0ff5a11d1 100644
--- a/src/basic/env-util.h
+++ b/src/basic/env-util.h
@@ -49,6 +49,8 @@ int strv_env_replace_consume(char ***l, char *p); /* In place ... */
int strv_env_replace_strdup(char ***l, const char *assignment);
int strv_env_replace_strdup_passthrough(char ***l, const char *assignment);
int strv_env_assign(char ***l, const char *key, const char *value);
+int _strv_env_assign_many(char ***l, ...) _sentinel_;
+#define strv_env_assign_many(l, ...) _strv_env_assign_many(l, __VA_ARGS__, NULL)
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_;