]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/strv.c
lscpu: define libsmartcols flags for -e
[thirdparty/util-linux.git] / lib / strv.c
1 /*
2 *
3 * Copyright 2010 Lennart Poettering
4 *
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.
9 *
10 *
11 * Copyright (C) 2015 Karel Zak <kzak@redhat.com>
12 * Modified the original version from systemd project for util-linux.
13 */
14
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <stdbool.h>
20 #include <assert.h>
21
22 #include "strutils.h"
23 #include "strv.h"
24
25 void strv_clear(char **l) {
26 char **k;
27
28 if (!l)
29 return;
30
31 for (k = l; *k; k++)
32 free(*k);
33
34 *l = NULL;
35 }
36
37 char **strv_free(char **l) {
38 strv_clear(l);
39 free(l);
40 return NULL;
41 }
42
43 char **strv_copy(char * const *l) {
44 char **r, **k;
45
46 k = r = malloc(sizeof(char *) * (strv_length(l) + 1));
47 if (!r)
48 return NULL;
49
50 if (l)
51 for (; *l; k++, l++) {
52 *k = strdup(*l);
53 if (!*k) {
54 strv_free(r);
55 return NULL;
56 }
57 }
58
59 *k = NULL;
60 return r;
61 }
62
63 unsigned strv_length(char * const *l) {
64 unsigned n = 0;
65
66 if (!l)
67 return 0;
68
69 for (; *l; l++)
70 n++;
71
72 return n;
73 }
74
75 char **strv_new_ap(const char *x, va_list ap) {
76 const char *s;
77 char **a;
78 unsigned n = 0, i = 0;
79 va_list aq;
80
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
84 * the string list. */
85
86 if (x) {
87 n = x == (const char*) -1 ? 0 : 1;
88
89 va_copy(aq, ap);
90 while ((s = va_arg(aq, const char*))) {
91 if (s == (const char*) -1)
92 continue;
93
94 n++;
95 }
96
97 va_end(aq);
98 }
99
100 a = malloc(sizeof(char *) * (n + 1));
101 if (!a)
102 return NULL;
103
104 if (x) {
105 if (x != (const char*) -1) {
106 a[i] = strdup(x);
107 if (!a[i])
108 goto fail;
109 i++;
110 }
111
112 while ((s = va_arg(ap, const char*))) {
113
114 if (s == (const char*) -1)
115 continue;
116
117 a[i] = strdup(s);
118 if (!a[i])
119 goto fail;
120
121 i++;
122 }
123 }
124
125 a[i] = NULL;
126
127 return a;
128
129 fail:
130 strv_free(a);
131 return NULL;
132 }
133
134 char **strv_new(const char *x, ...) {
135 char **r;
136 va_list ap;
137
138 va_start(ap, x);
139 r = strv_new_ap(x, ap);
140 va_end(ap);
141
142 return r;
143 }
144
145 int strv_extend_strv(char ***a, char **b) {
146 int r;
147 char **s;
148
149 STRV_FOREACH(s, b) {
150 r = strv_extend(a, *s);
151 if (r < 0)
152 return r;
153 }
154
155 return 0;
156 }
157
158 int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
159 int r;
160 char **s;
161
162 STRV_FOREACH(s, b) {
163 char *v;
164
165 v = strappend(*s, suffix);
166 if (!v)
167 return -ENOMEM;
168
169 r = strv_push(a, v);
170 if (r < 0) {
171 free(v);
172 return r;
173 }
174 }
175
176 return 0;
177 }
178
179
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)))
182
183 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
184 _FOREACH_WORD(word, length, s, separator, false, state)
185
186
187 char **strv_split(const char *s, const char *separator) {
188 const char *word, *state;
189 size_t l;
190 unsigned n, i;
191 char **r;
192
193 assert(s);
194
195 n = 0;
196 FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
197 n++;
198
199 r = malloc(sizeof(char *) * (n + 1));
200 if (!r)
201 return NULL;
202
203 i = 0;
204 FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
205 r[i] = strndup(word, l);
206 if (!r[i]) {
207 strv_free(r);
208 return NULL;
209 }
210
211 i++;
212 }
213
214 r[i] = NULL;
215 return r;
216 }
217
218 char *strv_join(char **l, const char *separator) {
219 char *r, *e;
220 char **s;
221 size_t n, k;
222
223 if (!separator)
224 separator = " ";
225
226 k = strlen(separator);
227
228 n = 0;
229 STRV_FOREACH(s, l) {
230 if (n != 0)
231 n += k;
232 n += strlen(*s);
233 }
234
235 r = malloc(n + 1);
236 if (!r)
237 return NULL;
238
239 e = r;
240 STRV_FOREACH(s, l) {
241 if (e != r)
242 e = stpcpy(e, separator);
243
244 e = stpcpy(e, *s);
245 }
246
247 *e = 0;
248
249 return r;
250 }
251
252 int strv_push(char ***l, char *value) {
253 char **c;
254 unsigned n, m;
255
256 if (!value)
257 return 0;
258
259 n = strv_length(*l);
260
261 /* Increase and check for overflow */
262 m = n + 2;
263 if (m < n)
264 return -ENOMEM;
265
266 c = realloc(*l, sizeof(char *) * m);
267 if (!c)
268 return -ENOMEM;
269
270 c[n] = value;
271 c[n+1] = NULL;
272
273 *l = c;
274 return 0;
275 }
276
277 int strv_push_prepend(char ***l, char *value) {
278 char **c;
279 unsigned n, m, i;
280
281 if (!value)
282 return 0;
283
284 n = strv_length(*l);
285
286 /* increase and check for overflow */
287 m = n + 2;
288 if (m < n)
289 return -ENOMEM;
290
291 c = malloc(sizeof(char *) * m);
292 if (!c)
293 return -ENOMEM;
294
295 for (i = 0; i < n; i++)
296 c[i+1] = (*l)[i];
297
298 c[0] = value;
299 c[n+1] = NULL;
300
301 free(*l);
302 *l = c;
303
304 return 0;
305 }
306
307 int strv_consume(char ***l, char *value) {
308 int r;
309
310 r = strv_push(l, value);
311 if (r < 0)
312 free(value);
313
314 return r;
315 }
316
317 int strv_consume_prepend(char ***l, char *value) {
318 int r;
319
320 r = strv_push_prepend(l, value);
321 if (r < 0)
322 free(value);
323
324 return r;
325 }
326
327 int strv_extend(char ***l, const char *value) {
328 char *v;
329
330 if (!value)
331 return 0;
332
333 v = strdup(value);
334 if (!v)
335 return -ENOMEM;
336
337 return strv_consume(l, v);
338 }
339
340 char **strv_remove(char **l, const char *s) {
341 char **f, **t;
342
343 if (!l)
344 return NULL;
345
346 assert(s);
347
348 /* Drops every occurrence of s in the string list, edits
349 * in-place. */
350
351 for (f = t = l; *f; f++)
352 if (strcmp(*f, s) == 0)
353 free(*f);
354 else
355 *(t++) = *f;
356
357 *t = NULL;
358 return l;
359 }
360
361 int strv_extendf(char ***l, const char *format, ...) {
362 va_list ap;
363 char *x;
364 int r;
365
366 va_start(ap, format);
367 r = vasprintf(&x, format, ap);
368 va_end(ap);
369
370 if (r < 0)
371 return -ENOMEM;
372
373 return strv_consume(l, x);
374 }
375
376 int strv_extendv(char ***l, const char *format, va_list ap) {
377 char *x;
378 int r;
379
380 r = vasprintf(&x, format, ap);
381 if (r < 0)
382 return -ENOMEM;
383
384 return strv_consume(l, x);
385 }
386
387 char **strv_reverse(char **l) {
388 unsigned n, i;
389
390 n = strv_length(l);
391 if (n <= 1)
392 return l;
393
394 for (i = 0; i < n / 2; i++) {
395 char *t;
396
397 t = l[i];
398 l[i] = l[n-1-i];
399 l[n-1-i] = t;
400 }
401
402 return l;
403 }