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/>.
30 char *strv_find(char **l, const char *name) {
42 char *strv_find_prefix(char **l, const char *name) {
48 if (startswith(*i, name))
54 char *strv_find_startswith(char **l, const char *name) {
59 /* Like strv_find_prefix, but actually returns only the
60 * suffix, not the whole item */
63 e = startswith(*i, name);
71 void strv_clear(char **l) {
83 void strv_free(char **l) {
88 char **strv_copy(char * const *l) {
91 k = r = new(char*, strv_length(l) + 1);
96 for (; *l; k++, l++) {
108 unsigned strv_length(char * const *l) {
120 char **strv_new_ap(const char *x, va_list ap) {
123 unsigned n = 0, i = 0;
126 /* As a special trick we ignore all listed strings that equal
127 * (const char*) -1. This is supposed to be used with the
128 * STRV_IFNOTNULL() macro to include possibly NULL strings in
129 * the string list. */
132 n = x == (const char*) -1 ? 0 : 1;
135 while ((s = va_arg(aq, const char*))) {
136 if (s == (const char*) -1)
150 if (x != (const char*) -1) {
157 while ((s = va_arg(ap, const char*))) {
159 if (s == (const char*) -1)
179 char **strv_new(const char *x, ...) {
184 r = strv_new_ap(x, ap);
190 int strv_extend_strv(char ***a, char **b) {
195 r = strv_extend(a, *s);
203 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
210 v = strappend(*s, suffix);
224 char **strv_split(const char *s, const char *separator) {
225 const char *word, *state;
233 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
241 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
242 r[i] = strndup(word, l);
255 char **strv_split_newlines(const char *s) {
261 /* Special version of strv_split() that splits on newlines and
262 * suppresses an empty string at the end */
264 l = strv_split(s, NEWLINE);
272 if (isempty(l[n-1])) {
280 int strv_split_quoted(char ***t, const char *s, bool relax) {
281 size_t n = 0, allocated = 0;
282 _cleanup_strv_free_ char **l = NULL;
289 _cleanup_free_ char *word = NULL;
291 r = unquote_first_word(&s, &word, relax);
297 if (!GREEDY_REALLOC(l, allocated, n + 2))
315 char *strv_join(char **l, const char *separator) {
323 k = strlen(separator);
339 e = stpcpy(e, separator);
349 char *strv_join_quoted(char **l) {
352 size_t allocated = 0, len = 0;
355 /* assuming here that escaped string cannot be more
356 * than twice as long, and reserving space for the
357 * separator and quotes.
359 _cleanup_free_ char *esc = NULL;
362 if (!GREEDY_REALLOC(buf, allocated,
363 len + strlen(*s) * 2 + 3))
370 needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
371 len > 0 ? " " : "", esc);
372 assert(needed < allocated - len);
386 int strv_push(char ***l, char *value) {
395 /* Increase and check for overflow */
400 c = realloc_multiply(*l, sizeof(char*), m);
411 int strv_push_pair(char ***l, char *a, char *b) {
420 /* increase and check for overflow */
421 m = n + !!a + !!b + 1;
425 c = realloc_multiply(*l, sizeof(char*), m);
439 int strv_push_prepend(char ***l, char *value) {
448 /* increase and check for overflow */
457 for (i = 0; i < n; i++)
469 int strv_consume(char ***l, char *value) {
472 r = strv_push(l, value);
479 int strv_consume_pair(char ***l, char *a, char *b) {
482 r = strv_push_pair(l, a, b);
491 int strv_consume_prepend(char ***l, char *value) {
494 r = strv_push_prepend(l, value);
501 int strv_extend(char ***l, const char *value) {
511 return strv_consume(l, v);
514 char **strv_uniq(char **l) {
517 /* Drops duplicate entries. The first identical string will be
518 * kept, the others dropped */
521 strv_remove(i+1, *i);
526 bool strv_is_uniq(char **l) {
530 if (strv_find(i+1, *i))
536 char **strv_remove(char **l, const char *s) {
544 /* Drops every occurrence of s in the string list, edits
547 for (f = t = l; *f; f++)
557 char **strv_parse_nulstr(const char *s, size_t l) {
559 unsigned c = 0, i = 0;
565 return new0(char*, 1);
567 for (p = s; p < s + l; p++)
574 v = new0(char*, c+1);
582 e = memchr(p, 0, s + l - p);
584 v[i] = strndup(p, e ? e - p : s + l - p);
603 char **strv_split_nulstr(const char *s) {
608 if (strv_extend(&r, i) < 0) {
614 return strv_new(NULL, NULL);
619 bool strv_overlap(char **a, char **b) {
623 if (strv_contains(b, *i))
629 static int str_compare(const void *_a, const void *_b) {
630 const char **a = (const char**) _a, **b = (const char**) _b;
632 return strcmp(*a, *b);
635 char **strv_sort(char **l) {
640 qsort(l, strv_length(l), sizeof(char*), str_compare);
644 bool strv_equal(char **a, char **b) {
648 for ( ; *a || *b; ++a, ++b)
649 if (!streq_ptr(*a, *b))
655 void strv_print(char **l) {
662 int strv_extendf(char ***l, const char *format, ...) {
667 va_start(ap, format);
668 r = vasprintf(&x, format, ap);
674 return strv_consume(l, x);
677 char **strv_reverse(char **l) {
684 for (i = 0; i < n / 2; i++) {
695 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
698 STRV_FOREACH(p, patterns)
699 if (fnmatch(*p, s, 0) == 0)