2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "alloc-util.h"
29 #include "extract-word.h"
31 #include "string-util.h"
35 char *strv_find(char **l, const char *name) {
47 char *strv_find_prefix(char **l, const char *name) {
53 if (startswith(*i, name))
59 char *strv_find_startswith(char **l, const char *name) {
64 /* Like strv_find_prefix, but actually returns only the
65 * suffix, not the whole item */
68 e = startswith(*i, name);
76 void strv_clear(char **l) {
88 char **strv_free(char **l) {
94 char **strv_free_erase(char **l) {
103 char **strv_copy(char * const *l) {
106 k = r = new(char*, strv_length(l) + 1);
111 for (; *l; k++, l++) {
123 unsigned strv_length(char * const *l) {
135 char **strv_new_ap(const char *x, va_list ap) {
138 unsigned n = 0, i = 0;
141 /* As a special trick we ignore all listed strings that equal
142 * (const char*) -1. This is supposed to be used with the
143 * STRV_IFNOTNULL() macro to include possibly NULL strings in
144 * the string list. */
147 n = x == (const char*) -1 ? 0 : 1;
150 while ((s = va_arg(aq, const char*))) {
151 if (s == (const char*) -1)
165 if (x != (const char*) -1) {
172 while ((s = va_arg(ap, const char*))) {
174 if (s == (const char*) -1)
194 char **strv_new(const char *x, ...) {
199 r = strv_new_ap(x, ap);
205 #if 0 /// UNNEEDED by elogind
206 int strv_extend_strv(char ***a, char **b, bool filter_duplicates) {
208 size_t p, q, i = 0, j;
218 t = realloc(*a, sizeof(char*) * (p + q + 1));
227 if (filter_duplicates && strv_contains(t, *s))
243 for (j = 0; j < i; j++)
250 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
257 v = strappend(*s, suffix);
272 char **strv_split(const char *s, const char *separator) {
273 const char *word, *state;
281 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
289 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
290 r[i] = strndup(word, l);
303 #if 0 /// UNNEEDED by elogind
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]))
322 l[n - 1] = mfree(l[n - 1]);
327 int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags) {
328 _cleanup_strv_free_ char **l = NULL;
329 size_t n = 0, allocated = 0;
336 _cleanup_free_ char *word = NULL;
338 r = extract_first_word(&s, &word, separators, flags);
344 if (!GREEDY_REALLOC(l, allocated, n + 2))
366 char *strv_join(char **l, const char *separator) {
374 k = strlen(separator);
390 e = stpcpy(e, separator);
400 #if 0 /// UNNEEDED by elogind
401 char *strv_join_quoted(char **l) {
404 size_t allocated = 0, len = 0;
407 /* assuming here that escaped string cannot be more
408 * than twice as long, and reserving space for the
409 * separator and quotes.
411 _cleanup_free_ char *esc = NULL;
414 if (!GREEDY_REALLOC(buf, allocated,
415 len + strlen(*s) * 2 + 3))
422 needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
423 len > 0 ? " " : "", esc);
424 assert(needed < allocated - len);
439 int strv_push(char ***l, char *value) {
448 /* Increase and check for overflow */
453 c = realloc_multiply(*l, sizeof(char*), m);
464 int strv_push_pair(char ***l, char *a, char *b) {
473 /* increase and check for overflow */
474 m = n + !!a + !!b + 1;
478 c = realloc_multiply(*l, sizeof(char*), m);
492 int strv_push_prepend(char ***l, char *value) {
501 /* increase and check for overflow */
510 for (i = 0; i < n; i++)
522 int strv_consume(char ***l, char *value) {
525 r = strv_push(l, value);
532 #if 0 /// UNNEEDED by elogind
533 int strv_consume_pair(char ***l, char *a, char *b) {
536 r = strv_push_pair(l, a, b);
546 int strv_consume_prepend(char ***l, char *value) {
549 r = strv_push_prepend(l, value);
556 int strv_extend(char ***l, const char *value) {
566 return strv_consume(l, v);
569 int strv_extend_front(char ***l, const char *value) {
575 /* Like strv_extend(), but prepends rather than appends the new entry */
582 /* Increase and overflow check. */
591 c = realloc_multiply(*l, sizeof(char*), m);
597 memmove(c+1, c, n * sizeof(char*));
605 char **strv_uniq(char **l) {
608 /* Drops duplicate entries. The first identical string will be
609 * kept, the others dropped */
612 strv_remove(i+1, *i);
617 #if 0 /// UNNEEDED by elogind
618 bool strv_is_uniq(char **l) {
622 if (strv_find(i+1, *i))
629 char **strv_remove(char **l, const char *s) {
637 /* Drops every occurrence of s in the string list, edits
640 for (f = t = l; *f; f++)
650 char **strv_parse_nulstr(const char *s, size_t l) {
652 unsigned c = 0, i = 0;
658 return new0(char*, 1);
660 for (p = s; p < s + l; p++)
667 v = new0(char*, c+1);
675 e = memchr(p, 0, s + l - p);
677 v[i] = strndup(p, e ? e - p : s + l - p);
696 char **strv_split_nulstr(const char *s) {
701 if (strv_extend(&r, i) < 0) {
707 return strv_new(NULL, NULL);
712 #if 0 /// UNNEEDED by elogind
713 int strv_make_nulstr(char **l, char **p, size_t *q) {
714 size_t n_allocated = 0, n = 0;
715 _cleanup_free_ char *m = NULL;
726 if (!GREEDY_REALLOC(m, n_allocated, n + z + 1))
729 memcpy(m + n, *i, z + 1);
748 bool strv_overlap(char **a, char **b) {
752 if (strv_contains(b, *i))
759 static int str_compare(const void *_a, const void *_b) {
760 const char **a = (const char**) _a, **b = (const char**) _b;
762 return strcmp(*a, *b);
765 char **strv_sort(char **l) {
770 qsort(l, strv_length(l), sizeof(char*), str_compare);
774 #if 0 /// UNNEEDED by elogind
775 bool strv_equal(char **a, char **b) {
778 return strv_isempty(b);
783 for ( ; *a || *b; ++a, ++b)
784 if (!streq_ptr(*a, *b))
790 void strv_print(char **l) {
797 int strv_extendf(char ***l, const char *format, ...) {
802 va_start(ap, format);
803 r = vasprintf(&x, format, ap);
809 return strv_consume(l, x);
812 char **strv_reverse(char **l) {
819 for (i = 0; i < n / 2; i++) {
830 char **strv_shell_escape(char **l, const char *bad) {
833 /* Escapes every character in every string in l that is in bad,
834 * edits in-place, does not roll-back on error. */
839 v = shell_escape(*s, bad);
850 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
853 STRV_FOREACH(p, patterns)
854 if (fnmatch(*p, s, 0) == 0)
860 char ***strv_free_free(char ***l) {
873 char **strv_skip(char **l, size_t n) {
885 int strv_extend_n(char ***l, const char *value, size_t n) {
896 /* Adds the value value n times to l */
900 nl = realloc(*l, sizeof(char*) * (k + n + 1));
906 for (i = k; i < k + n; i++) {
907 nl[i] = strdup(value);
916 for (j = k; j < i; j++)
923 int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
928 /* Like fputs(), but for strv, and with a less stupid argument order */
934 r = fputs_with_space(f, *s, separator, space);