1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 char *strv_find(char **l, const char *name) {
43 char *strv_find_prefix(char **l, const char *name) {
49 if (startswith(*i, name))
55 void strv_free(char **l) {
67 char **strv_copy(char **l) {
70 if (!(k = r = new(char*, strv_length(l)+1)))
75 if (!(*k = strdup(*l)))
82 for (k--; k >= r; k--)
90 unsigned strv_length(char **l) {
102 char **strv_new_ap(const char *x, va_list ap) {
105 unsigned n = 0, i = 0;
112 while (va_arg(aq, const char*))
117 if (!(a = new(char*, n+1)))
121 if (!(a[i] = strdup(x))) {
128 while ((s = va_arg(ap, const char*))) {
129 if (!(a[i] = strdup(s)))
151 char **strv_new(const char *x, ...) {
156 r = strv_new_ap(x, ap);
162 char **strv_merge(char **a, char **b) {
171 if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
174 for (k = r; *a; k++, a++)
175 if (!(*k = strdup(*a)))
178 if (!(*k = strdup(*b)))
185 for (k--; k >= r; k--)
193 char **strv_merge_concat(char **a, char **b, const char *suffix) {
196 /* Like strv_merge(), but appends suffix to all strings in b, before adding */
201 if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
206 for (; *a; k++, a++) {
212 for (; *b; k++, b++) {
213 *k = strappend(*b, suffix);
222 for (k--; k >= r; k--)
231 char **strv_split(const char *s, const char *separator) {
241 FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
244 if (!(r = new(char*, n+1)))
248 FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
249 if (!(r[i++] = strndup(w, l))) {
258 char **strv_split_quoted(const char *s) {
268 FOREACH_WORD_QUOTED(w, l, s, state)
271 if (!(r = new(char*, n+1)))
275 FOREACH_WORD_QUOTED(w, l, s, state)
276 if (!(r[i++] = cunescape_length(w, l))) {
285 char *strv_join(char **l, const char *separator) {
293 k = strlen(separator);
302 if (!(r = new(char, n+1)))
308 e = stpcpy(e, separator);
318 char **strv_append(char **l, const char *s) {
322 return strv_new(s, NULL);
327 if (!(r = new(char*, strv_length(l)+2)))
330 for (k = r; *l; k++, l++)
331 if (!(*k = strdup(*l)))
334 if (!(*(k++) = strdup(s)))
341 for (k--; k >= r; k--)
349 char **strv_uniq(char **l) {
352 /* Drops duplicate entries. The first identical string will be
353 * kept, the others dropped */
356 strv_remove(i+1, *i);
361 char **strv_remove(char **l, const char *s) {
369 /* Drops every occurrence of s in the string list, edits
372 for (f = t = l; *f; f++) {
386 static int env_append(char **r, char ***k, char **a) {
393 /* Add the entries of a to *k unless they already exist in *r
394 * in which case they are overridden instead. This assumes
395 * there is enough space in the r array. */
401 n = strcspn(*a, "=");
406 for (j = r; j < *k; j++)
407 if (strncmp(*j, *a, n) == 0)
415 if (!(*j = strdup(*a)))
422 char **strv_env_merge(unsigned n_lists, ...) {
428 /* Merges an arbitrary number of environment sets */
430 va_start(ap, n_lists);
431 for (i = 0; i < n_lists; i++) {
432 l = va_arg(ap, char**);
437 if (!(r = new(char*, n+1)))
442 va_start(ap, n_lists);
443 for (i = 0; i < n_lists; i++) {
444 l = va_arg(ap, char**);
445 if (env_append(r, &k, l) < 0)
457 for (k--; k >= r; k--)
465 static bool env_match(const char *t, const char *pattern) {
469 /* pattern a matches string a
474 * a= does not match a
475 * a=b does not match a=
476 * a=b does not match a
477 * a=b does not match a=c */
479 if (streq(t, pattern))
482 if (!strchr(pattern, '=')) {
483 size_t l = strlen(pattern);
485 return strncmp(t, pattern, l) == 0 && t[l] == '=';
491 char **strv_env_delete(char **x, unsigned n_lists, ...) {
496 /* Deletes every entry from x that is mentioned in the other
508 va_start(ap, n_lists);
509 for (v = 0; v < n_lists; v++) {
512 l = va_arg(ap, char**);
514 if (env_match(*k, *j))
539 char **strv_env_unset(char **l, const char *p) {
548 /* Drops every occurrence of the env var setting p in the
549 * string list. edits in-place. */
551 for (f = t = l; *f; f++) {
553 if (env_match(*f, p)) {
565 char **strv_env_set(char **x, const char *p) {
568 char* m[2] = { (char*) p, NULL };
570 /* Overrides the env var setting of p, returns a new copy */
572 if (!(r = new(char*, strv_length(x)+2)))
576 if (env_append(r, &k, x) < 0)
579 if (env_append(r, &k, m) < 0)
587 for (k--; k >= r; k--)
596 char *strv_env_get_with_length(char **l, const char *name, size_t k) {
602 if (strncmp(*i, name, k) == 0 &&
609 char *strv_env_get(char **l, const char *name) {
610 return strv_env_get_with_length(l, name, strlen(name));
613 char **strv_env_clean(char **l) {
616 for (r = ret = l; *l; l++) {
619 equal = strchr(*l, '=');
621 if (equal && equal[1] == 0) {
634 char **strv_parse_nulstr(const char *s, size_t l) {
636 unsigned c = 0, i = 0;
642 return strv_new(NULL, NULL);
644 for (p = s; p < s + l; p++)
651 if (!(v = new0(char*, c+1)))
658 e = memchr(p, 0, s + l - p);
660 if (!(v[i++] = strndup(p, e ? e - p : s + l - p))) {
676 bool strv_overlap(char **a, char **b) {