chiark / gitweb /
conf-parser: warn when we open configuration files with weird access bits
[elogind.git] / src / shared / strv.c
index f61680d4763602e843ee02350f0016d433690e4d..13deba7be7780a7978d52d22b5e998a427af15f5 100644 (file)
@@ -6,16 +6,16 @@
   Copyright 2010 Lennart Poettering
 
   systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
   (at your option) any later version.
 
   systemd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  General Public License for more details.
+  Lesser General Public License for more details.
 
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
@@ -64,31 +64,27 @@ void strv_free(char **l) {
         free(l);
 }
 
-char **strv_copy(char **l) {
+char **strv_copy(char * const *l) {
         char **r, **k;
 
-        k = r = new(char*, strv_length(l)+1);
-        if (!k)
+        k = r = new(char*, strv_length(l) + 1);
+        if (!r)
                 return NULL;
 
         if (l)
-                for (; *l; k++, l++)
-                        if (!(*k = strdup(*l)))
-                                goto fail;
+                for (; *l; k++, l++) {
+                        *k = strdup(*l);
+                        if (!*k) {
+                                strv_free(r);
+                                return NULL;
+                        }
+                }
 
         *k = NULL;
         return r;
-
-fail:
-        for (k--; k >= r; k--)
-                free(*k);
-
-        free(r);
-
-        return NULL;
 }
 
-unsigned strv_length(char **l) {
+unsigned strv_length(char * const *l) {
         unsigned n = 0;
 
         if (!l)
@@ -106,28 +102,44 @@ char **strv_new_ap(const char *x, va_list ap) {
         unsigned n = 0, i = 0;
         va_list aq;
 
+        /* As a special trick we ignore all listed strings that equal
+         * (const char*) -1. This is supposed to be used with the
+         * STRV_IFNOTNULL() macro to include possibly NULL strings in
+         * the string list. */
+
         if (x) {
-                n = 1;
+                n = x == (const char*) -1 ? 0 : 1;
 
                 va_copy(aq, ap);
-                while (va_arg(aq, const char*))
+                while ((s = va_arg(aq, const char*))) {
+                        if (s == (const char*) -1)
+                                continue;
+
                         n++;
+                }
+
                 va_end(aq);
         }
 
-        if (!(a = new(char*, n+1)))
+        a = new(char*, n+1);
+        if (!a)
                 return NULL;
 
         if (x) {
-                if (!(a[i] = strdup(x))) {
-                        free(a);
-                        return NULL;
+                if (x != (const char*) -1) {
+                        a[i] = strdup(x);
+                        if (!a[i])
+                                goto fail;
+                        i++;
                 }
 
-                i++;
-
                 while ((s = va_arg(ap, const char*))) {
-                        if (!(a[i] = strdup(s)))
+
+                        if (s == (const char*) -1)
+                                continue;
+
+                        a[i] = strdup(s);
+                        if (!a[i])
                                 goto fail;
 
                         i++;
@@ -139,13 +151,7 @@ char **strv_new_ap(const char *x, va_list ap) {
         return a;
 
 fail:
-
-        for (; i > 0; i--)
-                if (a[i-1])
-                        free(a[i-1]);
-
-        free(a);
-
+        strv_free(a);
         return NULL;
 }
 
@@ -160,74 +166,38 @@ char **strv_new(const char *x, ...) {
         return r;
 }
 
-char **strv_merge(char **a, char **b) {
-        char **r, **k;
-
-        if (!a)
-                return strv_copy(b);
-
-        if (!b)
-                return strv_copy(a);
-
-        if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
-                return NULL;
-
-        for (k = r; *a; k++, a++)
-                if (!(*k = strdup(*a)))
-                        goto fail;
-        for (; *b; k++, b++)
-                if (!(*k = strdup(*b)))
-                        goto fail;
-
-        *k = NULL;
-        return r;
-
-fail:
-        for (k--; k >= r; k--)
-                free(*k);
+int strv_extend_strv(char ***a, char **b) {
+        int r;
+        char **s;
 
-        free(r);
+        STRV_FOREACH(s, b) {
+                r = strv_extend(a, *s);
+                if (r < 0)
+                        return r;
+        }
 
-        return NULL;
+        return 0;
 }
 
-char **strv_merge_concat(char **a, char **b, const char *suffix) {
-        char **r, **k;
-
-        /* Like strv_merge(), but appends suffix to all strings in b, before adding */
+int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
+        int r;
+        char **s;
 
-        if (!b)
-                return strv_copy(a);
+        STRV_FOREACH(s, b) {
+                char *v;
 
-        r = new(char*, strv_length(a) + strv_length(b) + 1);
-        if (!r)
-                return NULL;
+                v = strappend(*s, suffix);
+                if (!v)
+                        return -ENOMEM;
 
-        k = r;
-        if (a)
-                for (; *a; k++, a++) {
-                        *k = strdup(*a);
-                        if (!*k)
-                                goto fail;
+                r = strv_push(a, v);
+                if (r < 0) {
+                        free(v);
+                        return r;
                 }
-
-        for (; *b; k++, b++) {
-                *k = strappend(*b, suffix);
-                if (!*k)
-                        goto fail;
         }
 
-        *k = NULL;
-        return r;
-
-fail:
-        for (k--; k >= r; k--)
-                free(*k);
-
-        free(r);
-
-        return NULL;
-
+        return 0;
 }
 
 char **strv_split(const char *s, const char *separator) {
@@ -243,16 +213,21 @@ char **strv_split(const char *s, const char *separator) {
         FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
                 n++;
 
-        if (!(r = new(char*, n+1)))
+        r = new(char*, n+1);
+        if (!r)
                 return NULL;
 
         i = 0;
-        FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
-                if (!(r[i++] = strndup(w, l))) {
+        FOREACH_WORD_SEPARATOR(w, l, s, separator, state) {
+                r[i] = strndup(w, l);
+                if (!r[i]) {
                         strv_free(r);
                         return NULL;
                 }
 
+                i++;
+        }
+
         r[i] = NULL;
         return r;
 }
@@ -270,20 +245,49 @@ char **strv_split_quoted(const char *s) {
         FOREACH_WORD_QUOTED(w, l, s, state)
                 n++;
 
-        if (!(r = new(char*, n+1)))
+        r = new(char*, n+1);
+        if (!r)
                 return NULL;
 
         i = 0;
-        FOREACH_WORD_QUOTED(w, l, s, state)
-                if (!(r[i++] = cunescape_length(w, l))) {
+        FOREACH_WORD_QUOTED(w, l, s, state) {
+                r[i] = cunescape_length(w, l);
+                if (!r[i]) {
                         strv_free(r);
                         return NULL;
                 }
+                i++;
+        }
 
         r[i] = NULL;
         return r;
 }
 
+char **strv_split_newlines(const char *s) {
+        char **l;
+        unsigned n;
+
+        assert(s);
+
+        /* Special version of strv_split() that splits on newlines and
+         * suppresses an empty string at the end */
+
+        l = strv_split(s, NEWLINE);
+        if (!l)
+                return NULL;
+
+        n = strv_length(l);
+        if (n <= 0)
+                return l;
+
+        if (isempty(l[n-1])) {
+                free(l[n-1]);
+                l[n-1] = NULL;
+        }
+
+        return l;
+}
+
 char *strv_join(char **l, const char *separator) {
         char *r, *e;
         char **s;
@@ -301,7 +305,8 @@ char *strv_join(char **l, const char *separator) {
                 n += strlen(*s);
         }
 
-        if (!(r = new(char, n+1)))
+        r = new(char, n+1);
+        if (!r)
                 return NULL;
 
         e = r;
@@ -317,36 +322,78 @@ char *strv_join(char **l, const char *separator) {
         return r;
 }
 
-char **strv_append(char **l, const char *s) {
-        char **r, **k;
+char *strv_join_quoted(char **l) {
+        char *buf = NULL;
+        char **s;
+        size_t allocated = 0, len = 0;
 
-        if (!l)
-                return strv_new(s, NULL);
+        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 (!s)
-                return strv_copy(l);
+        if (!buf)
+                buf = malloc0(1);
 
-        r = new(char*, strv_length(l)+2);
-        if (!r)
-                return NULL;
+        return buf;
 
-        for (k = r; *l; k++, l++)
-                if (!(*k = strdup(*l)))
-                        goto fail;
+ oom:
+        free(buf);
+        return NULL;
+}
 
-        if (!(*(k++) = strdup(s)))
-                goto fail;
+int strv_push(char ***l, char *value) {
+        char **c;
+        unsigned n;
 
-        *k = NULL;
-        return r;
+        if (!value)
+                return 0;
 
-fail:
-        for (k--; k >= r; k--)
-                free(*k);
+        n = strv_length(*l);
+        c = realloc(*l, sizeof(char*) * (n + 2));
+        if (!c)
+                return -ENOMEM;
 
-        free(r);
+        c[n] = value;
+        c[n+1] = NULL;
 
-        return NULL;
+        *l = c;
+        return 0;
+}
+
+int strv_extend(char ***l, const char *value) {
+        char *v;
+        int r;
+
+        if (!value)
+                return 0;
+
+        v = strdup(value);
+        if (!v)
+                return -ENOMEM;
+
+        r = strv_push(l, v);
+        if (r < 0)
+                free(v);
+
+        return r;
 }
 
 char **strv_uniq(char **l) {
@@ -372,293 +419,16 @@ char **strv_remove(char **l, const char *s) {
         /* Drops every occurrence of s in the string list, edits
          * in-place. */
 
-        for (f = t = l; *f; f++) {
-
-                if (streq(*f, s)) {
-                        free(*f);
-                        continue;
-                }
-
-                *(t++) = *f;
-        }
-
-        *t = NULL;
-        return l;
-}
-
-char **strv_remove_prefix(char **l, const char *s) {
-        char **f, **t;
-
-        if (!l)
-                return NULL;
-
-        assert(s);
-
-        /* Drops every occurrence of a string prefixed with s in the
-         * string list, edits in-place. */
-
-        for (f = t = l; *f; f++) {
-
-                if (startswith(*f, s)) {
+        for (f = t = l; *f; f++)
+                if (streq(*f, s))
                         free(*f);
-                        continue;
-                }
-
-                *(t++) = *f;
-        }
-
-        *t = NULL;
-        return l;
-}
-
-static int env_append(char **r, char ***k, char **a) {
-        assert(r);
-        assert(k);
-
-        if (!a)
-                return 0;
-
-        /* Add the entries of a to *k unless they already exist in *r
-         * in which case they are overridden instead. This assumes
-         * there is enough space in the r array. */
-
-        for (; *a; a++) {
-                char **j;
-                size_t n;
-
-                n = strcspn(*a, "=");
-
-                if ((*a)[n] == '=')
-                        n++;
-
-                for (j = r; j < *k; j++)
-                        if (strncmp(*j, *a, n) == 0)
-                                break;
-
-                if (j >= *k)
-                        (*k)++;
                 else
-                        free(*j);
-
-                if (!(*j = strdup(*a)))
-                        return -ENOMEM;
-        }
-
-        return 0;
-}
-
-char **strv_env_merge(unsigned n_lists, ...) {
-        size_t n = 0;
-        char **l, **k, **r;
-        va_list ap;
-        unsigned i;
-
-        /* Merges an arbitrary number of environment sets */
-
-        va_start(ap, n_lists);
-        for (i = 0; i < n_lists; i++) {
-                l = va_arg(ap, char**);
-                n += strv_length(l);
-        }
-        va_end(ap);
-
-        if (!(r = new(char*, n+1)))
-                return NULL;
-
-        k = r;
-
-        va_start(ap, n_lists);
-        for (i = 0; i < n_lists; i++) {
-                l = va_arg(ap, char**);
-                if (env_append(r, &k, l) < 0)
-                        goto fail;
-        }
-        va_end(ap);
-
-        *k = NULL;
-
-        return r;
-
-fail:
-        va_end(ap);
-
-        for (k--; k >= r; k--)
-                free(*k);
-
-        free(r);
-
-        return NULL;
-}
-
-static bool env_match(const char *t, const char *pattern) {
-        assert(t);
-        assert(pattern);
-
-        /* pattern a matches string a
-         *         a matches a=
-         *         a matches a=b
-         *         a= matches a=
-         *         a=b matches a=b
-         *         a= does not match a
-         *         a=b does not match a=
-         *         a=b does not match a
-         *         a=b does not match a=c */
-
-        if (streq(t, pattern))
-                return true;
-
-        if (!strchr(pattern, '=')) {
-                size_t l = strlen(pattern);
-
-                return strncmp(t, pattern, l) == 0 && t[l] == '=';
-        }
-
-        return false;
-}
-
-char **strv_env_delete(char **x, unsigned n_lists, ...) {
-        size_t n, i = 0;
-        char **k, **r;
-        va_list ap;
-
-        /* Deletes every entry from x that is mentioned in the other
-         * string lists */
-
-        n = strv_length(x);
-
-        r = new(char*, n+1);
-        if (!r)
-                return NULL;
-
-        STRV_FOREACH(k, x) {
-                unsigned v;
-
-                va_start(ap, n_lists);
-                for (v = 0; v < n_lists; v++) {
-                        char **l, **j;
-
-                        l = va_arg(ap, char**);
-                        STRV_FOREACH(j, l)
-                                if (env_match(*k, *j))
-                                        goto skip;
-                }
-                va_end(ap);
-
-                r[i] = strdup(*k);
-                if (!r[i]) {
-                        strv_free(r);
-                        return NULL;
-                }
-
-                i++;
-                continue;
-
-        skip:
-                va_end(ap);
-        }
-
-        r[i] = NULL;
-
-        assert(i <= n);
-
-        return r;
-}
-
-char **strv_env_unset(char **l, const char *p) {
-
-        char **f, **t;
-
-        if (!l)
-                return NULL;
-
-        assert(p);
-
-        /* Drops every occurrence of the env var setting p in the
-         * string list. edits in-place. */
-
-        for (f = t = l; *f; f++) {
-
-                if (env_match(*f, p)) {
-                        free(*f);
-                        continue;
-                }
-
-                *(t++) = *f;
-        }
+                        *(t++) = *f;
 
         *t = NULL;
         return l;
 }
 
-char **strv_env_set(char **x, const char *p) {
-
-        char **k, **r;
-        char* m[2] = { (char*) p, NULL };
-
-        /* Overrides the env var setting of p, returns a new copy */
-
-        if (!(r = new(char*, strv_length(x)+2)))
-                return NULL;
-
-        k = r;
-        if (env_append(r, &k, x) < 0)
-                goto fail;
-
-        if (env_append(r, &k, m) < 0)
-                goto fail;
-
-        *k = NULL;
-
-        return r;
-
-fail:
-        for (k--; k >= r; k--)
-                free(*k);
-
-        free(r);
-
-        return NULL;
-
-}
-
-char *strv_env_get_with_length(char **l, const char *name, size_t k) {
-        char **i;
-
-        assert(name);
-
-        STRV_FOREACH(i, l)
-                if (strncmp(*i, name, k) == 0 &&
-                    (*i)[k] == '=')
-                        return *i + k + 1;
-
-        return NULL;
-}
-
-char *strv_env_get(char **l, const char *name) {
-        return strv_env_get_with_length(l, name, strlen(name));
-}
-
-char **strv_env_clean(char **l) {
-        char **r, **ret;
-
-        for (r = ret = l; *l; l++) {
-                const char *equal;
-
-                equal = strchr(*l, '=');
-
-                if (equal && equal[1] == 0) {
-                        free(*l);
-                        continue;
-                }
-
-                *(r++) = *l;
-        }
-
-        *r = NULL;
-
-        return ret;
-}
-
 char **strv_parse_nulstr(const char *s, size_t l) {
         const char *p;
         unsigned c = 0, i = 0;
@@ -667,7 +437,7 @@ char **strv_parse_nulstr(const char *s, size_t l) {
         assert(s || l <= 0);
 
         if (l <= 0)
-                return strv_new(NULL, NULL);
+                return new0(char*, 1);
 
         for (p = s; p < s + l; p++)
                 if (*p == 0)
@@ -676,7 +446,8 @@ char **strv_parse_nulstr(const char *s, size_t l) {
         if (s[l-1] != 0)
                 c++;
 
-        if (!(v = new0(char*, c+1)))
+        v = new0(char*, c+1);
+        if (!v)
                 return NULL;
 
         p = s;
@@ -685,11 +456,14 @@ char **strv_parse_nulstr(const char *s, size_t l) {
 
                 e = memchr(p, 0, s + l - p);
 
-                if (!(v[i++] = strndup(p, e ? e - p : s + l - p))) {
+                v[i] = strndup(p, e ? e - p : s + l - p);
+                if (!v[i]) {
                         strv_free(v);
                         return NULL;
                 }
 
+                i++;
+
                 if (!e)
                         break;
 
@@ -701,15 +475,50 @@ char **strv_parse_nulstr(const char *s, size_t l) {
         return v;
 }
 
-bool strv_overlap(char **a, char **b) {
-        char **i, **j;
+char **strv_split_nulstr(const char *s) {
+        const char *i;
+        char **r = NULL;
 
-        STRV_FOREACH(i, a) {
-                STRV_FOREACH(j, b) {
-                        if (streq(*i, *j))
-                                return true;
+        NULSTR_FOREACH(i, s)
+                if (strv_extend(&r, i) < 0) {
+                        strv_free(r);
+                        return NULL;
                 }
-        }
+
+        if (!r)
+                return strv_new(NULL, NULL);
+
+        return r;
+}
+
+bool strv_overlap(char **a, char **b) {
+        char **i;
+
+        STRV_FOREACH(i, a)
+                if (strv_contains(b, *i))
+                        return true;
 
         return false;
 }
+
+static int str_compare(const void *_a, const void *_b) {
+        const char **a = (const char**) _a, **b = (const char**) _b;
+
+        return strcmp(*a, *b);
+}
+
+char **strv_sort(char **l) {
+
+        if (strv_isempty(l))
+                return l;
+
+        qsort(l, strv_length(l), sizeof(char*), str_compare);
+        return l;
+}
+
+void strv_print(char **l) {
+        char **s;
+
+        STRV_FOREACH(s, l)
+                puts(*s);
+}