]>
git.ipfire.org Git - thirdparty/dracut.git/blob - install/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/>.
29 char *strv_find(char **l
, const char *name
) {
41 char *strv_find_prefix(char **l
, const char *name
) {
47 if (startswith(*i
, name
))
53 void strv_free(char **l
) {
65 char **strv_copy(char * const *l
) {
68 k
= r
= new(char*, strv_length(l
) + 1);
73 for (; *l
; k
++, l
++) {
85 unsigned int strv_length(char * const *l
) {
97 char **strv_new_ap(const char *x
, va_list ap
) {
100 unsigned n
= 0, i
= 0;
103 /* As a special trick we ignore all listed strings that equal
104 * (const char*) -1. This is supposed to be used with the
105 * STRV_IFNOTNULL() macro to include possibly NULL strings in
106 * the string list. */
109 n
= x
== (const char*) -1 ? 0 : 1;
112 while ((s
= va_arg(aq
, const char*))) {
113 if (s
== (const char*) -1)
127 if (x
!= (const char*) -1) {
134 while ((s
= va_arg(ap
, const char*))) {
136 if (s
== (const char*) -1)
156 char **strv_new(const char *x
, ...) {
161 r
= strv_new_ap(x
, ap
);
167 char **strv_merge(char **a
, char **b
) {
176 r
= new(char*, strv_length(a
) + strv_length(b
) + 1);
180 for (k
= r
; *a
; k
++, a
++) {
186 for (; *b
; k
++, b
++) {
200 char **strv_merge_concat(char **a
, char **b
, const char *suffix
) {
203 /* Like strv_merge(), but appends suffix to all strings in b, before adding */
208 r
= new(char*, strv_length(a
) + strv_length(b
) + 1);
214 for (; *a
; k
++, a
++) {
220 for (; *b
; k
++, b
++) {
221 *k
= strappend(*b
, suffix
);
235 char **strv_split(const char *s
, const char *separator
) {
245 FOREACH_WORD_SEPARATOR(w
, l
, s
, separator
, state
)
253 FOREACH_WORD_SEPARATOR(w
, l
, s
, separator
, state
) {
254 r
[i
] = strndup(w
, l
);
267 char **strv_split_quoted(const char *s
) {
277 FOREACH_WORD_QUOTED(w
, l
, s
, state
)
285 FOREACH_WORD_QUOTED(w
, l
, s
, state
) {
286 r
[i
] = cunescape_length(w
, l
);
298 char **strv_split_newlines(const char *s
) {
304 /* Special version of strv_split() that splits on newlines and
305 * suppresses an empty string at the end */
307 l
= strv_split(s
, NEWLINE
);
315 if (isempty(l
[n
-1])) {
323 char *strv_join(char **l
, const char *separator
) {
331 k
= strlen(separator
);
347 e
= stpcpy(e
, separator
);
357 char **strv_append(char **l
, const char *s
) {
361 return strv_new(s
, NULL
);
366 r
= new(char*, strv_length(l
)+2);
370 for (k
= r
; *l
; k
++, l
++) {
388 int strv_push(char ***l
, char *value
) {
396 c
= realloc(*l
, sizeof(char*) * (n
+ 2));
407 int strv_extend(char ***l
, const char *value
) {
425 char **strv_uniq(char **l
) {
428 /* Drops duplicate entries. The first identical string will be
429 * kept, the others dropped */
432 strv_remove(i
+1, *i
);
437 char **strv_remove(char **l
, const char *s
) {
445 /* Drops every occurrence of s in the string list, edits
448 for (f
= t
= l
; *f
; f
++) {
462 char **strv_remove_prefix(char **l
, const char *s
) {
470 /* Drops every occurrence of a string prefixed with s in the
471 * string list, edits in-place. */
473 for (f
= t
= l
; *f
; f
++) {
475 if (startswith(*f
, s
)) {
487 char **strv_parse_nulstr(const char *s
, size_t l
) {
489 unsigned c
= 0, i
= 0;
495 return new0(char*, 1);
497 for (p
= s
; p
< s
+ l
; p
++)
504 v
= new0(char*, c
+1);
512 e
= memchr(p
, 0, s
+ l
- p
);
514 v
[i
] = strndup(p
, e
? e
- p
: s
+ l
- p
);
533 char **strv_split_nulstr(const char *s
) {
538 if (strv_extend(&r
, i
) < 0) {
544 return strv_new(NULL
, NULL
);
549 bool strv_overlap(char **a
, char **b
) {
562 static int str_compare(const void *_a
, const void *_b
) {
563 const char **a
= (const char**) _a
, **b
= (const char**) _b
;
565 return strcmp(*a
, *b
);
568 char **strv_sort(char **l
) {
573 qsort(l
, strv_length(l
), sizeof(char*), str_compare
);
577 void strv_print(char **l
) {