Systemd/src/shared/strv.c

525 lines
11 KiB
C
Raw Normal View History

/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2009-11-18 00:42:52 +01:00
/***
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 Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
2009-11-18 00:42:52 +01:00
#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
2009-11-18 00:42:52 +01:00
#include "util.h"
#include "strv.h"
char *strv_find(char **l, const char *name) {
2010-02-13 01:04:44 +01:00
char **i;
2009-11-18 00:42:52 +01:00
assert(name);
2010-02-13 01:04:44 +01:00
STRV_FOREACH(i, l)
if (streq(*i, name))
return *i;
2009-11-18 00:42:52 +01:00
return NULL;
}
2010-06-16 05:06:02 +02:00
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;
}
2009-11-18 00:42:52 +01:00
void strv_free(char **l) {
char **k;
if (!l)
return;
for (k = l; *k; k++)
free(*k);
free(l);
}
2013-03-25 02:30:32 +01:00
char **strv_copy(char * const *l) {
2009-11-18 00:42:52 +01:00
char **r, **k;
k = r = new(char*, strv_length(l) + 1);
if (!r)
2009-11-18 00:42:52 +01:00
return NULL;
if (l)
for (; *l; k++, l++) {
*k = strdup(*l);
if (!*k) {
strv_free(r);
return NULL;
}
}
2009-11-18 00:42:52 +01:00
*k = NULL;
return r;
}
2013-03-25 02:30:32 +01:00
unsigned strv_length(char * const *l) {
2009-11-18 00:42:52 +01:00
unsigned n = 0;
if (!l)
return 0;
for (; *l; l++)
n++;
return n;
}
2010-04-10 17:43:12 +02:00
char **strv_new_ap(const char *x, va_list ap) {
2009-11-18 00:42:52 +01:00
const char *s;
char **a;
unsigned n = 0, i = 0;
2010-04-10 17:43:12 +02:00
va_list aq;
/* As a special trick we ignore all listed strings that equal
* (const char*) -1. This is supposed to be used with the
* STRV_IFNOTNULL() macro to include possibly NULL strings in
* the string list. */
2009-11-18 00:42:52 +01:00
if (x) {
n = x == (const char*) -1 ? 0 : 1;
2009-11-18 00:42:52 +01:00
2010-04-10 17:43:12 +02:00
va_copy(aq, ap);
while ((s = va_arg(aq, const char*))) {
if (s == (const char*) -1)
continue;
2009-11-18 00:42:52 +01:00
n++;
}
2010-04-10 17:43:12 +02:00
va_end(aq);
2009-11-18 00:42:52 +01:00
}
a = new(char*, n+1);
if (!a)
2009-11-18 00:42:52 +01:00
return NULL;
if (x) {
if (x != (const char*) -1) {
a[i] = strdup(x);
if (!a[i])
goto fail;
i++;
2009-11-18 00:42:52 +01:00
}
while ((s = va_arg(ap, const char*))) {
if (s == (const char*) -1)
continue;
a[i] = strdup(s);
if (!a[i])
2009-11-18 00:42:52 +01:00
goto fail;
i++;
}
}
a[i] = NULL;
2010-04-10 17:43:12 +02:00
2009-11-18 00:42:52 +01:00
return a;
fail:
strv_free(a);
2009-11-18 00:42:52 +01:00
return NULL;
}
2010-04-10 17:43:12 +02:00
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;
}
int strv_extend_strv(char ***a, char **b) {
int r;
char **s;
STRV_FOREACH(s, b) {
r = strv_extend(a, *s);
if (r < 0)
return r;
}
return 0;
2010-02-13 01:04:44 +01:00
}
int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
int r;
char **s;
2010-02-13 01:04:44 +01:00
STRV_FOREACH(s, b) {
char *v;
2010-02-13 01:04:44 +01:00
v = strappend(*s, suffix);
if (!v)
return -ENOMEM;
2010-02-13 01:04:44 +01:00
r = strv_push(a, v);
if (r < 0) {
free(v);
return r;
}
}
2010-02-13 01:04:44 +01:00
return 0;
2010-02-13 01:04:44 +01:00
}
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++;
r = new(char*, n+1);
if (!r)
2010-02-13 01:04:44 +01:00
return NULL;
i = 0;
FOREACH_WORD_SEPARATOR(w, l, s, separator, state) {
r[i] = strndup(w, l);
if (!r[i]) {
2010-02-13 01:04:44 +01:00
strv_free(r);
return NULL;
}
i++;
}
2010-02-13 01:04:44 +01:00
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++;
r = new(char*, n+1);
if (!r)
2010-02-13 01:04:44 +01:00
return NULL;
i = 0;
FOREACH_WORD_QUOTED(w, l, s, state) {
r[i] = cunescape_length(w, l);
if (!r[i]) {
2010-02-13 01:04:44 +01:00
strv_free(r);
return NULL;
}
i++;
}
2010-02-13 01:04:44 +01:00
r[i] = NULL;
return r;
}
char **strv_split_newlines(const char *s) {
char **l;
unsigned n;
assert(s);
/* Special version of strv_split() that splits on newlines and
* suppresses an empty string at the end */
l = strv_split(s, NEWLINE);
if (!l)
return NULL;
n = strv_length(l);
if (n <= 0)
return l;
if (isempty(l[n-1])) {
free(l[n-1]);
l[n-1] = NULL;
}
return l;
}
2010-02-13 01:04:44 +01:00
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);
}
r = new(char, n+1);
if (!r)
2010-02-13 01:04:44 +01:00
return NULL;
e = r;
STRV_FOREACH(s, l) {
if (e != r)
e = stpcpy(e, separator);
e = stpcpy(e, *s);
}
*e = 0;
2010-02-13 01:04:44 +01:00
return r;
}
char *strv_join_quoted(char **l) {
char *buf = NULL;
char **s;
size_t allocated = 0, len = 0;
STRV_FOREACH(s, l) {
/* assuming here that escaped string cannot be more
* than twice as long, and reserving space for the
* separator and quotes.
*/
_cleanup_free_ char *esc = NULL;
size_t needed;
if (!GREEDY_REALLOC(buf, allocated,
len + strlen(*s) * 2 + 3))
goto oom;
esc = cescape(*s);
if (!esc)
goto oom;
needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
len > 0 ? " " : "", esc);
assert(needed < allocated - len);
len += needed;
}
if (!buf)
buf = malloc0(1);
return buf;
oom:
free(buf);
return NULL;
}
int strv_push(char ***l, char *value) {
char **c;
2013-01-18 00:47:19 +01:00
unsigned n;
if (!value)
return 0;
2013-01-18 00:47:19 +01:00
n = strv_length(*l);
c = realloc(*l, sizeof(char*) * (n + 2));
if (!c)
2013-01-18 00:47:19 +01:00
return -ENOMEM;
c[n] = value;
2013-01-18 00:47:19 +01:00
c[n+1] = NULL;
*l = c;
return 0;
}
int strv_extend(char ***l, const char *value) {
char *v;
int r;
if (!value)
return 0;
v = strdup(value);
if (!v)
return -ENOMEM;
r = strv_push(l, v);
if (r < 0)
free(v);
return r;
}
2010-02-13 01:04:44 +01:00
char **strv_uniq(char **l) {
2010-01-27 22:38:21 +01:00
char **i;
2010-02-13 01:04:44 +01:00
/* Drops duplicate entries. The first identical string will be
* kept, the others dropped */
2010-01-27 22:38:21 +01:00
STRV_FOREACH(i, l)
2010-02-13 01:04:44 +01:00
strv_remove(i+1, *i);
return l;
}
char **strv_remove(char **l, const char *s) {
char **f, **t;
if (!l)
return NULL;
assert(s);
/* Drops every occurrence of s in the string list, edits
* in-place. */
2010-02-13 01:04:44 +01:00
for (f = t = l; *f; f++)
if (streq(*f, s))
free(*f);
else
*(t++) = *f;
*t = NULL;
return l;
}
char **strv_parse_nulstr(const char *s, size_t l) {
const char *p;
unsigned c = 0, i = 0;
char **v;
assert(s || l <= 0);
if (l <= 0)
return new0(char*, 1);
for (p = s; p < s + l; p++)
if (*p == 0)
c++;
if (s[l-1] != 0)
c++;
v = new0(char*, c+1);
if (!v)
return NULL;
p = s;
while (p < s + l) {
const char *e;
e = memchr(p, 0, s + l - p);
v[i] = strndup(p, e ? e - p : s + l - p);
if (!v[i]) {
strv_free(v);
return NULL;
}
i++;
if (!e)
break;
p = e + 1;
}
assert(i == c);
return v;
}
char **strv_split_nulstr(const char *s) {
const char *i;
char **r = NULL;
NULSTR_FOREACH(i, s)
if (strv_extend(&r, i) < 0) {
strv_free(r);
return NULL;
}
if (!r)
return strv_new(NULL, NULL);
return r;
}
bool strv_overlap(char **a, char **b) {
char **i;
STRV_FOREACH(i, a)
if (strv_contains(b, *i))
return true;
return false;
}
2012-10-19 04:52:25 +02:00
static int str_compare(const void *_a, const void *_b) {
const char **a = (const char**) _a, **b = (const char**) _b;
return strcmp(*a, *b);
}
char **strv_sort(char **l) {
if (strv_isempty(l))
return l;
qsort(l, strv_length(l), sizeof(char*), str_compare);
return l;
}
void strv_print(char **l) {
char **s;
STRV_FOREACH(s, l)
puts(*s);
}