strv.c revision d7832d2c6e0ef5f2839a2296c1cc2fc85c7d9632
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering This file is part of systemd.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering Copyright 2010 Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering systemd is free software; you can redistribute it and/or modify it
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering under the terms of the GNU General Public License as published by
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering the Free Software Foundation; either version 2 of the License, or
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering (at your option) any later version.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering systemd is distributed in the hope that it will be useful, but
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering General Public License for more details.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering You should have received a copy of the GNU General Public License
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *strv_find(char **l, const char *name) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *strv_find_prefix(char **l, const char *name) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringvoid strv_free(char **l) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (k = l; *k; k++)
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidtchar **strv_copy(char **l) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char **r, **k;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering for (; *l; k++, l++)
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering if (!(*k = strdup(*l)))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (k--; k >= r; k--)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringunsigned strv_length(char **l) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned n = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (; *l; l++)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar **strv_new_ap(const char *x, va_list ap) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *s;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned n = 0, i = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!(a[i] = strdup(x))) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek while ((s = va_arg(ap, const char*))) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (!(a[i] = strdup(s)))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (; i > 0; i--)
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmekchar **strv_new(const char *x, ...) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmekchar **strv_merge(char **a, char **b) {
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek for (k = r; *a; k++, a++)
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek for (; *b; k++, b++)
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek for (k--; k >= r; k--)
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmekchar **strv_merge_concat(char **a, char **b, const char *suffix) {
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek /* Like strv_merge(), but appends suffix to all strings in b, before adding */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = new(char*, strv_length(a) + strv_length(b) + 1);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (; *a; k++, a++) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (; *b; k++, b++) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (k--; k >= r; k--)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar **strv_split(const char *s, const char *separator) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned n, i;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!(r[i++] = strndup(w, l))) {
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmekchar **strv_split_quoted(const char *s) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned n, i;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek FOREACH_WORD_QUOTED(w, l, s, state)
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek FOREACH_WORD_QUOTED(w, l, s, state)
baf167ee0a2953f98e4e7d4c35752ef737832674Zbigniew Jędrzejewski-Szmek if (!(r[i++] = cunescape_length(w, l))) {
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmekchar *strv_join(char **l, const char *separator) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar **strv_append(char **l, const char *s) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char **r, **k;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek for (k = r; *l; k++, l++)
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (!(*(k++) = strdup(s)))
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek for (k--; k >= r; k--)
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekchar **strv_uniq(char **l) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek /* Drops duplicate entries. The first identical string will be
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek * kept, the others dropped */
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekchar **strv_remove(char **l, const char *s) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek /* Drops every occurrence of s in the string list, edits
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek for (f = t = l; *f; f++) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekstatic int env_append(char **r, char ***k, char **a) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek /* Add the entries of a to *k unless they already exist in *r
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek * in which case they are overridden instead. This assumes
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek * there is enough space in the r array. */
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek for (; *a; a++) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if ((*a)[n] == '=')
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (j = r; j < *k; j++)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (strncmp(*j, *a, n) == 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar **strv_env_merge(unsigned n_lists, ...) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char **l, **k, **r;
e3b9d9c8027a7c4c55cf1614e0fe9423fad69e8fZbigniew Jędrzejewski-Szmek /* Merges an arbitrary number of environment sets */
e3b9d9c8027a7c4c55cf1614e0fe9423fad69e8fZbigniew Jędrzejewski-Szmek for (i = 0; i < n_lists; i++) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (i = 0; i < n_lists; i++) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (env_append(r, &k, l) < 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (k--; k >= r; k--)
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekstatic bool env_match(const char *t, const char *pattern) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* pattern a matches string a
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering * a matches a=
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering * a matches a=b
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering * a=b matches a=b
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering * a= does not match a
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek * a=b does not match a=
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering * a=b does not match a
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering * a=b does not match a=c */
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return strncmp(t, pattern, l) == 0 && t[l] == '=';
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar **strv_env_delete(char **x, unsigned n_lists, ...) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char **k, **r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* Deletes every entry from x that is mentioned in the other
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering * string lists */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (v = 0; v < n_lists; v++) {
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering char **l, **j;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar **strv_env_unset(char **l, const char *p) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char **f, **t;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* Drops every occurrence of the env var setting p in the
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering * string list. edits in-place. */
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering for (f = t = l; *f; f++) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar **strv_env_set(char **x, const char *p) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char **k, **r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* Overrides the env var setting of p, returns a new copy */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (env_append(r, &k, x) < 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (env_append(r, &k, m) < 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (k--; k >= r; k--)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *strv_env_get_with_length(char **l, const char *name, size_t k) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering (*i)[k] == '=')
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return *i + k + 1;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar *strv_env_get(char **l, const char *name) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return strv_env_get_with_length(l, name, strlen(name));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringchar **strv_env_clean(char **l) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (r = ret = l; *l; l++) {
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekchar **strv_parse_nulstr(const char *s, size_t l) {
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek unsigned c = 0, i = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (p = s; p < s + l; p++)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (s[l-1] != 0)
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering while (p < s + l) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *e;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering e = memchr(p, 0, s + l - p);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!(v[i++] = strndup(p, e ? e - p : s + l - p))) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekbool strv_overlap(char **a, char **b) {