strv: multiple cleanups

- turn strv_merge into strv_extend_strv.
   appending strv b to the end of strv a instead of creating a new strv
- strv_append: remove in favor of strv_extend and strv_push.
- strv_remove: write slightly more elegant
- strv_remove_prefix: remove unused function
- strv_overlap: use strv_contains
- strv_printf: STRV_FOREACH handles NULL correctly
This commit is contained in:
Simon Peeters 2014-01-04 02:35:27 +01:00 committed by Zbigniew Jędrzejewski-Szmek
parent bf85c24daa
commit e3e45d4f82
8 changed files with 97 additions and 279 deletions

View file

@ -601,15 +601,12 @@ static int config_parse_join_controllers(const char *unit,
if (strv_overlap(*a, l)) { if (strv_overlap(*a, l)) {
char **c; char **c;
c = strv_merge(*a, l); if (strv_extend_strv(&l, *a) < 0) {
if (!c) {
strv_free(l); strv_free(l);
strv_free_free(t); strv_free_free(t);
return log_oom(); return log_oom();
} }
strv_free(l);
l = c;
} else { } else {
char **c; char **c;
@ -1853,10 +1850,11 @@ finish:
shutdown_verb, shutdown_verb,
NULL NULL
}; };
char **env_block; _cleanup_strv_free_ char **env_block = NULL;
env_block = strv_copy(environ);
if (arm_reboot_watchdog && arg_shutdown_watchdog > 0) { if (arm_reboot_watchdog && arg_shutdown_watchdog > 0) {
char e[32]; char *e;
/* If we reboot let's set the shutdown /* If we reboot let's set the shutdown
* watchdog and tell the shutdown binary to * watchdog and tell the shutdown binary to
@ -1864,15 +1862,11 @@ finish:
watchdog_set_timeout(&arg_shutdown_watchdog); watchdog_set_timeout(&arg_shutdown_watchdog);
watchdog_close(false); watchdog_close(false);
/* Tell the binary how often to ping */ /* Tell the binary how often to ping, ignore failure */
snprintf(e, sizeof(e), "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog); if (asprintf(&e, "WATCHDOG_USEC="USEC_FMT, arg_shutdown_watchdog) > 0)
char_array_0(e); strv_push(&env_block, e);
} else
env_block = strv_append(environ, e);
} else {
env_block = strv_copy(environ);
watchdog_close(true); watchdog_close(true);
}
/* Avoid the creation of new processes forked by the /* Avoid the creation of new processes forked by the
* kernel; at this point, we will not listen to the * kernel; at this point, we will not listen to the
@ -1881,7 +1875,6 @@ finish:
cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER); cg_uninstall_release_agent(SYSTEMD_CGROUP_CONTROLLER);
execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block); execve(SYSTEMD_SHUTDOWN_BINARY_PATH, (char **) command_line, env_block);
free(env_block);
log_error("Failed to execute shutdown binary, freezing: %m"); log_error("Failed to execute shutdown binary, freezing: %m");
} }

View file

@ -64,13 +64,9 @@ static int add_modules(const char *p) {
if (!k) if (!k)
return log_oom(); return log_oom();
t = strv_merge(arg_proc_cmdline_modules, k); if (strv_extend_strv(&arg_proc_cmdline_modules, k) < 0)
if (!t)
return log_oom(); return log_oom();
strv_free(arg_proc_cmdline_modules);
arg_proc_cmdline_modules = t;
return 0; return 0;
} }

View file

@ -90,9 +90,9 @@ static char** user_dirs(
}; };
const char *home, *e; const char *home, *e;
char *config_home = NULL, *data_home = NULL; _cleanup_free_ char *config_home = NULL, *data_home = NULL;
char **config_dirs = NULL, **data_dirs = NULL; _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
char **r = NULL, **t; char **r = NULL;
/* Implement the mechanisms defined in /* Implement the mechanisms defined in
* *
@ -150,89 +150,48 @@ static char** user_dirs(
goto fail; goto fail;
/* Now merge everything we found. */ /* Now merge everything we found. */
if (generator_early) { if (generator_early)
t = strv_append(r, generator_early); if (strv_extend(&r, generator_early) < 0)
if (!t)
goto fail; goto fail;
strv_free(r);
r = t;
}
if (config_home) { if (config_home)
t = strv_append(r, config_home); if (strv_extend(&r, config_home) < 0)
if (!t)
goto fail; goto fail;
strv_free(r);
r = t;
}
if (!strv_isempty(config_dirs)) { if (!strv_isempty(config_dirs))
t = strv_merge_concat(r, config_dirs, "/systemd/user"); if (strv_extend_strv_concat(&r, config_dirs, "/systemd/user") < 0)
if (!t) goto fail;
goto finish;
strv_free(r);
r = t;
}
t = strv_merge(r, (char**) config_unit_paths); if (strv_extend_strv(&r, (char**) config_unit_paths) < 0)
if (!t)
goto fail; goto fail;
strv_free(r);
r = t;
if (generator) { if (generator)
t = strv_append(r, generator); if (strv_extend(&r, generator) < 0)
if (!t)
goto fail; goto fail;
strv_free(r);
r = t;
}
if (data_home) { if (data_home)
t = strv_append(r, data_home); if (strv_extend(&r, data_home) < 0)
if (!t)
goto fail; goto fail;
strv_free(r);
r = t;
}
if (!strv_isempty(data_dirs)) { if (!strv_isempty(data_dirs))
t = strv_merge_concat(r, data_dirs, "/systemd/user"); if (strv_extend_strv_concat(&r, data_dirs, "/systemd/user") < 0)
if (!t)
goto fail; goto fail;
strv_free(r);
r = t;
}
t = strv_merge(r, (char**) data_unit_paths); if (strv_extend_strv(&r, (char**) data_unit_paths) < 0)
if (!t)
goto fail; goto fail;
strv_free(r);
r = t;
if (generator_late) { if (generator_late)
t = strv_append(r, generator_late); if (strv_extend(&r, generator_late) < 0)
if (!t)
goto fail; goto fail;
strv_free(r);
r = t;
}
if (!path_strv_make_absolute_cwd(r)) if (!path_strv_make_absolute_cwd(r))
goto fail; goto fail;
finish:
free(config_home);
strv_free(config_dirs);
free(data_home);
strv_free(data_dirs);
return r; return r;
fail: fail:
strv_free(r); strv_free(r);
r = NULL; return NULL;
goto finish;
} }
int lookup_paths_init( int lookup_paths_init(

View file

@ -166,72 +166,38 @@ char **strv_new(const char *x, ...) {
return r; return r;
} }
char **strv_merge(char **a, char **b) { int strv_extend_strv(char ***a, char **b) {
char **r, **k; int r;
char **s;
if (!a) STRV_FOREACH(s, b) {
return strv_copy(b); r = strv_extend(a, *s);
if (r < 0)
if (!b) return r;
return strv_copy(a);
r = new(char*, strv_length(a) + strv_length(b) + 1);
if (!r)
return NULL;
for (k = r; *a; k++, a++) {
*k = strdup(*a);
if (!*k)
goto fail;
} }
for (; *b; k++, b++) { return 0;
*k = strdup(*b);
if (!*k)
goto fail;
}
*k = NULL;
return r;
fail:
strv_free(r);
return NULL;
} }
char **strv_merge_concat(char **a, char **b, const char *suffix) { int strv_extend_strv_concat(char ***a, char **b, const char *suffix) {
char **r, **k; int r;
char **s;
/* Like strv_merge(), but appends suffix to all strings in b, before adding */ STRV_FOREACH(s, b) {
char *v;
if (!b) v = strappend(*s, suffix);
return strv_copy(a); if (!v)
return -ENOMEM;
r = new(char*, strv_length(a) + strv_length(b) + 1); r = strv_push(a, v);
if (!r) if (r < 0) {
return NULL; free(v);
return r;
k = r;
if (a)
for (; *a; k++, a++) {
*k = strdup(*a);
if (!*k)
goto fail;
} }
for (; *b; k++, b++) {
*k = strappend(*b, suffix);
if (!*k)
goto fail;
} }
*k = NULL; return 0;
return r;
fail:
strv_free(r);
return NULL;
} }
char **strv_split(const char *s, const char *separator) { char **strv_split(const char *s, const char *separator) {
@ -393,37 +359,6 @@ char *strv_join_quoted(char **l) {
return NULL; return NULL;
} }
char **strv_append(char **l, const char *s) {
char **r, **k;
if (!l)
return strv_new(s, NULL);
if (!s)
return strv_copy(l);
r = new(char*, strv_length(l)+2);
if (!r)
return NULL;
for (k = r; *l; k++, l++) {
*k = strdup(*l);
if (!*k)
goto fail;
}
k[0] = strdup(s);
if (!k[0])
goto fail;
k[1] = NULL;
return r;
fail:
strv_free(r);
return NULL;
}
int strv_push(char ***l, char *value) { int strv_push(char ***l, char *value) {
char **c; char **c;
unsigned n; unsigned n;
@ -484,40 +419,11 @@ char **strv_remove(char **l, const char *s) {
/* Drops every occurrence of s in the string list, edits /* Drops every occurrence of s in the string list, edits
* in-place. */ * in-place. */
for (f = t = l; *f; f++) { for (f = t = l; *f; f++)
if (streq(*f, s))
if (streq(*f, s)) {
free(*f); free(*f);
continue; else
} *(t++) = *f;
*(t++) = *f;
}
*t = NULL;
return l;
}
char **strv_remove_prefix(char **l, const char *s) {
char **f, **t;
if (!l)
return NULL;
assert(s);
/* Drops every occurrence of a string prefixed with s in the
* string list, edits in-place. */
for (f = t = l; *f; f++) {
if (startswith(*f, s)) {
free(*f);
continue;
}
*(t++) = *f;
}
*t = NULL; *t = NULL;
return l; return l;
@ -586,14 +492,11 @@ char **strv_split_nulstr(const char *s) {
} }
bool strv_overlap(char **a, char **b) { bool strv_overlap(char **a, char **b) {
char **i, **j; char **i;
STRV_FOREACH(i, a) { STRV_FOREACH(i, a)
STRV_FOREACH(j, b) { if (strv_contains(b, *i))
if (streq(*i, *j)) return true;
return true;
}
}
return false; return false;
} }
@ -616,9 +519,6 @@ char **strv_sort(char **l) {
void strv_print(char **l) { void strv_print(char **l) {
char **s; char **s;
if (!l)
return;
STRV_FOREACH(s, l) STRV_FOREACH(s, l)
puts(*s); puts(*s);
} }

View file

@ -36,14 +36,12 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
char **strv_copy(char * const *l); char **strv_copy(char * const *l);
unsigned strv_length(char * const *l) _pure_; unsigned strv_length(char * const *l) _pure_;
char **strv_merge(char **a, char **b); int strv_extend_strv(char ***a, char **b);
char **strv_merge_concat(char **a, char **b, const char *suffix); int strv_extend_strv_concat(char ***a, char **b, const char *suffix);
char **strv_append(char **l, const char *s);
int strv_extend(char ***l, const char *value); int strv_extend(char ***l, const char *value);
int strv_push(char ***l, char *value); int strv_push(char ***l, char *value);
char **strv_remove(char **l, const char *s); char **strv_remove(char **l, const char *s);
char **strv_remove_prefix(char **l, const char *s);
char **strv_uniq(char **l); char **strv_uniq(char **l);
#define strv_contains(l, s) (!!strv_find((l), (s))) #define strv_contains(l, s) (!!strv_find((l), (s)))

View file

@ -251,13 +251,9 @@ static int parse_argv(int argc, char *argv[]) {
if (*p == '.') if (*p == '.')
*p = '/'; *p = '/';
l = strv_append(arg_prefixes, optarg); if (strv_extend(&arg_prefixes, optarg) < 0)
if (!l)
return log_oom(); return log_oom();
strv_free(arg_prefixes);
arg_prefixes = l;
break; break;
} }

View file

@ -1327,7 +1327,7 @@ static int list_dependencies_one(
char ***units, char ***units,
unsigned int branches) { unsigned int branches) {
_cleanup_strv_free_ char **deps = NULL, **u; _cleanup_strv_free_ char **deps = NULL;
char **c; char **c;
int r = 0; int r = 0;
@ -1335,8 +1335,8 @@ static int list_dependencies_one(
assert(name); assert(name);
assert(units); assert(units);
u = strv_append(*units, name); r = strv_extend(units, name);
if (!u) if (r < 0)
return log_oom(); return log_oom();
r = list_dependencies_get_dependencies(bus, name, &deps); r = list_dependencies_get_dependencies(bus, name, &deps);
@ -1348,7 +1348,7 @@ static int list_dependencies_one(
STRV_FOREACH(c, deps) { STRV_FOREACH(c, deps) {
int state; int state;
if (strv_contains(u, *c)) { if (strv_contains(*units, *c)) {
if (!arg_plain) { if (!arg_plain) {
r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1);
if (r < 0) if (r < 0)
@ -1368,17 +1368,14 @@ static int list_dependencies_one(
return r; return r;
if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) { if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) {
r = list_dependencies_one(bus, *c, level + 1, &u, (branches << 1) | (c[1] == NULL ? 0 : 1)); r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1));
if (r < 0) if (r < 0)
return r; return r;
} }
} }
if (arg_plain) { if (!arg_plain)
strv_free(*units); strv_remove(*units, name);
*units = u;
u = NULL;
}
return 0; return 0;
} }

View file

@ -214,22 +214,6 @@ static void test_strv_split_nulstr(void) {
assert_se(streq(l[3], "str3")); assert_se(streq(l[3], "str3"));
} }
static void test_strv_remove_prefix(void) {
unsigned i = 0;
char **s;
_cleanup_strv_free_ char **l = NULL;
l = strv_new("_one", "_two", "_three", NULL);
assert(l);
l = strv_remove_prefix(l, "_");
assert(l);
STRV_FOREACH(s, l) {
assert_se(streq(*s, input_table_multiple[i++]));
}
}
static void test_strv_parse_nulstr(void) { static void test_strv_parse_nulstr(void) {
_cleanup_strv_free_ char **l = NULL; _cleanup_strv_free_ char **l = NULL;
const char nulstr[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx"; const char nulstr[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx";
@ -289,58 +273,54 @@ static void test_strv_sort(void) {
assert_se(streq(input_table[4], "durian")); assert_se(streq(input_table[4], "durian"));
} }
static void test_strv_merge_concat(void) { static void test_strv_extend_strv_concat(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL; _cleanup_strv_free_ char **a = NULL, **b = NULL;
a = strv_new("without", "suffix", NULL); a = strv_new("without", "suffix", NULL);
b = strv_new("with", "suffix", NULL); b = strv_new("with", "suffix", NULL);
assert_se(a); assert_se(a);
assert_se(b); assert_se(b);
c = strv_merge_concat(a, b, "_suffix"); assert_se(strv_extend_strv_concat(&a, b, "_suffix") >= 0);
assert_se(c);
assert_se(streq(c[0], "without")); assert_se(streq(a[0], "without"));
assert_se(streq(c[1], "suffix")); assert_se(streq(a[1], "suffix"));
assert_se(streq(c[2], "with_suffix")); assert_se(streq(a[2], "with_suffix"));
assert_se(streq(c[3], "suffix_suffix")); assert_se(streq(a[3], "suffix_suffix"));
} }
static void test_strv_merge(void) { static void test_strv_extend_strv(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL; _cleanup_strv_free_ char **a = NULL, **b = NULL;
a = strv_new("abc", "def", "ghi", NULL); a = strv_new("abc", "def", "ghi", NULL);
b = strv_new("jkl", "mno", "pqr", NULL); b = strv_new("jkl", "mno", "pqr", NULL);
assert_se(a); assert_se(a);
assert_se(b); assert_se(b);
c = strv_merge(a, b); assert_se(strv_extend_strv(&a, b) >= 0);
assert_se(c);
assert_se(streq(c[0], "abc")); assert_se(streq(a[0], "abc"));
assert_se(streq(c[1], "def")); assert_se(streq(a[1], "def"));
assert_se(streq(c[2], "ghi")); assert_se(streq(a[2], "ghi"));
assert_se(streq(c[3], "jkl")); assert_se(streq(a[3], "jkl"));
assert_se(streq(c[4], "mno")); assert_se(streq(a[4], "mno"));
assert_se(streq(c[5], "pqr")); assert_se(streq(a[5], "pqr"));
assert_se(strv_length(c) == 6); assert_se(strv_length(a) == 6);
} }
static void test_strv_append(void) { static void test_strv_extend(void) {
_cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL; _cleanup_strv_free_ char **a = NULL, **b = NULL;
a = strv_new("test", "test1", NULL); a = strv_new("test", "test1", NULL);
assert_se(a); assert_se(a);
b = strv_append(a, "test2"); assert_se(strv_extend(&a, "test2") >= 0);
c = strv_append(NULL, "test3"); assert_se(strv_extend(&b, "test3") >= 0);
assert_se(b);
assert_se(c);
assert_se(streq(b[0], "test")); assert_se(streq(a[0], "test"));
assert_se(streq(b[1], "test1")); assert_se(streq(a[1], "test1"));
assert_se(streq(b[2], "test2")); assert_se(streq(a[2], "test2"));
assert_se(streq(c[0], "test3")); assert_se(streq(b[0], "test3"));
} }
static void test_strv_foreach(void) { static void test_strv_foreach(void) {
@ -436,12 +416,11 @@ int main(int argc, char *argv[]) {
test_strv_split_newlines(); test_strv_split_newlines();
test_strv_split_nulstr(); test_strv_split_nulstr();
test_strv_parse_nulstr(); test_strv_parse_nulstr();
test_strv_remove_prefix();
test_strv_overlap(); test_strv_overlap();
test_strv_sort(); test_strv_sort();
test_strv_merge(); test_strv_extend_strv();
test_strv_merge_concat(); test_strv_extend_strv_concat();
test_strv_append(); test_strv_extend();
test_strv_from_stdarg_alloca(); test_strv_from_stdarg_alloca();
return 0; return 0;