chiark / gitweb /
strv: add calls to add two entries to an strv at once
[elogind.git] / src / shared / strv.c
index 0df978d23b911f53be7e7c597e01c8b26c28a2a6..fdb658c0a33696a0e5d644f1c2636748ce0c3a5e 100644 (file)
@@ -248,40 +248,6 @@ char **strv_split(const char *s, const char *separator) {
         return r;
 }
 
-int strv_split_quoted(char ***t, const char *s) {
-        const char *word, *state;
-        size_t l;
-        unsigned n, i;
-        char **r;
-
-        assert(s);
-
-        n = 0;
-        FOREACH_WORD_QUOTED(word, l, s, state)
-                n++;
-        if (!isempty(state))
-                /* bad syntax */
-                return -EINVAL;
-
-        r = new(char*, n+1);
-        if (!r)
-                return -ENOMEM;
-
-        i = 0;
-        FOREACH_WORD_QUOTED(word, l, s, state) {
-                r[i] = cunescape_length(word, l);
-                if (!r[i]) {
-                        strv_free(r);
-                        return -ENOMEM;
-                }
-                i++;
-        }
-
-        r[i] = NULL;
-        *t = r;
-        return 0;
-}
-
 char **strv_split_newlines(const char *s) {
         char **l;
         unsigned n;
@@ -307,6 +273,41 @@ char **strv_split_newlines(const char *s) {
         return l;
 }
 
+int strv_split_quoted(char ***t, const char *s, bool relax) {
+        size_t n = 0, allocated = 0;
+        _cleanup_strv_free_ char **l = NULL;
+        int r;
+
+        assert(t);
+        assert(s);
+
+        for (;;) {
+                _cleanup_free_ char *word = NULL;
+
+                r = unquote_first_word(&s, &word, relax);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        break;
+
+                if (!GREEDY_REALLOC(l, allocated, n + 2))
+                        return -ENOMEM;
+
+                l[n++] = word;
+                word = NULL;
+
+                l[n] = NULL;
+        }
+
+        if (!l)
+                l = new0(char*, 1);
+
+        *t = l;
+        l = NULL;
+
+        return 0;
+}
+
 char *strv_join(char **l, const char *separator) {
         char *r, *e;
         char **s;
@@ -380,13 +381,19 @@ char *strv_join_quoted(char **l) {
 
 int strv_push(char ***l, char *value) {
         char **c;
-        unsigned n;
+        unsigned n, m;
 
         if (!value)
                 return 0;
 
         n = strv_length(*l);
-        c = realloc(*l, sizeof(char*) * (n + 2));
+
+        /* Increase and check for overflow */
+        m = n + 2;
+        if (m < n)
+                return -ENOMEM;
+
+        c = realloc_multiply(*l, sizeof(char*), m);
         if (!c)
                 return -ENOMEM;
 
@@ -397,15 +404,49 @@ int strv_push(char ***l, char *value) {
         return 0;
 }
 
+int strv_push_pair(char ***l, char *a, char *b) {
+        char **c;
+        unsigned n, m;
+
+        if (!a && !b)
+                return 0;
+
+        n = strv_length(*l);
+
+        /* increase and check for overflow */
+        m = n + !!a + !!b + 1;
+        if (m < n)
+                return -ENOMEM;
+
+        c = realloc_multiply(*l, sizeof(char*), m);
+        if (!c)
+                return -ENOMEM;
+
+        if (a)
+                c[n++] = a;
+        if (b)
+                c[n++] = b;
+        c[n] = NULL;
+
+        *l = c;
+        return 0;
+}
+
 int strv_push_prepend(char ***l, char *value) {
         char **c;
-        unsigned n, i;
+        unsigned n, m, i;
 
         if (!value)
                 return 0;
 
         n = strv_length(*l);
-        c = new(char*, n + 2);
+
+        /* increase and check for overflow */
+        m = n + 2;
+        if (m < n)
+                return -ENOMEM;
+
+        c = new(char*, m);
         if (!c)
                 return -ENOMEM;
 
@@ -431,6 +472,18 @@ int strv_consume(char ***l, char *value) {
         return r;
 }
 
+int strv_consume_pair(char ***l, char *a, char *b) {
+        int r;
+
+        r = strv_push_pair(l, a, b);
+        if (r < 0) {
+                free(a);
+                free(b);
+        }
+
+        return r;
+}
+
 int strv_consume_prepend(char ***l, char *value) {
         int r;
 
@@ -574,6 +627,17 @@ char **strv_sort(char **l) {
         return l;
 }
 
+bool strv_equal(char **a, char **b) {
+        if (!a || !b)
+                return a == b;
+
+        for ( ; *a || *b; ++a, ++b)
+                if (!streq_ptr(*a, *b))
+                        return false;
+
+        return true;
+}
+
 void strv_print(char **l) {
         char **s;