]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
60918275 | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
60918275 LP |
22 | #include <assert.h> |
23 | #include <stdlib.h> | |
24 | #include <stdarg.h> | |
25 | #include <string.h> | |
2e6c9e6b | 26 | #include <errno.h> |
60918275 LP |
27 | |
28 | #include "util.h" | |
29 | #include "strv.h" | |
30 | ||
31 | char *strv_find(char **l, const char *name) { | |
5f9a22c3 LP |
32 | char **i; |
33 | ||
60918275 LP |
34 | assert(name); |
35 | ||
5f9a22c3 LP |
36 | STRV_FOREACH(i, l) |
37 | if (streq(*i, name)) | |
38 | return *i; | |
60918275 LP |
39 | |
40 | return NULL; | |
41 | } | |
42 | ||
a4bfb399 LP |
43 | char *strv_find_prefix(char **l, const char *name) { |
44 | char **i; | |
45 | ||
46 | assert(name); | |
47 | ||
48 | STRV_FOREACH(i, l) | |
49 | if (startswith(*i, name)) | |
50 | return *i; | |
51 | ||
52 | return NULL; | |
53 | } | |
54 | ||
60918275 LP |
55 | void strv_free(char **l) { |
56 | char **k; | |
57 | ||
58 | if (!l) | |
59 | return; | |
60 | ||
61 | for (k = l; *k; k++) | |
62 | free(*k); | |
63 | ||
64 | free(l); | |
65 | } | |
66 | ||
67 | char **strv_copy(char **l) { | |
68 | char **r, **k; | |
69 | ||
70 | if (!(r = new(char*, strv_length(l)+1))) | |
71 | return NULL; | |
72 | ||
73 | for (k = r; *l; k++, l++) | |
74 | if (!(*k = strdup(*l))) | |
75 | goto fail; | |
76 | ||
77 | *k = NULL; | |
78 | return r; | |
79 | ||
80 | fail: | |
da19d5c1 | 81 | for (k--; k >= r; k--) |
60918275 LP |
82 | free(*k); |
83 | ||
da19d5c1 LP |
84 | free(r); |
85 | ||
60918275 LP |
86 | return NULL; |
87 | } | |
88 | ||
89 | unsigned strv_length(char **l) { | |
90 | unsigned n = 0; | |
91 | ||
92 | if (!l) | |
93 | return 0; | |
94 | ||
95 | for (; *l; l++) | |
96 | n++; | |
97 | ||
98 | return n; | |
99 | } | |
100 | ||
257eca1a | 101 | char **strv_new_ap(const char *x, va_list ap) { |
60918275 LP |
102 | const char *s; |
103 | char **a; | |
104 | unsigned n = 0, i = 0; | |
257eca1a LP |
105 | va_list aq; |
106 | ||
60918275 LP |
107 | |
108 | if (x) { | |
109 | n = 1; | |
110 | ||
257eca1a LP |
111 | va_copy(aq, ap); |
112 | while (va_arg(aq, const char*)) | |
60918275 | 113 | n++; |
257eca1a | 114 | va_end(aq); |
60918275 LP |
115 | } |
116 | ||
117 | if (!(a = new(char*, n+1))) | |
118 | return NULL; | |
119 | ||
120 | if (x) { | |
121 | if (!(a[i] = strdup(x))) { | |
122 | free(a); | |
123 | return NULL; | |
124 | } | |
125 | ||
126 | i++; | |
127 | ||
60918275 LP |
128 | while ((s = va_arg(ap, const char*))) { |
129 | if (!(a[i] = strdup(s))) | |
130 | goto fail; | |
131 | ||
132 | i++; | |
133 | } | |
60918275 LP |
134 | } |
135 | ||
136 | a[i] = NULL; | |
257eca1a | 137 | |
60918275 LP |
138 | return a; |
139 | ||
140 | fail: | |
141 | ||
142 | for (; i > 0; i--) | |
143 | if (a[i-1]) | |
144 | free(a[i-1]); | |
145 | ||
146 | free(a); | |
257eca1a | 147 | |
60918275 LP |
148 | return NULL; |
149 | } | |
034c6ed7 | 150 | |
257eca1a LP |
151 | char **strv_new(const char *x, ...) { |
152 | char **r; | |
153 | va_list ap; | |
154 | ||
155 | va_start(ap, x); | |
156 | r = strv_new_ap(x, ap); | |
157 | va_end(ap); | |
158 | ||
159 | return r; | |
160 | } | |
161 | ||
034c6ed7 LP |
162 | char **strv_merge(char **a, char **b) { |
163 | char **r, **k; | |
164 | ||
165 | if (!a) | |
166 | return strv_copy(b); | |
167 | ||
168 | if (!b) | |
169 | return strv_copy(a); | |
170 | ||
171 | if (!(r = new(char*, strv_length(a)+strv_length(b)+1))) | |
172 | return NULL; | |
173 | ||
174 | for (k = r; *a; k++, a++) | |
175 | if (!(*k = strdup(*a))) | |
176 | goto fail; | |
177 | for (; *b; k++, b++) | |
178 | if (!(*k = strdup(*b))) | |
179 | goto fail; | |
180 | ||
181 | *k = NULL; | |
182 | return r; | |
183 | ||
184 | fail: | |
185 | for (k--; k >= r; k--) | |
186 | free(*k); | |
187 | ||
100a76ee LP |
188 | free(r); |
189 | ||
034c6ed7 | 190 | return NULL; |
5f9a22c3 LP |
191 | } |
192 | ||
193 | char **strv_merge_concat(char **a, char **b, const char *suffix) { | |
194 | char **r, **k; | |
195 | ||
196 | /* Like strv_merge(), but appends suffix to all strings in b, before adding */ | |
197 | ||
198 | if (!b) | |
199 | return strv_copy(a); | |
200 | ||
201 | if (!(r = new(char*, strv_length(a)+strv_length(b)+1))) | |
202 | return NULL; | |
203 | ||
204 | for (k = r; *a; k++, a++) | |
205 | if (!(*k = strdup(*a))) | |
206 | goto fail; | |
207 | for (; *b; k++, b++) | |
208 | if (!(*k = strappend(*b, suffix))) | |
209 | goto fail; | |
210 | ||
211 | *k = NULL; | |
212 | return r; | |
213 | ||
214 | fail: | |
215 | for (k--; k >= r; k--) | |
216 | free(*k); | |
217 | ||
100a76ee LP |
218 | free(r); |
219 | ||
5f9a22c3 LP |
220 | return NULL; |
221 | ||
222 | } | |
223 | ||
224 | char **strv_split(const char *s, const char *separator) { | |
225 | char *state; | |
226 | char *w; | |
227 | size_t l; | |
228 | unsigned n, i; | |
229 | char **r; | |
230 | ||
231 | assert(s); | |
232 | ||
233 | n = 0; | |
234 | FOREACH_WORD_SEPARATOR(w, l, s, separator, state) | |
235 | n++; | |
236 | ||
237 | if (!(r = new(char*, n+1))) | |
238 | return NULL; | |
239 | ||
240 | i = 0; | |
241 | FOREACH_WORD_SEPARATOR(w, l, s, separator, state) | |
242 | if (!(r[i++] = strndup(w, l))) { | |
243 | strv_free(r); | |
244 | return NULL; | |
245 | } | |
246 | ||
247 | r[i] = NULL; | |
248 | return r; | |
249 | } | |
250 | ||
251 | char **strv_split_quoted(const char *s) { | |
252 | char *state; | |
253 | char *w; | |
254 | size_t l; | |
255 | unsigned n, i; | |
256 | char **r; | |
257 | ||
258 | assert(s); | |
259 | ||
260 | n = 0; | |
261 | FOREACH_WORD_QUOTED(w, l, s, state) | |
262 | n++; | |
263 | ||
264 | if (!(r = new(char*, n+1))) | |
265 | return NULL; | |
266 | ||
267 | i = 0; | |
268 | FOREACH_WORD_QUOTED(w, l, s, state) | |
f60f22df | 269 | if (!(r[i++] = cunescape_length(w, l))) { |
5f9a22c3 LP |
270 | strv_free(r); |
271 | return NULL; | |
272 | } | |
273 | ||
274 | r[i] = NULL; | |
275 | return r; | |
276 | } | |
277 | ||
278 | char *strv_join(char **l, const char *separator) { | |
279 | char *r, *e; | |
280 | char **s; | |
281 | size_t n, k; | |
282 | ||
283 | if (!separator) | |
284 | separator = " "; | |
285 | ||
286 | k = strlen(separator); | |
287 | ||
288 | n = 0; | |
289 | STRV_FOREACH(s, l) { | |
290 | if (n != 0) | |
291 | n += k; | |
292 | n += strlen(*s); | |
293 | } | |
294 | ||
295 | if (!(r = new(char, n+1))) | |
296 | return NULL; | |
297 | ||
298 | e = r; | |
299 | STRV_FOREACH(s, l) { | |
300 | if (e != r) | |
301 | e = stpcpy(e, separator); | |
302 | ||
303 | e = stpcpy(e, *s); | |
304 | } | |
305 | ||
8d49745c LP |
306 | *e = 0; |
307 | ||
5f9a22c3 LP |
308 | return r; |
309 | } | |
310 | ||
311 | char **strv_append(char **l, const char *s) { | |
312 | char **r, **k; | |
313 | ||
314 | if (!l) | |
315 | return strv_new(s, NULL); | |
316 | ||
317 | if (!s) | |
318 | return strv_copy(l); | |
319 | ||
320 | if (!(r = new(char*, strv_length(l)+2))) | |
321 | return NULL; | |
034c6ed7 | 322 | |
5f9a22c3 LP |
323 | for (k = r; *l; k++, l++) |
324 | if (!(*k = strdup(*l))) | |
325 | goto fail; | |
2e6c9e6b | 326 | |
5f9a22c3 LP |
327 | if (!(*(k++) = strdup(s))) |
328 | goto fail; | |
329 | ||
330 | *k = NULL; | |
331 | return r; | |
332 | ||
333 | fail: | |
334 | for (k--; k >= r; k--) | |
335 | free(*k); | |
336 | ||
100a76ee LP |
337 | free(r); |
338 | ||
5f9a22c3 | 339 | return NULL; |
034c6ed7 | 340 | } |
cba8922f | 341 | |
5f9a22c3 | 342 | char **strv_uniq(char **l) { |
cba8922f LP |
343 | char **i; |
344 | ||
5f9a22c3 LP |
345 | /* Drops duplicate entries. The first identical string will be |
346 | * kept, the others dropped */ | |
347 | ||
cba8922f | 348 | STRV_FOREACH(i, l) |
5f9a22c3 LP |
349 | strv_remove(i+1, *i); |
350 | ||
351 | return l; | |
352 | } | |
353 | ||
354 | char **strv_remove(char **l, const char *s) { | |
355 | char **f, **t; | |
356 | ||
357 | if (!l) | |
358 | return NULL; | |
359 | ||
35b8ca3a | 360 | /* Drops every occurrence of s in the string list */ |
5f9a22c3 LP |
361 | |
362 | for (f = t = l; *f; f++) { | |
363 | ||
364 | if (streq(*f, s)) { | |
365 | free(*f); | |
366 | continue; | |
367 | } | |
368 | ||
369 | *(t++) = *f; | |
370 | } | |
cba8922f | 371 | |
5f9a22c3 LP |
372 | *t = NULL; |
373 | return l; | |
cba8922f | 374 | } |
2e6c9e6b LP |
375 | |
376 | static int env_append(char **r, char ***k, char **a) { | |
377 | assert(r); | |
378 | assert(k); | |
5b6319dc LP |
379 | |
380 | if (!a) | |
381 | return 0; | |
2e6c9e6b LP |
382 | |
383 | /* Add the entries of a to *k unless they already exist in *r | |
35b8ca3a | 384 | * in which case they are overridden instead. This assumes |
a6ff950e | 385 | * there is enough space in the r array. */ |
2e6c9e6b LP |
386 | |
387 | for (; *a; a++) { | |
388 | char **j; | |
389 | size_t n = strcspn(*a, "=") + 1; | |
390 | ||
391 | for (j = r; j < *k; j++) | |
392 | if (strncmp(*j, *a, n) == 0) | |
393 | break; | |
394 | ||
395 | if (j >= *k) | |
396 | (*k)++; | |
397 | else | |
398 | free(*j); | |
399 | ||
400 | if (!(*j = strdup(*a))) | |
401 | return -ENOMEM; | |
402 | } | |
403 | ||
404 | return 0; | |
405 | } | |
406 | ||
5b6319dc | 407 | char **strv_env_merge(unsigned n_lists, ...) { |
2e6c9e6b LP |
408 | size_t n = 0; |
409 | char **l, **k, **r; | |
410 | va_list ap; | |
5b6319dc | 411 | unsigned i; |
2e6c9e6b LP |
412 | |
413 | /* Merges an arbitrary number of environment sets */ | |
414 | ||
5b6319dc LP |
415 | va_start(ap, n_lists); |
416 | for (i = 0; i < n_lists; i++) { | |
417 | l = va_arg(ap, char**); | |
418 | n += strv_length(l); | |
2e6c9e6b | 419 | } |
5b6319dc | 420 | va_end(ap); |
2e6c9e6b LP |
421 | |
422 | if (!(r = new(char*, n+1))) | |
423 | return NULL; | |
424 | ||
425 | k = r; | |
426 | ||
5b6319dc LP |
427 | va_start(ap, n_lists); |
428 | for (i = 0; i < n_lists; i++) { | |
429 | l = va_arg(ap, char**); | |
430 | if (env_append(r, &k, l) < 0) | |
2e6c9e6b | 431 | goto fail; |
2e6c9e6b | 432 | } |
5b6319dc | 433 | va_end(ap); |
2e6c9e6b LP |
434 | |
435 | *k = NULL; | |
436 | ||
437 | return r; | |
438 | ||
439 | fail: | |
da19d5c1 LP |
440 | va_end(ap); |
441 | ||
2e6c9e6b LP |
442 | for (k--; k >= r; k--) |
443 | free(*k); | |
444 | ||
445 | free(r); | |
446 | ||
447 | return NULL; | |
448 | } | |
1137a57c LP |
449 | |
450 | static bool env_match(const char *t, const char *pattern) { | |
451 | assert(t); | |
452 | assert(pattern); | |
453 | ||
454 | /* pattern a matches string a | |
455 | * a matches a= | |
456 | * a matches a=b | |
457 | * a= matches a= | |
458 | * a=b matches a=b | |
459 | * a= does not match a | |
460 | * a=b does not match a= | |
461 | * a=b does not match a | |
462 | * a=b does not match a=c */ | |
463 | ||
464 | if (streq(t, pattern)) | |
465 | return true; | |
466 | ||
467 | if (!strchr(pattern, '=')) { | |
468 | size_t l = strlen(pattern); | |
469 | ||
470 | return strncmp(t, pattern, l) == 0 && t[l] == '='; | |
471 | } | |
472 | ||
473 | return false; | |
474 | } | |
475 | ||
5b6319dc | 476 | char **strv_env_delete(char **x, unsigned n_lists, ...) { |
1137a57c LP |
477 | size_t n = 0, i = 0; |
478 | char **l, **k, **r, **j; | |
479 | va_list ap; | |
480 | ||
35b8ca3a | 481 | /* Deletes every entry from x that is mentioned in the other |
1137a57c LP |
482 | * string lists */ |
483 | ||
484 | n = strv_length(x); | |
485 | ||
486 | if (!(r = new(char*, n+1))) | |
487 | return NULL; | |
488 | ||
489 | STRV_FOREACH(k, x) { | |
5b6319dc | 490 | va_start(ap, n_lists); |
1137a57c | 491 | |
5b6319dc LP |
492 | for (i = 0; i < n_lists; i++) { |
493 | l = va_arg(ap, char**); | |
1137a57c LP |
494 | STRV_FOREACH(j, l) |
495 | if (env_match(*k, *j)) | |
496 | goto delete; | |
5b6319dc | 497 | } |
1137a57c LP |
498 | |
499 | va_end(ap); | |
500 | ||
501 | if (!(r[i++] = strdup(*k))) { | |
502 | strv_free(r); | |
503 | return NULL; | |
504 | } | |
505 | ||
506 | continue; | |
507 | ||
508 | delete: | |
509 | va_end(ap); | |
510 | } | |
511 | ||
512 | r[i] = NULL; | |
513 | ||
514 | assert(i <= n); | |
515 | ||
516 | return r; | |
517 | } | |
ddb26e18 LP |
518 | |
519 | char **strv_env_set(char **x, const char *p) { | |
520 | ||
521 | char **k, **r; | |
522 | ||
523 | if (!(r = new(char*, strv_length(x)+2))) | |
524 | return NULL; | |
525 | ||
526 | k = r; | |
527 | if (env_append(r, &k, x) < 0) | |
528 | goto fail; | |
529 | ||
530 | if (!(*(k++) = strdup(p))) | |
531 | goto fail; | |
532 | ||
533 | *k = NULL; | |
534 | ||
535 | return r; | |
536 | ||
537 | fail: | |
538 | for (k--; k >= r; k--) | |
539 | free(*k); | |
540 | ||
541 | free(r); | |
542 | ||
543 | return NULL; | |
544 | ||
545 | } | |
fab56fc5 LP |
546 | |
547 | char *strv_env_get_with_length(char **l, const char *name, size_t k) { | |
548 | char **i; | |
549 | ||
550 | assert(name); | |
551 | ||
552 | STRV_FOREACH(i, l) | |
553 | if (strncmp(*i, name, k) == 0 && | |
554 | (*i)[k] == '=') | |
555 | return *i + k + 1; | |
556 | ||
557 | return NULL; | |
558 | } | |
559 | ||
560 | char *strv_env_get(char **l, const char *name) { | |
561 | return strv_env_get_with_length(l, name, strlen(name)); | |
562 | } | |
a6ff950e LP |
563 | |
564 | char **strv_env_clean(char **l) { | |
565 | char **r, **ret; | |
566 | ||
567 | for (r = ret = l; *l; l++) { | |
568 | const char *equal; | |
569 | ||
570 | equal = strchr(*l, '='); | |
571 | ||
572 | if (equal && equal[1] == 0) { | |
573 | free(*l); | |
574 | continue; | |
575 | } | |
576 | ||
577 | *(r++) = *l; | |
578 | } | |
579 | ||
580 | *r = NULL; | |
581 | ||
582 | return ret; | |
583 | } | |
21bc923a LP |
584 | |
585 | char **strv_parse_nulstr(const char *s, size_t l) { | |
586 | const char *p; | |
587 | unsigned c = 0, i = 0; | |
588 | char **v; | |
589 | ||
590 | assert(s || l <= 0); | |
591 | ||
592 | if (l <= 0) | |
593 | return strv_new(NULL, NULL); | |
594 | ||
595 | for (p = s; p < s + l; p++) | |
596 | if (*p == 0) | |
597 | c++; | |
598 | ||
599 | if (s[l-1] != 0) | |
600 | c++; | |
601 | ||
602 | if (!(v = new0(char*, c+1))) | |
603 | return NULL; | |
604 | ||
605 | p = s; | |
606 | while (p < s + l) { | |
607 | const char *e; | |
608 | ||
609 | e = memchr(p, 0, s + l - p); | |
610 | ||
611 | if (!(v[i++] = strndup(p, e ? e - p : s + l - p))) { | |
612 | strv_free(v); | |
613 | return NULL; | |
614 | } | |
615 | ||
616 | if (!e) | |
617 | break; | |
618 | ||
619 | p = e + 1; | |
620 | } | |
621 | ||
622 | assert(i == c); | |
623 | ||
624 | return v; | |
625 | } |