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 void strv_free(char **l) {
67 char **strv_copy(char * const *l) {
70 k = r = new(char*, strv_length(l) + 1);
75 for (; *l; k++, l++) {
87 unsigned strv_length(char * const *l) {
99 char **strv_new_ap(const char *x, va_list ap) {
102 unsigned n = 0, i = 0;
105 /* As a special trick we ignore all listed strings that equal
106 * (const char*) -1. This is supposed to be used with the
107 * STRV_IFNOTNULL() macro to include possibly NULL strings in
108 * the string list. */
111 n = x == (const char*) -1 ? 0 : 1;
114 while ((s = va_arg(aq, const char*))) {
115 if (s == (const char*) -1)
129 if (x != (const char*) -1) {
136 while ((s = va_arg(ap, const char*))) {
138 if (s == (const char*) -1)
158 char **strv_new(const char *x, ...) {
163 r = strv_new_ap(x, ap);
169 int strv_extend_strv(char ***a, char **b) {
174 r = strv_extend(a, *s);
182 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
189 v = strappend(*s, suffix);
203 char **strv_split(const char *s, const char *separator) {
204 const char *word, *state;
212 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
220 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
221 r[i] = strndup(word, l);
234 int strv_split_quoted(char ***t, const char *s) {
235 const char *word, *state;
243 FOREACH_WORD_QUOTED(word, l, s, state)
254 FOREACH_WORD_QUOTED(word, l, s, state) {
255 r[i] = cunescape_length(word, l);
268 char **strv_split_newlines(const char *s) {
274 /* Special version of strv_split() that splits on newlines and
275 * suppresses an empty string at the end */
277 l = strv_split(s, NEWLINE);
285 if (isempty(l[n-1])) {
293 char *strv_join(char **l, const char *separator) {
301 k = strlen(separator);
317 e = stpcpy(e, separator);
327 char *strv_join_quoted(char **l) {
330 size_t allocated = 0, len = 0;
333 /* assuming here that escaped string cannot be more
334 * than twice as long, and reserving space for the
335 * separator and quotes.
337 _cleanup_free_ char *esc = NULL;
340 if (!GREEDY_REALLOC(buf, allocated,
341 len + strlen(*s) * 2 + 3))
348 needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
349 len > 0 ? " " : "", esc);
350 assert(needed < allocated - len);
364 int strv_push(char ***l, char *value) {
372 c = realloc(*l, sizeof(char*) * (n + 2));
383 int strv_push_pair(char ***l, char *a, char *b) {
392 /* increase and check for overflow */
393 m = n + !!a + !!b + 1;
397 c = realloc_multiply(*l, sizeof(char*), m);
411 int strv_push_prepend(char ***l, char *value) {
419 c = new(char*, n + 2);
423 for (i = 0; i < n; i++)
435 int strv_consume(char ***l, char *value) {
438 r = strv_push(l, value);
445 int strv_consume_pair(char ***l, char *a, char *b) {
448 r = strv_push_pair(l, a, b);
457 int strv_consume_prepend(char ***l, char *value) {
460 r = strv_push_prepend(l, value);
467 int strv_extend(char ***l, const char *value) {
477 return strv_consume(l, v);
480 char **strv_uniq(char **l) {
483 /* Drops duplicate entries. The first identical string will be
484 * kept, the others dropped */
487 strv_remove(i+1, *i);
492 char **strv_remove(char **l, const char *s) {
500 /* Drops every occurrence of s in the string list, edits
503 for (f = t = l; *f; f++)
513 char **strv_parse_nulstr(const char *s, size_t l) {
515 unsigned c = 0, i = 0;
521 return new0(char*, 1);
523 for (p = s; p < s + l; p++)
530 v = new0(char*, c+1);
538 e = memchr(p, 0, s + l - p);
540 v[i] = strndup(p, e ? e - p : s + l - p);
559 char **strv_split_nulstr(const char *s) {
564 if (strv_extend(&r, i) < 0) {
570 return strv_new(NULL, NULL);
575 bool strv_overlap(char **a, char **b) {
579 if (strv_contains(b, *i))
585 static int str_compare(const void *_a, const void *_b) {
586 const char **a = (const char**) _a, **b = (const char**) _b;
588 return strcmp(*a, *b);
591 char **strv_sort(char **l) {
596 qsort(l, strv_length(l), sizeof(char*), str_compare);
600 void strv_print(char **l) {
607 int strv_extendf(char ***l, const char *format, ...) {
612 va_start(ap, format);
613 r = vasprintf(&x, format, ap);
619 return strv_consume(l, x);