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_free(char **l) {
84 char **strv_copy(char * const *l) {
87 k = r = new(char*, strv_length(l) + 1);
92 for (; *l; k++, l++) {
104 unsigned strv_length(char * const *l) {
116 char **strv_new_ap(const char *x, va_list ap) {
119 unsigned n = 0, i = 0;
122 /* As a special trick we ignore all listed strings that equal
123 * (const char*) -1. This is supposed to be used with the
124 * STRV_IFNOTNULL() macro to include possibly NULL strings in
125 * the string list. */
128 n = x == (const char*) -1 ? 0 : 1;
131 while ((s = va_arg(aq, const char*))) {
132 if (s == (const char*) -1)
146 if (x != (const char*) -1) {
153 while ((s = va_arg(ap, const char*))) {
155 if (s == (const char*) -1)
175 char **strv_new(const char *x, ...) {
180 r = strv_new_ap(x, ap);
186 int strv_extend_strv(char ***a, char **b) {
191 r = strv_extend(a, *s);
199 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
206 v = strappend(*s, suffix);
220 char **strv_split(const char *s, const char *separator) {
221 const char *word, *state;
229 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
237 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
238 r[i] = strndup(word, l);
251 char **strv_split_newlines(const char *s) {
257 /* Special version of strv_split() that splits on newlines and
258 * suppresses an empty string at the end */
260 l = strv_split(s, NEWLINE);
268 if (isempty(l[n-1])) {
276 int strv_split_quoted(char ***t, const char *s, bool relax) {
277 size_t n = 0, allocated = 0;
278 _cleanup_strv_free_ char **l = NULL;
285 _cleanup_free_ char *word = NULL;
287 r = unquote_first_word(&s, &word, relax);
293 if (!GREEDY_REALLOC(l, allocated, n + 2))
311 char *strv_join(char **l, const char *separator) {
319 k = strlen(separator);
335 e = stpcpy(e, separator);
345 char *strv_join_quoted(char **l) {
348 size_t allocated = 0, len = 0;
351 /* assuming here that escaped string cannot be more
352 * than twice as long, and reserving space for the
353 * separator and quotes.
355 _cleanup_free_ char *esc = NULL;
358 if (!GREEDY_REALLOC(buf, allocated,
359 len + strlen(*s) * 2 + 3))
366 needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
367 len > 0 ? " " : "", esc);
368 assert(needed < allocated - len);
382 int strv_push(char ***l, char *value) {
391 /* Increase and check for overflow */
396 c = realloc_multiply(*l, sizeof(char*), m);
407 int strv_push_pair(char ***l, char *a, char *b) {
416 /* increase and check for overflow */
417 m = n + !!a + !!b + 1;
421 c = realloc_multiply(*l, sizeof(char*), m);
435 int strv_push_prepend(char ***l, char *value) {
444 /* increase and check for overflow */
453 for (i = 0; i < n; i++)
465 int strv_consume(char ***l, char *value) {
468 r = strv_push(l, value);
475 int strv_consume_pair(char ***l, char *a, char *b) {
478 r = strv_push_pair(l, a, b);
487 int strv_consume_prepend(char ***l, char *value) {
490 r = strv_push_prepend(l, value);
497 int strv_extend(char ***l, const char *value) {
507 return strv_consume(l, v);
510 char **strv_uniq(char **l) {
513 /* Drops duplicate entries. The first identical string will be
514 * kept, the others dropped */
517 strv_remove(i+1, *i);
522 char **strv_remove(char **l, const char *s) {
530 /* Drops every occurrence of s in the string list, edits
533 for (f = t = l; *f; f++)
543 char **strv_parse_nulstr(const char *s, size_t l) {
545 unsigned c = 0, i = 0;
551 return new0(char*, 1);
553 for (p = s; p < s + l; p++)
560 v = new0(char*, c+1);
568 e = memchr(p, 0, s + l - p);
570 v[i] = strndup(p, e ? e - p : s + l - p);
589 char **strv_split_nulstr(const char *s) {
594 if (strv_extend(&r, i) < 0) {
600 return strv_new(NULL, NULL);
605 bool strv_overlap(char **a, char **b) {
609 if (strv_contains(b, *i))
615 static int str_compare(const void *_a, const void *_b) {
616 const char **a = (const char**) _a, **b = (const char**) _b;
618 return strcmp(*a, *b);
621 char **strv_sort(char **l) {
626 qsort(l, strv_length(l), sizeof(char*), str_compare);
630 bool strv_equal(char **a, char **b) {
634 for ( ; *a || *b; ++a, ++b)
635 if (!streq_ptr(*a, *b))
641 void strv_print(char **l) {
648 int strv_extendf(char ***l, const char *format, ...) {
653 va_start(ap, format);
654 r = vasprintf(&x, format, ap);
660 return strv_consume(l, x);