]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/strv.c
3 * Copyright 2010 Lennart Poettering
5 * This is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 of the License, or
8 * (at your option) any later version.
11 * Copyright (C) 2015 Karel Zak <kzak@redhat.com>
12 * Modified the original version from systemd project for util-linux.
25 void strv_clear(char **l
) {
37 char **strv_free(char **l
) {
43 char **strv_copy(char * const *l
) {
46 k
= r
= malloc(sizeof(char *) * (strv_length(l
) + 1));
51 for (; *l
; k
++, l
++) {
63 unsigned strv_length(char * const *l
) {
75 char **strv_new_ap(const char *x
, va_list ap
) {
78 unsigned n
= 0, i
= 0;
81 /* As a special trick we ignore all listed strings that equal
82 * (const char*) -1. This is supposed to be used with the
83 * STRV_IFNOTNULL() macro to include possibly NULL strings in
87 n
= x
== (const char*) -1 ? 0 : 1;
90 while ((s
= va_arg(aq
, const char*))) {
91 if (s
== (const char*) -1)
100 a
= malloc(sizeof(char *) * (n
+ 1));
105 if (x
!= (const char*) -1) {
112 while ((s
= va_arg(ap
, const char*))) {
114 if (s
== (const char*) -1)
134 char **strv_new(const char *x
, ...) {
139 r
= strv_new_ap(x
, ap
);
145 int strv_extend_strv(char ***a
, char **b
) {
150 r
= strv_extend(a
, *s
);
158 int strv_extend_strv_concat(char ***a
, char **b
, const char *suffix
) {
165 v
= strappend(*s
, suffix
);
180 #define _FOREACH_WORD(word, length, s, separator, quoted, state) \
181 for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
183 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
184 _FOREACH_WORD(word, length, s, separator, false, state)
187 char **strv_split(const char *s
, const char *separator
) {
188 const char *word
, *state
;
196 FOREACH_WORD_SEPARATOR(word
, l
, s
, separator
, state
)
199 r
= malloc(sizeof(char *) * (n
+ 1));
204 FOREACH_WORD_SEPARATOR(word
, l
, s
, separator
, state
) {
205 r
[i
] = strndup(word
, l
);
218 char *strv_join(char **l
, const char *separator
) {
226 k
= strlen(separator
);
242 e
= stpcpy(e
, separator
);
252 int strv_push(char ***l
, char *value
) {
261 /* Increase and check for overflow */
266 c
= realloc(*l
, sizeof(char *) * m
);
277 int strv_push_prepend(char ***l
, char *value
) {
286 /* increase and check for overflow */
291 c
= malloc(sizeof(char *) * m
);
295 for (i
= 0; i
< n
; i
++)
307 int strv_consume(char ***l
, char *value
) {
310 r
= strv_push(l
, value
);
317 int strv_consume_prepend(char ***l
, char *value
) {
320 r
= strv_push_prepend(l
, value
);
327 int strv_extend(char ***l
, const char *value
) {
337 return strv_consume(l
, v
);
340 char **strv_remove(char **l
, const char *s
) {
348 /* Drops every occurrence of s in the string list, edits
351 for (f
= t
= l
; *f
; f
++)
352 if (strcmp(*f
, s
) == 0)
361 int strv_extendf(char ***l
, const char *format
, ...) {
366 va_start(ap
, format
);
367 r
= vasprintf(&x
, format
, ap
);
373 return strv_consume(l
, x
);
376 int strv_extendv(char ***l
, const char *format
, va_list ap
) {
380 r
= vasprintf(&x
, format
, ap
);
384 return strv_consume(l
, x
);
387 char **strv_reverse(char **l
) {
394 for (i
= 0; i
< n
/ 2; i
++) {