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) {
93 char **strv_free_erase(char **l) {
102 char **strv_copy(char * const *l) {
105 k = r = new(char*, strv_length(l) + 1);
110 for (; *l; k++, l++) {
122 unsigned strv_length(char * const *l) {
134 char **strv_new_ap(const char *x, va_list ap) {
137 unsigned n = 0, i = 0;
140 /* As a special trick we ignore all listed strings that equal
141 * STRV_IGNORE. This is supposed to be used with the
142 * STRV_IFNOTNULL() macro to include possibly NULL strings in
143 * the string list. */
146 n = x == STRV_IGNORE ? 0 : 1;
149 while ((s = va_arg(aq, const char*))) {
150 if (s == STRV_IGNORE)
164 if (x != STRV_IGNORE) {
171 while ((s = va_arg(ap, const char*))) {
173 if (s == STRV_IGNORE)
193 char **strv_new(const char *x, ...) {
198 r = strv_new_ap(x, ap);
204 #if 0 /// UNNEEDED by elogind
205 int strv_extend_strv(char ***a, char **b, bool filter_duplicates) {
207 size_t p, q, i = 0, j;
217 t = realloc(*a, sizeof(char*) * (p + q + 1));
226 if (filter_duplicates && strv_contains(t, *s))
242 for (j = 0; j < i; j++)
249 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
256 v = strappend(*s, suffix);
271 char **strv_split(const char *s, const char *separator) {
272 const char *word, *state;
280 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
288 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
289 r[i] = strndup(word, l);
302 #if 0 /// UNNEEDED by elogind
303 char **strv_split_newlines(const char *s) {
309 /* Special version of strv_split() that splits on newlines and
310 * suppresses an empty string at the end */
312 l = strv_split(s, NEWLINE);
320 if (isempty(l[n - 1]))
321 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))
365 char *strv_join(char **l, const char *separator) {
373 k = strlen(separator);
389 e = stpcpy(e, separator);
399 #if 0 /// UNNEEDED by elogind
400 char *strv_join_quoted(char **l) {
403 size_t allocated = 0, len = 0;
406 /* assuming here that escaped string cannot be more
407 * than twice as long, and reserving space for the
408 * separator and quotes.
410 _cleanup_free_ char *esc = NULL;
413 if (!GREEDY_REALLOC(buf, allocated,
414 len + strlen(*s) * 2 + 3))
421 needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
422 len > 0 ? " " : "", esc);
423 assert(needed < allocated - len);
437 int strv_push(char ***l, char *value) {
446 /* Increase and check for overflow */
451 c = realloc_multiply(*l, sizeof(char*), m);
462 int strv_push_pair(char ***l, char *a, char *b) {
471 /* increase and check for overflow */
472 m = n + !!a + !!b + 1;
476 c = realloc_multiply(*l, sizeof(char*), m);
490 int strv_push_prepend(char ***l, char *value) {
499 /* increase and check for overflow */
508 for (i = 0; i < n; i++)
520 int strv_consume(char ***l, char *value) {
523 r = strv_push(l, value);
530 #if 0 /// UNNEEDED by elogind
531 int strv_consume_pair(char ***l, char *a, char *b) {
534 r = strv_push_pair(l, a, b);
544 int strv_consume_prepend(char ***l, char *value) {
547 r = strv_push_prepend(l, value);
554 int strv_extend(char ***l, const char *value) {
564 return strv_consume(l, v);
567 int strv_extend_front(char ***l, const char *value) {
573 /* Like strv_extend(), but prepends rather than appends the new entry */
580 /* Increase and overflow check. */
589 c = realloc_multiply(*l, sizeof(char*), m);
595 memmove(c+1, c, n * sizeof(char*));
603 char **strv_uniq(char **l) {
606 /* Drops duplicate entries. The first identical string will be
607 * kept, the others dropped */
610 strv_remove(i+1, *i);
615 #if 0 /// UNNEEDED by elogind
616 bool strv_is_uniq(char **l) {
620 if (strv_find(i+1, *i))
627 char **strv_remove(char **l, const char *s) {
635 /* Drops every occurrence of s in the string list, edits
638 for (f = t = l; *f; f++)
648 char **strv_parse_nulstr(const char *s, size_t l) {
649 /* l is the length of the input data, which will be split at NULs into
650 * elements of the resulting strv. Hence, the number of items in the resulting strv
651 * will be equal to one plus the number of NUL bytes in the l bytes starting at s,
652 * unless s[l-1] is NUL, in which case the final empty string is not stored in
653 * the resulting strv, and length is equal to the number of NUL bytes.
655 * Note that contrary to a normal nulstr which cannot contain empty strings, because
656 * the input data is terminated by any two consequent NUL bytes, this parser accepts
657 * empty strings in s.
661 unsigned c = 0, i = 0;
667 return new0(char*, 1);
669 for (p = s; p < s + l; p++)
676 v = new0(char*, c+1);
684 e = memchr(p, 0, s + l - p);
686 v[i] = strndup(p, e ? e - p : s + l - p);
705 char **strv_split_nulstr(const char *s) {
710 if (strv_extend(&r, i) < 0) {
716 return strv_new(NULL, NULL);
721 #if 0 /// UNNEEDED by elogind
722 int strv_make_nulstr(char **l, char **p, size_t *q) {
723 /* A valid nulstr with two NULs at the end will be created, but
724 * q will be the length without the two trailing NULs. Thus the output
725 * string is a valid nulstr and can be iterated over using NULSTR_FOREACH,
726 * and can also be parsed by strv_parse_nulstr as long as the length
727 * is provided separately.
730 size_t n_allocated = 0, n = 0;
731 _cleanup_free_ char *m = NULL;
742 if (!GREEDY_REALLOC(m, n_allocated, n + z + 2))
745 memcpy(m + n, *i, z + 1);
755 /* make sure there is a second extra NUL at the end of resulting nulstr */
767 bool strv_overlap(char **a, char **b) {
771 if (strv_contains(b, *i))
778 static int str_compare(const void *_a, const void *_b) {
779 const char **a = (const char**) _a, **b = (const char**) _b;
781 return strcmp(*a, *b);
784 char **strv_sort(char **l) {
789 qsort(l, strv_length(l), sizeof(char*), str_compare);
793 bool strv_equal(char **a, char **b) {
796 return strv_isempty(b);
801 for ( ; *a || *b; ++a, ++b)
802 if (!streq_ptr(*a, *b))
808 void strv_print(char **l) {
815 #if 0 /// UNNEEDED by elogind
816 int strv_extendf(char ***l, const char *format, ...) {
821 va_start(ap, format);
822 r = vasprintf(&x, format, ap);
828 return strv_consume(l, x);
831 char **strv_reverse(char **l) {
838 for (i = 0; i < n / 2; i++)
839 SWAP_TWO(l[i], l[n-1-i]);
844 char **strv_shell_escape(char **l, const char *bad) {
847 /* Escapes every character in every string in l that is in bad,
848 * edits in-place, does not roll-back on error. */
853 v = shell_escape(*s, bad);
864 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
867 STRV_FOREACH(p, patterns)
868 if (fnmatch(*p, s, flags) == 0)
874 char ***strv_free_free(char ***l) {
886 char **strv_skip(char **l, size_t n) {
898 int strv_extend_n(char ***l, const char *value, size_t n) {
909 /* Adds the value n times to l */
913 nl = realloc(*l, sizeof(char*) * (k + n + 1));
919 for (i = k; i < k + n; i++) {
920 nl[i] = strdup(value);
929 for (j = k; j < i; j++)
936 int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
941 /* Like fputs(), but for strv, and with a less stupid argument order */
947 r = fputs_with_space(f, *s, separator, space);