summary refs log tree commit diff
path: root/src/systemd/src/basic/strv.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/systemd/src/basic/strv.c')
-rw-r--r--src/systemd/src/basic/strv.c62
1 files changed, 48 insertions, 14 deletions
diff --git a/src/systemd/src/basic/strv.c b/src/systemd/src/basic/strv.c
index dee5bbd7..08bcff6e 100644
--- a/src/systemd/src/basic/strv.c
+++ b/src/systemd/src/basic/strv.c
@@ -1,4 +1,3 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -216,7 +215,7 @@ int strv_extend_strv(char ***a, char **b, bool filter_duplicates) {
         p = strv_length(*a);
         q = strv_length(b);
 
-        t = reallocarray(*a, p + q + 1, sizeof(char *));
+        t = realloc(*a, sizeof(char*) * (p + q + 1));
         if (!t)
                 return -ENOMEM;
 
@@ -344,7 +343,8 @@ int strv_split_extract(char ***t, const char *s, const char *separators, Extract
                 if (!GREEDY_REALLOC(l, allocated, n + 2))
                         return -ENOMEM;
 
-                l[n++] = TAKE_PTR(word);
+                l[n++] = word;
+                word = NULL;
 
                 l[n] = NULL;
         }
@@ -355,7 +355,8 @@ int strv_split_extract(char ***t, const char *s, const char *separators, Extract
                         return -ENOMEM;
         }
 
-        *t = TAKE_PTR(l);
+        *t = l;
+        l = NULL;
 
         return (int) n;
 }
@@ -395,6 +396,42 @@ char *strv_join(char **l, const char *separator) {
         return r;
 }
 
+char *strv_join_quoted(char **l) {
+        char *buf = NULL;
+        char **s;
+        size_t allocated = 0, len = 0;
+
+        STRV_FOREACH(s, l) {
+                /* assuming here that escaped string cannot be more
+                 * than twice as long, and reserving space for the
+                 * separator and quotes.
+                 */
+                _cleanup_free_ char *esc = NULL;
+                size_t needed;
+
+                if (!GREEDY_REALLOC(buf, allocated,
+                                    len + strlen(*s) * 2 + 3))
+                        goto oom;
+
+                esc = cescape(*s);
+                if (!esc)
+                        goto oom;
+
+                needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
+                                  len > 0 ? " " : "", esc);
+                assert(needed < allocated - len);
+                len += needed;
+        }
+
+        if (!buf)
+                buf = malloc0(1);
+
+        return buf;
+
+ oom:
+        return mfree(buf);
+}
+
 int strv_push(char ***l, char *value) {
         char **c;
         unsigned n, m;
@@ -409,7 +446,7 @@ int strv_push(char ***l, char *value) {
         if (m < n)
                 return -ENOMEM;
 
-        c = reallocarray(*l, m, sizeof(char*));
+        c = realloc_multiply(*l, sizeof(char*), m);
         if (!c)
                 return -ENOMEM;
 
@@ -434,7 +471,7 @@ int strv_push_pair(char ***l, char *a, char *b) {
         if (m < n)
                 return -ENOMEM;
 
-        c = reallocarray(*l, m, sizeof(char*));
+        c = realloc_multiply(*l, sizeof(char*), m);
         if (!c)
                 return -ENOMEM;
 
@@ -448,7 +485,7 @@ int strv_push_pair(char ***l, char *a, char *b) {
         return 0;
 }
 
-int strv_insert(char ***l, unsigned position, char *value) {
+int strv_push_prepend(char ***l, char *value) {
         char **c;
         unsigned n, m, i;
 
@@ -456,7 +493,6 @@ int strv_insert(char ***l, unsigned position, char *value) {
                 return 0;
 
         n = strv_length(*l);
-        position = MIN(position, n);
 
         /* increase and check for overflow */
         m = n + 2;
@@ -467,12 +503,10 @@ int strv_insert(char ***l, unsigned position, char *value) {
         if (!c)
                 return -ENOMEM;
 
-        for (i = 0; i < position; i++)
-                c[i] = (*l)[i];
-        c[position] = value;
-        for (i = position; i < n; i++)
+        for (i = 0; i < n; i++)
                 c[i+1] = (*l)[i];
 
+        c[0] = value;
         c[n+1] = NULL;
 
         free(*l);
@@ -548,7 +582,7 @@ int strv_extend_front(char ***l, const char *value) {
         if (!v)
                 return -ENOMEM;
 
-        c = reallocarray(*l, m, sizeof(char*));
+        c = realloc_multiply(*l, sizeof(char*), m);
         if (!c) {
                 free(v);
                 return -ENOMEM;
@@ -863,7 +897,7 @@ int strv_extend_n(char ***l, const char *value, size_t n) {
 
         k = strv_length(*l);
 
-        nl = reallocarray(*l, k + n + 1, sizeof(char *));
+        nl = realloc(*l, sizeof(char*) * (k + n + 1));
         if (!nl)
                 return -ENOMEM;