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