strv.c revision 5b6319dceedd81f3f1ce7eb70ea5defaef43bcec
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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 **i;
STRV_FOREACH(i, l)
return *i;
return NULL;
}
char *strv_find_prefix(char **l, const char *name) {
char **i;
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;
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;
}
const char *s;
char **a;
unsigned n = 0, i = 0;
if (x) {
n = 1;
n++;
}
if (!(a = new(char*, n+1)))
return NULL;
if (x) {
if (!(a[i] = strdup(x))) {
free(a);
return NULL;
}
i++;
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;
r = strv_new_ap(x, 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);
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);
return NULL;
for (k = r; *a; k++, a++)
if (!(*k = strdup(*a)))
goto fail;
for (; *b; k++, b++)
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;
n++;
if (!(r = new(char*, n+1)))
return NULL;
i = 0;
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 *r, *e;
char **s;
size_t n, k;
if (!separator)
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, *s);
}
*e = 0;
return r;
}
char **strv_append(char **l, const char *s) {
char **r, **k;
if (!l)
if (!s)
return strv_copy(l);
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;
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;
unsigned i;
/* Merges an arbitrary number of environment sets */
for (i = 0; i < n_lists; i++) {
n += strv_length(l);
}
if (!(r = new(char*, n+1)))
return NULL;
k = r;
for (i = 0; i < n_lists; i++) {
if (env_append(r, &k, l) < 0)
goto fail;
}
*k = NULL;
return r;
fail:
for (k--; k >= r; k--)
free(*k);
free(r);
return NULL;
}
assert(t);
/* 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 */
return true;
}
return false;
}
char **strv_env_delete(char **x, unsigned n_lists, ...) {
size_t n = 0, i = 0;
char **l, **k, **r, **j;
/* 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) {
for (i = 0; i < n_lists; i++) {
STRV_FOREACH(j, l)
if (env_match(*k, *j))
goto delete;
}
if (!(r[i++] = strdup(*k))) {
strv_free(r);
return NULL;
}
continue;
}
r[i] = NULL;
assert(i <= n);
return r;
}