]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/strv.c
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 int strv_extend_strv(char ***a
, char **b
, bool filter_duplicates
) {
206 size_t p
, q
, i
= 0, j
;
216 t
= realloc(*a
, sizeof(char*) * (p
+ q
+ 1));
225 if (filter_duplicates
&& strv_contains(t
, *s
))
241 for (j
= 0; j
< i
; j
++)
248 int strv_extend_strv_concat(char ***a
, char **b
, const char *suffix
) {
255 v
= strappend(*s
, suffix
);
269 char **strv_split(const char *s
, const char *separator
) {
270 const char *word
, *state
;
278 FOREACH_WORD_SEPARATOR(word
, l
, s
, separator
, state
)
286 FOREACH_WORD_SEPARATOR(word
, l
, s
, separator
, state
) {
287 r
[i
] = strndup(word
, l
);
300 char **strv_split_newlines(const char *s
) {
306 /* Special version of strv_split() that splits on newlines and
307 * suppresses an empty string at the end */
309 l
= strv_split(s
, NEWLINE
);
317 if (isempty(l
[n
- 1]))
318 l
[n
- 1] = mfree(l
[n
- 1]);
323 int strv_split_extract(char ***t
, const char *s
, const char *separators
, ExtractFlags flags
) {
324 _cleanup_strv_free_
char **l
= NULL
;
325 size_t n
= 0, allocated
= 0;
332 _cleanup_free_
char *word
= NULL
;
334 r
= extract_first_word(&s
, &word
, separators
, flags
);
340 if (!GREEDY_REALLOC(l
, allocated
, n
+ 2))
361 char *strv_join(char **l
, const char *separator
) {
369 k
= strlen(separator
);
385 e
= stpcpy(e
, separator
);
395 char *strv_join_quoted(char **l
) {
398 size_t allocated
= 0, len
= 0;
401 /* assuming here that escaped string cannot be more
402 * than twice as long, and reserving space for the
403 * separator and quotes.
405 _cleanup_free_
char *esc
= NULL
;
408 if (!GREEDY_REALLOC(buf
, allocated
,
409 len
+ strlen(*s
) * 2 + 3))
416 needed
= snprintf(buf
+ len
, allocated
- len
, "%s\"%s\"",
417 len
> 0 ? " " : "", esc
);
418 assert(needed
< allocated
- len
);
431 int strv_push(char ***l
, char *value
) {
440 /* Increase and check for overflow */
445 c
= realloc_multiply(*l
, sizeof(char*), m
);
456 int strv_push_pair(char ***l
, char *a
, char *b
) {
465 /* increase and check for overflow */
466 m
= n
+ !!a
+ !!b
+ 1;
470 c
= realloc_multiply(*l
, sizeof(char*), m
);
484 int strv_push_prepend(char ***l
, char *value
) {
493 /* increase and check for overflow */
502 for (i
= 0; i
< n
; i
++)
514 int strv_consume(char ***l
, char *value
) {
517 r
= strv_push(l
, value
);
524 int strv_consume_pair(char ***l
, char *a
, char *b
) {
527 r
= strv_push_pair(l
, a
, b
);
536 int strv_consume_prepend(char ***l
, char *value
) {
539 r
= strv_push_prepend(l
, value
);
546 int strv_extend(char ***l
, const char *value
) {
556 return strv_consume(l
, v
);
559 int strv_extend_front(char ***l
, const char *value
) {
565 /* Like strv_extend(), but prepends rather than appends the new entry */
572 /* Increase and overflow check. */
581 c
= realloc_multiply(*l
, sizeof(char*), m
);
587 memmove(c
+1, c
, n
* sizeof(char*));
595 char **strv_uniq(char **l
) {
598 /* Drops duplicate entries. The first identical string will be
599 * kept, the others dropped */
602 strv_remove(i
+1, *i
);
607 bool strv_is_uniq(char **l
) {
611 if (strv_find(i
+1, *i
))
617 char **strv_remove(char **l
, const char *s
) {
625 /* Drops every occurrence of s in the string list, edits
628 for (f
= t
= l
; *f
; f
++)
638 char **strv_parse_nulstr(const char *s
, size_t l
) {
639 /* l is the length of the input data, which will be split at NULs into
640 * elements of the resulting strv. Hence, the number of items in the resulting strv
641 * will be equal to one plus the number of NUL bytes in the l bytes starting at s,
642 * unless s[l-1] is NUL, in which case the final empty string is not stored in
643 * the resulting strv, and length is equal to the number of NUL bytes.
645 * Note that contrary to a normal nulstr which cannot contain empty strings, because
646 * the input data is terminated by any two consequent NUL bytes, this parser accepts
647 * empty strings in s.
651 unsigned c
= 0, i
= 0;
657 return new0(char*, 1);
659 for (p
= s
; p
< s
+ l
; p
++)
666 v
= new0(char*, c
+1);
674 e
= memchr(p
, 0, s
+ l
- p
);
676 v
[i
] = strndup(p
, e
? e
- p
: s
+ l
- p
);
695 char **strv_split_nulstr(const char *s
) {
700 if (strv_extend(&r
, i
) < 0) {
706 return strv_new(NULL
, NULL
);
711 int strv_make_nulstr(char **l
, char **p
, size_t *q
) {
712 /* A valid nulstr with two NULs at the end will be created, but
713 * q will be the length without the two trailing NULs. Thus the output
714 * string is a valid nulstr and can be iterated over using NULSTR_FOREACH,
715 * and can also be parsed by strv_parse_nulstr as long as the length
716 * is provided separately.
719 size_t n_allocated
= 0, n
= 0;
720 _cleanup_free_
char *m
= NULL
;
731 if (!GREEDY_REALLOC(m
, n_allocated
, n
+ z
+ 2))
734 memcpy(m
+ n
, *i
, z
+ 1);
744 /* make sure there is a second extra NUL at the end of resulting nulstr */
756 bool strv_overlap(char **a
, char **b
) {
760 if (strv_contains(b
, *i
))
766 static int str_compare(const void *_a
, const void *_b
) {
767 const char **a
= (const char**) _a
, **b
= (const char**) _b
;
769 return strcmp(*a
, *b
);
772 char **strv_sort(char **l
) {
777 qsort(l
, strv_length(l
), sizeof(char*), str_compare
);
781 bool strv_equal(char **a
, char **b
) {
784 return strv_isempty(b
);
789 for ( ; *a
|| *b
; ++a
, ++b
)
790 if (!streq_ptr(*a
, *b
))
796 void strv_print(char **l
) {
803 int strv_extendf(char ***l
, const char *format
, ...) {
808 va_start(ap
, format
);
809 r
= vasprintf(&x
, format
, ap
);
815 return strv_consume(l
, x
);
818 char **strv_reverse(char **l
) {
825 for (i
= 0; i
< n
/ 2; i
++)
826 SWAP_TWO(l
[i
], l
[n
-1-i
]);
831 char **strv_shell_escape(char **l
, const char *bad
) {
834 /* Escapes every character in every string in l that is in bad,
835 * edits in-place, does not roll-back on error. */
840 v
= shell_escape(*s
, bad
);
851 bool strv_fnmatch(char* const* patterns
, const char *s
, int flags
) {
854 STRV_FOREACH(p
, patterns
)
855 if (fnmatch(*p
, s
, flags
) == 0)
861 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 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
);