strv.c revision fe382237cad0ade50d38075e0bf948ce07618461
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2010 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <stdlib.h>
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <stdarg.h>
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek#include <string.h>
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek#include <errno.h>
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "util.h"
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek#include "strv.h"
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringchar *strv_find(char **l, const char *name) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char **i;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering STRV_FOREACH(i, l)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (streq(*i, name))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return *i;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringchar *strv_find_prefix(char **l, const char *name) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char **i;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering STRV_FOREACH(i, l)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (startswith(*i, name))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return *i;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringchar *strv_find_startswith(char **l, const char *name) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char **i, *e;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Like strv_find_prefix, but actually returns only the
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * suffix, not the whole item */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering STRV_FOREACH(i, l) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering e = startswith(*i, name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (e)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return e;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringvoid strv_clear(char **l) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char **k;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!l)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering for (k = l; *k; k++)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(*k);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *l = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringchar **strv_free(char **l) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering strv_clear(l);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(l);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringchar **strv_copy(char * const *l) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char **r, **k;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k = r = new(char*, strv_length(l) + 1);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!r)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (l)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering for (; *l; k++, l++) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *k = strdup(*l);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!*k) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering strv_free(r);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *k = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringunsigned strv_length(char * const *l) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering unsigned n = 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!l)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering for (; *l; l++)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering n++;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return n;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringchar **strv_new_ap(const char *x, va_list ap) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *s;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char **a;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering unsigned n = 0, i = 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering va_list aq;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* As a special trick we ignore all listed strings that equal
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * (const char*) -1. This is supposed to be used with the
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * STRV_IFNOTNULL() macro to include possibly NULL strings in
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * the string list. */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (x) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering n = x == (const char*) -1 ? 0 : 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering va_copy(aq, ap);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt while ((s = va_arg(aq, const char*))) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (s == (const char*) -1)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering continue;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering n++;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering va_end(aq);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering a = new(char*, n+1);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt if (!a)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (x) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (x != (const char*) -1) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering a[i] = strdup(x);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!a[i])
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering goto fail;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering i++;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering while ((s = va_arg(ap, const char*))) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (s == (const char*) -1)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering continue;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering a[i] = strdup(s);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!a[i])
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering goto fail;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering i++;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt }
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt }
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt a[i] = NULL;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return a;
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringfail:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering strv_free(a);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersenchar **strv_new(const char *x, ...) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char **r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering va_list ap;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering va_start(ap, x);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = strv_new_ap(x, ap);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen va_end(ap);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
23432a1c249b9c513e438bffe9778a7ce2ee74feZbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringint strv_extend_strv(char ***a, char **b) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char **s;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering STRV_FOREACH(s, b) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = strv_extend(a, *s);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char **s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering STRV_FOREACH(s, b) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *v;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering v = strappend(*s, suffix);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (!v)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return -ENOMEM;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering r = strv_push(a, v);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (r < 0) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering free(v);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return r;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringchar **strv_split(const char *s, const char *separator) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *word, *state;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering size_t l;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering unsigned n, i;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char **r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(s);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering n = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering FOREACH_WORD_SEPARATOR(word, l, s, separator, state)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering n++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = new(char*, n+1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!r)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering i = 0;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek FOREACH_WORD_SEPARATOR(word, l, s, separator, state) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r[i] = strndup(word, l);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (!r[i]) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering strv_free(r);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering }
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek i++;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek }
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r[i] = NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return r;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering}
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringchar **strv_split_newlines(const char *s) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char **l;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek unsigned n;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek assert(s);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek /* Special version of strv_split() that splits on newlines and
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek * suppresses an empty string at the end */
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek l = strv_split(s, NEWLINE);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (!l)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering n = strv_length(l);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (n <= 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return l;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (isempty(l[n - 1]))
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen l[n - 1] = mfree(l[n - 1]);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return l;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen}
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poetteringint strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering size_t n = 0, allocated = 0;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering _cleanup_strv_free_ char **l = NULL;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek int r;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(t);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(s);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek for (;;) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *word = NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = extract_first_word(&s, &word, separators, flags);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (r < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return r;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (r == 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen break;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (!GREEDY_REALLOC(l, allocated, n + 2))
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return -ENOMEM;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen l[n++] = word;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen word = NULL;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek l[n] = NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek }
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (!l)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering l = new0(char*, 1);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *t = l;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek l = NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringchar *strv_join(char **l, const char *separator) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering char *r, *e;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering char **s;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering size_t n, k;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!separator)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering separator = " ";
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering k = strlen(separator);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering n = 0;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering STRV_FOREACH(s, l) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (n != 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering n += k;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering n += strlen(*s);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = new(char, n+1);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!r)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return NULL;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering e = r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering STRV_FOREACH(s, l) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (e != r)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering e = stpcpy(e, separator);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering e = stpcpy(e, *s);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering *e = 0;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringchar *strv_join_quoted(char **l) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering char *buf = NULL;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering char **s;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering size_t allocated = 0, len = 0;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering STRV_FOREACH(s, l) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* assuming here that escaped string cannot be more
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering * than twice as long, and reserving space for the
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering * separator and quotes.
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering */
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_free_ char *esc = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering size_t needed;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!GREEDY_REALLOC(buf, allocated,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering len + strlen(*s) * 2 + 3))
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering goto oom;
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering esc = cescape(*s);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering if (!esc)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering goto oom;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering len > 0 ? " " : "", esc);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering assert(needed < allocated - len);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering len += needed;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering }
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (!buf)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering buf = malloc0(1);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return buf;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering oom:
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering free(buf);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringint strv_push(char ***l, char *value) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char **c;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek unsigned n, m;
0f84a72e3c0f58d71cff2121e6df1611eaf9c9eaDavid Herrmann
0f84a72e3c0f58d71cff2121e6df1611eaf9c9eaDavid Herrmann if (!value)
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering n = strv_length(*l);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* Increase and check for overflow */
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering m = n + 2;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (m < n)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -ENOMEM;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering c = realloc_multiply(*l, sizeof(char*), m);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (!c)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return -ENOMEM;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering c[n] = value;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering c[n+1] = NULL;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering *l = c;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return 0;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering}
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poetteringint strv_push_pair(char ***l, char *a, char *b) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek char **c;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek unsigned n, m;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (!a && !b)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return 0;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek n = strv_length(*l);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* increase and check for overflow */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek m = n + !!a + !!b + 1;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (m < n)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -ENOMEM;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek c = realloc_multiply(*l, sizeof(char*), m);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!c)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -ENOMEM;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (a)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen c[n++] = a;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (b)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen c[n++] = b;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen c[n] = NULL;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering *l = c;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return 0;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering}
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poetteringint strv_push_prepend(char ***l, char *value) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering char **c;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek unsigned n, m, i;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (!value)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return 0;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek n = strv_length(*l);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek /* increase and check for overflow */
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek m = n + 2;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn if (m < n)
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn return -ENOMEM;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn c = new(char*, m);
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn if (!c)
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn return -ENOMEM;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek for (i = 0; i < n; i++)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek c[i+1] = (*l)[i];
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek c[0] = value;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek c[n+1] = NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen free(*l);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *l = c;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return 0;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen}
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenint strv_consume(char ***l, char *value) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen int r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = strv_push(l, value);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen free(value);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringint strv_consume_pair(char ***l, char *a, char *b) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = strv_push_pair(l, a, b);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek free(a);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek free(b);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekint strv_consume_prepend(char ***l, char *value) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek int r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = strv_push_prepend(l, value);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek free(value);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekint strv_extend(char ***l, const char *value) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek char *v;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!value)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek v = strdup(value);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!v)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -ENOMEM;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return strv_consume(l, v);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersenchar **strv_uniq(char **l) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen char **i;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen /* Drops duplicate entries. The first identical string will be
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen * kept, the others dropped */
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen STRV_FOREACH(i, l)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen strv_remove(i+1, *i);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return l;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen}
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersenbool strv_is_uniq(char **l) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen char **i;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen STRV_FOREACH(i, l)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (strv_find(i+1, *i))
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen return false;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return true;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersenchar **strv_remove(char **l, const char *s) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen char **f, **t;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!l)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return NULL;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(s);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen /* Drops every occurrence of s in the string list, edits
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen * in-place. */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen for (f = t = l; *f; f++)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (streq(*f, s))
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen free(*f);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen else
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *(t++) = *f;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *t = NULL;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return l;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersenchar **strv_parse_nulstr(const char *s, size_t l) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen const char *p;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen unsigned c = 0, i = 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char **v;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(s || l <= 0);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (l <= 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return new0(char*, 1);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering for (p = s; p < s + l; p++)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (*p == 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering c++;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (s[l-1] != 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering c++;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering v = new0(char*, c+1);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (!v)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering p = s;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering while (p < s + l) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering const char *e;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering e = memchr(p, 0, s + l - p);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering v[i] = strndup(p, e ? e - p : s + l - p);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!v[i]) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering strv_free(v);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering return NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering i++;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!e)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering break;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering p = e + 1;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(i == c);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return v;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek}
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmekchar **strv_split_nulstr(const char *s) {
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek const char *i;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek char **r = NULL;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek NULSTR_FOREACH(i, s)
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (strv_extend(&r, i) < 0) {
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek strv_free(r);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return NULL;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!r)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return strv_new(NULL, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringbool strv_overlap(char **a, char **b) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char **i;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering STRV_FOREACH(i, a)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (strv_contains(b, *i))
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return true;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return false;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int str_compare(const void *_a, const void *_b) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek const char **a = (const char**) _a, **b = (const char**) _b;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return strcmp(*a, *b);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringchar **strv_sort(char **l) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (strv_isempty(l))
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return l;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering qsort(l, strv_length(l), sizeof(char*), str_compare);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return l;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringbool strv_equal(char **a, char **b) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!a || !b)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return a == b;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering for ( ; *a || *b; ++a, ++b)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (!streq_ptr(*a, *b))
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return false;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return true;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek}
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmekvoid strv_print(char **l) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek char **s;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek STRV_FOREACH(s, l)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek puts(*s);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekint strv_extendf(char ***l, const char *format, ...) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek va_list ap;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek char *x;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek int r;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek va_start(ap, format);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = vasprintf(&x, format, ap);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek va_end(ap);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -ENOMEM;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return strv_consume(l, x);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen}
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersenchar **strv_reverse(char **l) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen unsigned n, i;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen n = strv_length(l);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (n <= 1)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return l;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen for (i = 0; i < n / 2; i++) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen char *t;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen t = l[i];
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen l[i] = l[n-1-i];
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen l[n-1-i] = t;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return l;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering}
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poetteringchar **strv_shell_escape(char **l, const char *bad) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering char **s;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering /* Escapes every character in every string in l that is in bad,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek * edits in-place, does not roll-back on error. */
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering STRV_FOREACH(s, l) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering char *v;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek v = shell_escape(*s, bad);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek if (!v)
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek return NULL;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek free(*s);
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen *s = v;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return l;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekbool strv_fnmatch(char* const* patterns, const char *s, int flags) {
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek char* const* p;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek STRV_FOREACH(p, patterns)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (fnmatch(*p, s, 0) == 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return true;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek return false;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekchar ***strv_free_free(char ***l) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen char ***i;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!l)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen for (i = l; *i; i++)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek strv_free(*i);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek free(l);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return NULL;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen}
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen