1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "alloc-util.h"
30 #include "extract-word.h"
32 #include "string-util.h"
36 char *strv_find(char **l, const char *name) {
48 char *strv_find_prefix(char **l, const char *name) {
54 if (startswith(*i, name))
60 char *strv_find_startswith(char **l, const char *name) {
65 /* Like strv_find_prefix, but actually returns only the
66 * suffix, not the whole item */
69 e = startswith(*i, name);
77 void strv_clear(char **l) {
89 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 * STRV_IGNORE. 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 == STRV_IGNORE ? 0 : 1;
150 while ((s = va_arg(aq, const char*))) {
151 if (s == STRV_IGNORE)
165 if (x != STRV_IGNORE) {
172 while ((s = va_arg(ap, const char*))) {
174 if (s == STRV_IGNORE)
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 = reallocarray(*a, p + q + 1, sizeof(char *));
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]);
328 int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags) {
329 _cleanup_strv_free_ char **l = NULL;
330 size_t n = 0, allocated = 0;
337 _cleanup_free_ char *word = NULL;
339 r = extract_first_word(&s, &word, separators, flags);
345 if (!GREEDY_REALLOC(l, allocated, n + 2))
348 l[n++] = TAKE_PTR(word);
364 char *strv_join(char **l, const char *separator) {
372 k = strlen(separator);
388 e = stpcpy(e, separator);
398 #if 0 /// UNNEEDED by elogind
400 int strv_push(char ***l, char *value) {
409 /* Increase and check for overflow */
414 c = reallocarray(*l, m, sizeof(char*));
425 int strv_push_pair(char ***l, char *a, char *b) {
434 /* increase and check for overflow */
435 m = n + !!a + !!b + 1;
439 c = reallocarray(*l, m, sizeof(char*));
453 int strv_insert(char ***l, unsigned position, char *value) {
461 position = MIN(position, n);
463 /* increase and check for overflow */
472 for (i = 0; i < position; i++)
475 for (i = position; i < n; i++)
486 int strv_consume(char ***l, char *value) {
489 r = strv_push(l, value);
496 #if 0 /// UNNEEDED by elogind
497 int strv_consume_pair(char ***l, char *a, char *b) {
500 r = strv_push_pair(l, a, b);
510 int strv_consume_prepend(char ***l, char *value) {
513 r = strv_push_prepend(l, value);
520 int strv_extend(char ***l, const char *value) {
530 return strv_consume(l, v);
533 int strv_extend_front(char ***l, const char *value) {
539 /* Like strv_extend(), but prepends rather than appends the new entry */
546 /* Increase and overflow check. */
555 c = reallocarray(*l, m, sizeof(char*));
561 memmove(c+1, c, n * sizeof(char*));
569 char **strv_uniq(char **l) {
572 /* Drops duplicate entries. The first identical string will be
573 * kept, the others dropped */
576 strv_remove(i+1, *i);
581 #if 0 /// UNNEEDED by elogind
582 bool strv_is_uniq(char **l) {
586 if (strv_find(i+1, *i))
593 char **strv_remove(char **l, const char *s) {
601 /* Drops every occurrence of s in the string list, edits
604 for (f = t = l; *f; f++)
614 char **strv_parse_nulstr(const char *s, size_t l) {
615 /* l is the length of the input data, which will be split at NULs into
616 * elements of the resulting strv. Hence, the number of items in the resulting strv
617 * will be equal to one plus the number of NUL bytes in the l bytes starting at s,
618 * unless s[l-1] is NUL, in which case the final empty string is not stored in
619 * the resulting strv, and length is equal to the number of NUL bytes.
621 * Note that contrary to a normal nulstr which cannot contain empty strings, because
622 * the input data is terminated by any two consequent NUL bytes, this parser accepts
623 * empty strings in s.
627 unsigned c = 0, i = 0;
633 return new0(char*, 1);
635 for (p = s; p < s + l; p++)
642 v = new0(char*, c+1);
650 e = memchr(p, 0, s + l - p);
652 v[i] = strndup(p, e ? e - p : s + l - p);
671 char **strv_split_nulstr(const char *s) {
676 if (strv_extend(&r, i) < 0) {
682 return strv_new(NULL, NULL);
687 #if 0 /// UNNEEDED by elogind
688 int strv_make_nulstr(char **l, char **p, size_t *q) {
689 /* A valid nulstr with two NULs at the end will be created, but
690 * q will be the length without the two trailing NULs. Thus the output
691 * string is a valid nulstr and can be iterated over using NULSTR_FOREACH,
692 * and can also be parsed by strv_parse_nulstr as long as the length
693 * is provided separately.
696 size_t n_allocated = 0, n = 0;
697 _cleanup_free_ char *m = NULL;
708 if (!GREEDY_REALLOC(m, n_allocated, n + z + 2))
711 memcpy(m + n, *i, z + 1);
721 /* make sure there is a second extra NUL at the end of resulting nulstr */
733 bool strv_overlap(char **a, char **b) {
737 if (strv_contains(b, *i))
744 static int str_compare(const void *_a, const void *_b) {
745 const char **a = (const char**) _a, **b = (const char**) _b;
747 return strcmp(*a, *b);
750 char **strv_sort(char **l) {
751 qsort_safe(l, strv_length(l), sizeof(char*), str_compare);
755 bool strv_equal(char **a, char **b) {
758 return strv_isempty(b);
763 for ( ; *a || *b; ++a, ++b)
764 if (!streq_ptr(*a, *b))
770 void strv_print(char **l) {
777 #if 0 /// UNNEEDED by elogind
778 int strv_extendf(char ***l, const char *format, ...) {
783 va_start(ap, format);
784 r = vasprintf(&x, format, ap);
790 return strv_consume(l, x);
793 char **strv_reverse(char **l) {
800 for (i = 0; i < n / 2; i++)
801 SWAP_TWO(l[i], l[n-1-i]);
806 char **strv_shell_escape(char **l, const char *bad) {
809 /* Escapes every character in every string in l that is in bad,
810 * edits in-place, does not roll-back on error. */
815 v = shell_escape(*s, bad);
826 bool strv_fnmatch(char* const* patterns, const char *s, int flags) {
829 STRV_FOREACH(p, patterns)
830 if (fnmatch(*p, s, flags) == 0)
836 char ***strv_free_free(char ***l) {
848 char **strv_skip(char **l, size_t n) {
861 int strv_extend_n(char ***l, const char *value, size_t n) {
872 /* Adds the value n times to l */
876 nl = reallocarray(*l, k + n + 1, sizeof(char *));
882 for (i = k; i < k + n; i++) {
883 nl[i] = strdup(value);
892 for (j = k; j < i; j++)
899 #if 0 /// UNNEEDED by elogind
900 int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
905 /* Like fputs(), but for strv, and with a less stupid argument order */
911 r = fputs_with_space(f, *s, separator, space);