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) {
213 FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
221 FOREACH_WORD_SEPARATOR(w, l, s, separator, state) {
222 r[i] = strndup(w, l);
235 char **strv_split_quoted(const char *s) {
245 FOREACH_WORD_QUOTED(w, l, s, state)
253 FOREACH_WORD_QUOTED(w, l, s, state) {
254 r[i] = cunescape_length(w, l);
266 char **strv_split_newlines(const char *s) {
272 /* Special version of strv_split() that splits on newlines and
273 * suppresses an empty string at the end */
275 l = strv_split(s, NEWLINE);
283 if (isempty(l[n-1])) {
291 char *strv_join(char **l, const char *separator) {
299 k = strlen(separator);
315 e = stpcpy(e, separator);
325 char *strv_join_quoted(char **l) {
328 size_t allocated = 0, len = 0;
331 /* assuming here that escaped string cannot be more
332 * than twice as long, and reserving space for the
333 * separator and quotes.
335 _cleanup_free_ char *esc = NULL;
338 if (!GREEDY_REALLOC(buf, allocated,
339 len + strlen(*s) * 2 + 3))
346 needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
347 len > 0 ? " " : "", esc);
348 assert(needed < allocated - len);
362 int strv_push(char ***l, char *value) {
370 c = realloc(*l, sizeof(char*) * (n + 2));
381 int strv_push_prepend(char ***l, char *value) {
389 c = new(char*, n + 2);
393 for (i = 0; i < n; i++)
405 int strv_consume(char ***l, char *value) {
408 r = strv_push(l, value);
415 int strv_consume_prepend(char ***l, char *value) {
418 r = strv_push_prepend(l, value);
425 int strv_extend(char ***l, const char *value) {
435 return strv_consume(l, v);
438 char **strv_uniq(char **l) {
441 /* Drops duplicate entries. The first identical string will be
442 * kept, the others dropped */
445 strv_remove(i+1, *i);
450 char **strv_remove(char **l, const char *s) {
458 /* Drops every occurrence of s in the string list, edits
461 for (f = t = l; *f; f++)
471 char **strv_parse_nulstr(const char *s, size_t l) {
473 unsigned c = 0, i = 0;
479 return new0(char*, 1);
481 for (p = s; p < s + l; p++)
488 v = new0(char*, c+1);
496 e = memchr(p, 0, s + l - p);
498 v[i] = strndup(p, e ? e - p : s + l - p);
517 char **strv_split_nulstr(const char *s) {
522 if (strv_extend(&r, i) < 0) {
528 return strv_new(NULL, NULL);
533 bool strv_overlap(char **a, char **b) {
537 if (strv_contains(b, *i))
543 static int str_compare(const void *_a, const void *_b) {
544 const char **a = (const char**) _a, **b = (const char**) _b;
546 return strcmp(*a, *b);
549 char **strv_sort(char **l) {
554 qsort(l, strv_length(l), sizeof(char*), str_compare);
558 void strv_print(char **l) {
565 int strv_extendf(char ***l, const char *format, ...) {
570 va_start(ap, format);
571 r = vasprintf(&x, format, ap);
577 return strv_consume(l, x);