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 void strv_freep(char ***l) {
71 char **strv_copy(char **l) {
74 k = r = new(char*, strv_length(l) + 1);
79 for (; *l; k++, l++) {
91 unsigned strv_length(char **l) {
103 char **strv_new_ap(const char *x, va_list ap) {
106 unsigned n = 0, i = 0;
109 /* As a special trick we ignore all listed strings that equal
110 * (const char*) -1. This is supposed to be used with the
111 * STRV_IFNOTNULL() macro to include possibly NULL strings in
112 * the string list. */
115 n = x == (const char*) -1 ? 0 : 1;
118 while ((s = va_arg(aq, const char*))) {
119 if (s == (const char*) -1)
133 if (x != (const char*) -1) {
140 while ((s = va_arg(ap, const char*))) {
142 if (s == (const char*) -1)
162 char **strv_new(const char *x, ...) {
167 r = strv_new_ap(x, ap);
173 char **strv_merge(char **a, char **b) {
182 r = new(char*, strv_length(a) + strv_length(b) + 1);
186 for (k = r; *a; k++, a++) {
192 for (; *b; k++, b++) {
206 char **strv_merge_concat(char **a, char **b, const char *suffix) {
209 /* Like strv_merge(), but appends suffix to all strings in b, before adding */
214 r = new(char*, strv_length(a) + strv_length(b) + 1);
220 for (; *a; k++, a++) {
226 for (; *b; k++, b++) {
227 *k = strappend(*b, suffix);
241 char **strv_split(const char *s, const char *separator) {
251 FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
259 FOREACH_WORD_SEPARATOR(w, l, s, separator, state) {
260 r[i] = strndup(w, l);
273 char **strv_split_quoted(const char *s) {
283 FOREACH_WORD_QUOTED(w, l, s, state)
291 FOREACH_WORD_QUOTED(w, l, s, state) {
292 r[i] = cunescape_length(w, l);
304 char **strv_split_newlines(const char *s) {
310 /* Special version of strv_split() that splits on newlines and
311 * suppresses an empty string at the end */
313 l = strv_split(s, NEWLINE);
321 if (isempty(l[n-1])) {
329 char *strv_join(char **l, const char *separator) {
337 k = strlen(separator);
353 e = stpcpy(e, separator);
363 char **strv_append(char **l, const char *s) {
367 return strv_new(s, NULL);
372 r = new(char*, strv_length(l)+2);
376 for (k = r; *l; k++, l++) {
394 int strv_extend(char ***l, const char *value) {
407 c = realloc(*l, sizeof(char*) * (n + 2));
420 char **strv_uniq(char **l) {
423 /* Drops duplicate entries. The first identical string will be
424 * kept, the others dropped */
427 strv_remove(i+1, *i);
432 char **strv_remove(char **l, const char *s) {
440 /* Drops every occurrence of s in the string list, edits
443 for (f = t = l; *f; f++) {
457 char **strv_remove_prefix(char **l, const char *s) {
465 /* Drops every occurrence of a string prefixed with s in the
466 * string list, edits in-place. */
468 for (f = t = l; *f; f++) {
470 if (startswith(*f, s)) {
482 char **strv_parse_nulstr(const char *s, size_t l) {
484 unsigned c = 0, i = 0;
490 return strv_new(NULL, NULL);
492 for (p = s; p < s + l; p++)
499 v = new0(char*, c+1);
507 e = memchr(p, 0, s + l - p);
509 v[i] = strndup(p, e ? e - p : s + l - p);
528 char **strv_split_nulstr(const char *s) {
533 if (strv_extend(&r, i) < 0) {
539 return strv_new(NULL, NULL);
544 bool strv_overlap(char **a, char **b) {
557 static int str_compare(const void *_a, const void *_b) {
558 const char **a = (const char**) _a, **b = (const char**) _b;
560 return strcmp(*a, *b);
563 char **strv_sort(char **l) {
568 qsort(l, strv_length(l), sizeof(char*), str_compare);
572 void strv_print(char **l) {