chiark / gitweb /
path-util: in path_is_mount_point() fall back to the classic stat() test if fs does...
[elogind.git] / src / shared / strv.c
index bb309d9f92510feb1c622e20f91e6b9e6b46f15a..c8d856344c18987b7c2dc6360cc4417b0820fd8c 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/>.
 ***/
 
@@ -106,28 +106,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++;
@@ -169,25 +185,27 @@ char **strv_merge(char **a, char **b) {
         if (!b)
                 return strv_copy(a);
 
-        if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
+        r = new(char*, strv_length(a) + strv_length(b) + 1);
+        if (!r)
                 return NULL;
 
-        for (k = r; *a; k++, a++)
-                if (!(*k = strdup(*a)))
+        for (k = r; *a; k++, a++) {
+                *k = strdup(*a);
+                if (!*k)
                         goto fail;
-        for (; *b; k++, b++)
-                if (!(*k = strdup(*b)))
+        }
+
+        for (; *b; k++, b++) {
+                *k = strdup(*b);
+                if (!*k)
                         goto fail;
+        }
 
         *k = NULL;
         return r;
 
 fail:
-        for (k--; k >= r; k--)
-                free(*k);
-
-        free(r);
-
+        strv_free(r);
         return NULL;
 }
 
@@ -221,11 +239,7 @@ char **strv_merge_concat(char **a, char **b, const char *suffix) {
         return r;
 
 fail:
-        for (k--; k >= r; k--)
-                free(*k);
-
-        free(r);
-
+        strv_free(r);
         return NULL;
 
 }
@@ -386,6 +400,31 @@ char **strv_remove(char **l, const char *s) {
         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)) {
+                        free(*f);
+                        continue;
+                }
+
+                *(t++) = *f;
+        }
+
+        *t = NULL;
+        return l;
+}
+
 static int env_append(char **r, char ***k, char **a) {
         assert(r);
         assert(k);