summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Villemoes <rasmus.villemoes@prevas.dk>2020-10-30 10:54:15 +0100
committerRasmus Villemoes <rasmus.villemoes@prevas.dk>2020-11-03 14:26:08 +0100
commit3d2b1fa473691cf9b7cbb407a5c75df4bf8b2ae3 (patch)
treecf74e690c2677b1e603ad0051fea890c8e91161a
parentbd8e699c27fbc52dd7a2c232722ba59222d3d47d (diff)
downloadsystemd-3d2b1fa473691cf9b7cbb407a5c75df4bf8b2ae3.tar.gz
strv.c: simplify strv_new_ap
Instead of duplicating the code for x and the varargs, handle them all the same way by using for loops.
-rw-r--r--src/basic/strv.c42
1 files changed, 13 insertions, 29 deletions
diff --git a/src/basic/strv.c b/src/basic/strv.c
index b2b6de388a..7a69107462 100644
--- a/src/basic/strv.c
+++ b/src/basic/strv.c
@@ -123,7 +123,6 @@ size_t strv_length(char * const *l) {
}
char **strv_new_ap(const char *x, va_list ap) {
- const char *s;
_cleanup_strv_free_ char **a = NULL;
size_t n = 0, i = 0;
va_list aq;
@@ -133,43 +132,28 @@ char **strv_new_ap(const char *x, va_list ap) {
* STRV_IFNOTNULL() macro to include possibly NULL strings in
* the string list. */
- if (x) {
- n = x == STRV_IGNORE ? 0 : 1;
-
- va_copy(aq, ap);
- while ((s = va_arg(aq, const char*))) {
- if (s == STRV_IGNORE)
- continue;
-
- n++;
- }
+ va_copy(aq, ap);
+ for (const char *s = x; s; s = va_arg(aq, const char*)) {
+ if (s == STRV_IGNORE)
+ continue;
- va_end(aq);
+ n++;
}
+ va_end(aq);
a = new(char*, n+1);
if (!a)
return NULL;
- if (x) {
- if (x != STRV_IGNORE) {
- a[i] = strdup(x);
- if (!a[i])
- return NULL;
- i++;
- }
-
- while ((s = va_arg(ap, const char*))) {
-
- if (s == STRV_IGNORE)
- continue;
+ for (const char *s = x; s; s = va_arg(ap, const char*)) {
+ if (s == STRV_IGNORE)
+ continue;
- a[i] = strdup(s);
- if (!a[i])
- return NULL;
+ a[i] = strdup(s);
+ if (!a[i])
+ return NULL;
- i++;
- }
+ i++;
}
a[i] = NULL;