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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser 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 char *strv_find_startswith(char **l, const char *name) {
60 /* Like strv_find_prefix, but actually returns only the
61 * suffix, not the whole item */
64 e = startswith(*i, name);
72 void strv_clear(char **l) {
84 void strv_free(char **l) {
89 char **strv_copy(char * const *l) {
92 k = r = new(char*, strv_length(l) + 1);
97 for (; *l; k++, l++) {
109 unsigned strv_length(char * const *l) {
121 char **strv_new_ap(const char *x, va_list ap) {
124 unsigned n = 0, i = 0;
127 /* As a special trick we ignore all listed strings that equal
128 * (const char*) -1. This is supposed to be used with the
129 * STRV_IFNOTNULL() macro to include possibly NULL strings in
130 * the string list. */
133 n = x == (const char*) -1 ? 0 : 1;
136 while ((s = va_arg(aq, const char*))) {
137 if (s == (const char*) -1)
151 if (x != (const char*) -1) {
158 while ((s = va_arg(ap, const char*))) {
160 if (s == (const char*) -1)
180 char **strv_new(const char *x, ...) {
185 r = strv_new_ap(x, ap);
191 int strv_extend_strv(char ***a, char **b) {
196 r = strv_extend(a, *s);
204 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
211 v = strappend(*s, suffix);
225 char **strv_split(const char *s, const char *separator) {
226 const char *word, *state;
234 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
242 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
243 r[i] = strndup(word, l);
256 char **strv_split_newlines(const char *s) {
262 /* Special version of strv_split() that splits on newlines and
263 * suppresses an empty string at the end */
265 l = strv_split(s, NEWLINE);
273 if (isempty(l[n-1])) {
281 int strv_split_quoted(char ***t, const char *s, bool relax) {
282 size_t n = 0, allocated = 0;
283 _cleanup_strv_free_ char **l = NULL;
290 _cleanup_free_ char *word = NULL;
292 r = unquote_first_word(&s, &word, relax);
298 if (!GREEDY_REALLOC(l, allocated, n + 2))
316 char *strv_join(char **l, const char *separator) {
324 k = strlen(separator);
340 e = stpcpy(e, separator);
350 char *strv_join_quoted(char **l) {
353 size_t allocated = 0, len = 0;
356 /* assuming here that escaped string cannot be more
357 * than twice as long, and reserving space for the
358 * separator and quotes.
360 _cleanup_free_ char *esc = NULL;
363 if (!GREEDY_REALLOC(buf, allocated,
364 len + strlen(*s) * 2 + 3))
371 needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
372 len > 0 ? " " : "", esc);
373 assert(needed < allocated - len);
387 int strv_push(char ***l, char *value) {
396 /* Increase and check for overflow */
401 c = realloc_multiply(*l, sizeof(char*), m);
412 int strv_push_pair(char ***l, char *a, char *b) {
421 /* increase and check for overflow */
422 m = n + !!a + !!b + 1;
426 c = realloc_multiply(*l, sizeof(char*), m);
440 int strv_push_prepend(char ***l, char *value) {
449 /* increase and check for overflow */
458 for (i = 0; i < n; i++)
470 int strv_consume(char ***l, char *value) {
473 r = strv_push(l, value);
480 int strv_consume_pair(char ***l, char *a, char *b) {
483 r = strv_push_pair(l, a, b);
492 int strv_consume_prepend(char ***l, char *value) {
495 r = strv_push_prepend(l, value);
502 int strv_extend(char ***l, const char *value) {
512 return strv_consume(l, v);
515 char **strv_uniq(char **l) {
518 /* Drops duplicate entries. The first identical string will be
519 * kept, the others dropped */
522 strv_remove(i+1, *i);
527 bool strv_is_uniq(char **l) {
531 if (strv_find(i+1, *i))
537 char **strv_remove(char **l, const char *s) {
545 /* Drops every occurrence of s in the string list, edits
548 for (f = t = l; *f; f++)
558 char **strv_parse_nulstr(const char *s, size_t l) {
560 unsigned c = 0, i = 0;
566 return new0(char*, 1);
568 for (p = s; p < s + l; p++)
575 v = new0(char*, c+1);
583 e = memchr(p, 0, s + l - p);
585 v[i] = strndup(p, e ? e - p : s + l - p);
604 char **strv_split_nulstr(const char *s) {
609 if (strv_extend(&r, i) < 0) {
615 return strv_new(NULL, NULL);
620 bool strv_overlap(char **a, char **b) {
624 if (strv_contains(b, *i))
630 static int str_compare(const void *_a, const void *_b) {
631 const char **a = (const char**) _a, **b = (const char**) _b;
633 return strcmp(*a, *b);
636 char **strv_sort(char **l) {
641 qsort(l, strv_length(l), sizeof(char*), str_compare);
645 bool strv_equal(char **a, char **b) {
649 for ( ; *a || *b; ++a, ++b)
650 if (!streq_ptr(*a, *b))
656 void strv_print(char **l) {
663 int strv_extendf(char ***l, const char *format, ...) {
668 va_start(ap, format);
669 r = vasprintf(&x, format, ap);
675 return strv_consume(l, x);
678 char **strv_reverse(char **l) {
685 for (i = 0; i < n / 2; i++) {