chiark / gitweb /
strv: fix counting in strv_env_delete()
[elogind.git] / src / strv.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
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
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "util.h"
29 #include "strv.h"
30
31 char *strv_find(char **l, const char *name) {
32         char **i;
33
34         assert(name);
35
36         STRV_FOREACH(i, l)
37                 if (streq(*i, name))
38                         return *i;
39
40         return NULL;
41 }
42
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
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 (!(k = r = new(char*, strv_length(l)+1)))
71                 return NULL;
72
73         if (l)
74                 for (; *l; k++, l++)
75                         if (!(*k = strdup(*l)))
76                                 goto fail;
77
78         *k = NULL;
79         return r;
80
81 fail:
82         for (k--; k >= r; k--)
83                 free(*k);
84
85         free(r);
86
87         return NULL;
88 }
89
90 unsigned strv_length(char **l) {
91         unsigned n = 0;
92
93         if (!l)
94                 return 0;
95
96         for (; *l; l++)
97                 n++;
98
99         return n;
100 }
101
102 char **strv_new_ap(const char *x, va_list ap) {
103         const char *s;
104         char **a;
105         unsigned n = 0, i = 0;
106         va_list aq;
107
108         if (x) {
109                 n = 1;
110
111                 va_copy(aq, ap);
112                 while (va_arg(aq, const char*))
113                         n++;
114                 va_end(aq);
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
128                 while ((s = va_arg(ap, const char*))) {
129                         if (!(a[i] = strdup(s)))
130                                 goto fail;
131
132                         i++;
133                 }
134         }
135
136         a[i] = NULL;
137
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);
147
148         return NULL;
149 }
150
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
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
188         free(r);
189
190         return NULL;
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
218         free(r);
219
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)
269                 if (!(r[i++] = cunescape_length(w, l))) {
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
306         *e = 0;
307
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;
322
323         for (k = r; *l; k++, l++)
324                 if (!(*k = strdup(*l)))
325                         goto fail;
326
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
337         free(r);
338
339         return NULL;
340 }
341
342 char **strv_uniq(char **l) {
343         char **i;
344
345         /* Drops duplicate entries. The first identical string will be
346          * kept, the others dropped */
347
348         STRV_FOREACH(i, l)
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
360         assert(s);
361
362         /* Drops every occurrence of s in the string list, edits
363          * in-place. */
364
365         for (f = t = l; *f; f++) {
366
367                 if (streq(*f, s)) {
368                         free(*f);
369                         continue;
370                 }
371
372                 *(t++) = *f;
373         }
374
375         *t = NULL;
376         return l;
377 }
378
379 static int env_append(char **r, char ***k, char **a) {
380         assert(r);
381         assert(k);
382
383         if (!a)
384                 return 0;
385
386         /* Add the entries of a to *k unless they already exist in *r
387          * in which case they are overridden instead. This assumes
388          * there is enough space in the r array. */
389
390         for (; *a; a++) {
391                 char **j;
392                 size_t n;
393
394                 n = strcspn(*a, "=");
395
396                 if ((*a)[n] == '=')
397                         n++;
398
399                 for (j = r; j < *k; j++)
400                         if (strncmp(*j, *a, n) == 0)
401                                 break;
402
403                 if (j >= *k)
404                         (*k)++;
405                 else
406                         free(*j);
407
408                 if (!(*j = strdup(*a)))
409                         return -ENOMEM;
410         }
411
412         return 0;
413 }
414
415 char **strv_env_merge(unsigned n_lists, ...) {
416         size_t n = 0;
417         char **l, **k, **r;
418         va_list ap;
419         unsigned i;
420
421         /* Merges an arbitrary number of environment sets */
422
423         va_start(ap, n_lists);
424         for (i = 0; i < n_lists; i++) {
425                 l = va_arg(ap, char**);
426                 n += strv_length(l);
427         }
428         va_end(ap);
429
430         if (!(r = new(char*, n+1)))
431                 return NULL;
432
433         k = r;
434
435         va_start(ap, n_lists);
436         for (i = 0; i < n_lists; i++) {
437                 l = va_arg(ap, char**);
438                 if (env_append(r, &k, l) < 0)
439                         goto fail;
440         }
441         va_end(ap);
442
443         *k = NULL;
444
445         return r;
446
447 fail:
448         va_end(ap);
449
450         for (k--; k >= r; k--)
451                 free(*k);
452
453         free(r);
454
455         return NULL;
456 }
457
458 static bool env_match(const char *t, const char *pattern) {
459         assert(t);
460         assert(pattern);
461
462         /* pattern a matches string a
463          *         a matches a=
464          *         a matches a=b
465          *         a= matches a=
466          *         a=b matches a=b
467          *         a= does not match a
468          *         a=b does not match a=
469          *         a=b does not match a
470          *         a=b does not match a=c */
471
472         if (streq(t, pattern))
473                 return true;
474
475         if (!strchr(pattern, '=')) {
476                 size_t l = strlen(pattern);
477
478                 return strncmp(t, pattern, l) == 0 && t[l] == '=';
479         }
480
481         return false;
482 }
483
484 char **strv_env_delete(char **x, unsigned n_lists, ...) {
485         size_t n, i = 0;
486         char **k, **r;
487         va_list ap;
488
489         /* Deletes every entry from x that is mentioned in the other
490          * string lists */
491
492         n = strv_length(x);
493
494         r = new(char*, n+1);
495         if (!r)
496                 return NULL;
497
498         STRV_FOREACH(k, x) {
499                 unsigned v;
500
501                 va_start(ap, n_lists);
502                 for (v = 0; v < n_lists; v++) {
503                         char **l, **j;
504
505                         l = va_arg(ap, char**);
506                         STRV_FOREACH(j, l)
507                                 if (env_match(*k, *j))
508                                         goto skip;
509                 }
510                 va_end(ap);
511
512                 r[i] = strdup(*k);
513                 if (!r[i]) {
514                         strv_free(r);
515                         return NULL;
516                 }
517
518                 i++;
519                 continue;
520
521         skip:
522                 va_end(ap);
523         }
524
525         r[i] = NULL;
526
527         assert(i <= n);
528
529         return r;
530 }
531
532 char **strv_env_unset(char **l, const char *p) {
533
534         char **f, **t;
535
536         if (!l)
537                 return NULL;
538
539         assert(p);
540
541         /* Drops every occurrence of the env var setting p in the
542          * string list. edits in-place. */
543
544         for (f = t = l; *f; f++) {
545
546                 if (env_match(*f, p)) {
547                         free(*f);
548                         continue;
549                 }
550
551                 *(t++) = *f;
552         }
553
554         *t = NULL;
555         return l;
556 }
557
558 char **strv_env_set(char **x, const char *p) {
559
560         char **k, **r;
561         char* m[2] = { (char*) p, NULL };
562
563         /* Overrides the env var setting of p, returns a new copy */
564
565         if (!(r = new(char*, strv_length(x)+2)))
566                 return NULL;
567
568         k = r;
569         if (env_append(r, &k, x) < 0)
570                 goto fail;
571
572         if (env_append(r, &k, m) < 0)
573                 goto fail;
574
575         *k = NULL;
576
577         return r;
578
579 fail:
580         for (k--; k >= r; k--)
581                 free(*k);
582
583         free(r);
584
585         return NULL;
586
587 }
588
589 char *strv_env_get_with_length(char **l, const char *name, size_t k) {
590         char **i;
591
592         assert(name);
593
594         STRV_FOREACH(i, l)
595                 if (strncmp(*i, name, k) == 0 &&
596                     (*i)[k] == '=')
597                         return *i + k + 1;
598
599         return NULL;
600 }
601
602 char *strv_env_get(char **l, const char *name) {
603         return strv_env_get_with_length(l, name, strlen(name));
604 }
605
606 char **strv_env_clean(char **l) {
607         char **r, **ret;
608
609         for (r = ret = l; *l; l++) {
610                 const char *equal;
611
612                 equal = strchr(*l, '=');
613
614                 if (equal && equal[1] == 0) {
615                         free(*l);
616                         continue;
617                 }
618
619                 *(r++) = *l;
620         }
621
622         *r = NULL;
623
624         return ret;
625 }
626
627 char **strv_parse_nulstr(const char *s, size_t l) {
628         const char *p;
629         unsigned c = 0, i = 0;
630         char **v;
631
632         assert(s || l <= 0);
633
634         if (l <= 0)
635                 return strv_new(NULL, NULL);
636
637         for (p = s; p < s + l; p++)
638                 if (*p == 0)
639                         c++;
640
641         if (s[l-1] != 0)
642                 c++;
643
644         if (!(v = new0(char*, c+1)))
645                 return NULL;
646
647         p = s;
648         while (p < s + l) {
649                 const char *e;
650
651                 e = memchr(p, 0, s + l - p);
652
653                 if (!(v[i++] = strndup(p, e ? e - p : s + l - p))) {
654                         strv_free(v);
655                         return NULL;
656                 }
657
658                 if (!e)
659                         break;
660
661                 p = e + 1;
662         }
663
664         assert(i == c);
665
666         return v;
667 }
668
669 bool strv_overlap(char **a, char **b) {
670         char **i, **j;
671
672         STRV_FOREACH(i, a) {
673                 STRV_FOREACH(j, b) {
674                         if (streq(*i, *j))
675                                 return true;
676                 }
677         }
678
679         return false;
680 }