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/>.
***/
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++;
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;
}
return r;
fail:
- for (k--; k >= r; k--)
- free(*k);
-
- free(r);
-
+ strv_free(r);
return 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)) {
+ free(*f);
+ continue;
+ }
+
+ *(t++) = *f;
+ }
+
+ *t = NULL;
+ return l;
+}
+
static int env_append(char **r, char ***k, char **a) {
assert(r);
assert(k);