diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/path-lookup.c | 83 | ||||
-rw-r--r-- | src/shared/strv.c | 158 | ||||
-rw-r--r-- | src/shared/strv.h | 6 |
3 files changed, 52 insertions, 195 deletions
diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c index 1a47ea9ce7..e2ca9420d9 100644 --- a/src/shared/path-lookup.c +++ b/src/shared/path-lookup.c @@ -90,9 +90,9 @@ static char** user_dirs( }; const char *home, *e; - char *config_home = NULL, *data_home = NULL; - char **config_dirs = NULL, **data_dirs = NULL; - char **r = NULL, **t; + _cleanup_free_ char *config_home = NULL, *data_home = NULL; + _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; + char **r = NULL; /* Implement the mechanisms defined in * @@ -150,89 +150,48 @@ static char** user_dirs( goto fail; /* Now merge everything we found. */ - if (generator_early) { - t = strv_append(r, generator_early); - if (!t) + if (generator_early) + if (strv_extend(&r, generator_early) < 0) goto fail; - strv_free(r); - r = t; - } - if (config_home) { - t = strv_append(r, config_home); - if (!t) + if (config_home) + if (strv_extend(&r, config_home) < 0) goto fail; - strv_free(r); - r = t; - } - if (!strv_isempty(config_dirs)) { - t = strv_merge_concat(r, config_dirs, "/systemd/user"); - if (!t) - goto finish; - strv_free(r); - r = t; - } + if (!strv_isempty(config_dirs)) + if (strv_extend_strv_concat(&r, config_dirs, "/systemd/user") < 0) + goto fail; - t = strv_merge(r, (char**) config_unit_paths); - if (!t) + if (strv_extend_strv(&r, (char**) config_unit_paths) < 0) goto fail; - strv_free(r); - r = t; - if (generator) { - t = strv_append(r, generator); - if (!t) + if (generator) + if (strv_extend(&r, generator) < 0) goto fail; - strv_free(r); - r = t; - } - if (data_home) { - t = strv_append(r, data_home); - if (!t) + if (data_home) + if (strv_extend(&r, data_home) < 0) goto fail; - strv_free(r); - r = t; - } - if (!strv_isempty(data_dirs)) { - t = strv_merge_concat(r, data_dirs, "/systemd/user"); - if (!t) + if (!strv_isempty(data_dirs)) + if (strv_extend_strv_concat(&r, data_dirs, "/systemd/user") < 0) goto fail; - strv_free(r); - r = t; - } - t = strv_merge(r, (char**) data_unit_paths); - if (!t) + if (strv_extend_strv(&r, (char**) data_unit_paths) < 0) goto fail; - strv_free(r); - r = t; - if (generator_late) { - t = strv_append(r, generator_late); - if (!t) + if (generator_late) + if (strv_extend(&r, generator_late) < 0) goto fail; - strv_free(r); - r = t; - } if (!path_strv_make_absolute_cwd(r)) goto fail; -finish: - free(config_home); - strv_free(config_dirs); - free(data_home); - strv_free(data_dirs); - return r; fail: strv_free(r); - r = NULL; - goto finish; + return NULL; } int lookup_paths_init( diff --git a/src/shared/strv.c b/src/shared/strv.c index 607c221ae6..13deba7be7 100644 --- a/src/shared/strv.c +++ b/src/shared/strv.c @@ -166,72 +166,38 @@ char **strv_new(const char *x, ...) { return r; } -char **strv_merge(char **a, char **b) { - char **r, **k; - - if (!a) - return strv_copy(b); - - if (!b) - 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; - } +int strv_extend_strv(char ***a, char **b) { + int r; + char **s; - for (; *b; k++, b++) { - *k = strdup(*b); - if (!*k) - goto fail; + STRV_FOREACH(s, b) { + r = strv_extend(a, *s); + if (r < 0) + return r; } - *k = NULL; - return r; - -fail: - strv_free(r); - return NULL; + return 0; } -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 */ +int strv_extend_strv_concat(char ***a, char **b, const char *suffix) { + int r; + char **s; - if (!b) - return strv_copy(a); + STRV_FOREACH(s, b) { + char *v; - r = new(char*, strv_length(a) + strv_length(b) + 1); - if (!r) - return NULL; + v = strappend(*s, suffix); + if (!v) + return -ENOMEM; - k = r; - if (a) - for (; *a; k++, a++) { - *k = strdup(*a); - if (!*k) - goto fail; + r = strv_push(a, v); + if (r < 0) { + free(v); + return r; } - - for (; *b; k++, b++) { - *k = strappend(*b, suffix); - if (!*k) - goto fail; } - *k = NULL; - return r; - -fail: - strv_free(r); - return NULL; - + return 0; } char **strv_split(const char *s, const char *separator) { @@ -393,37 +359,6 @@ char *strv_join_quoted(char **l) { 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) { char **c; unsigned n; @@ -484,40 +419,11 @@ char **strv_remove(char **l, const char *s) { /* Drops every occurrence of s in the string list, edits * in-place. */ - for (f = t = l; *f; f++) { - - if (streq(*f, s)) { - free(*f); - continue; - } - - *(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)) { + for (f = t = l; *f; f++) + if (streq(*f, s)) free(*f); - continue; - } - - *(t++) = *f; - } + else + *(t++) = *f; *t = NULL; return l; @@ -586,14 +492,11 @@ char **strv_split_nulstr(const char *s) { } bool strv_overlap(char **a, char **b) { - char **i, **j; + char **i; - STRV_FOREACH(i, a) { - STRV_FOREACH(j, b) { - if (streq(*i, *j)) - return true; - } - } + STRV_FOREACH(i, a) + if (strv_contains(b, *i)) + return true; return false; } @@ -616,9 +519,6 @@ char **strv_sort(char **l) { void strv_print(char **l) { char **s; - if (!l) - return; - STRV_FOREACH(s, l) puts(*s); } diff --git a/src/shared/strv.h b/src/shared/strv.h index daf9ad0d0b..715bc5419c 100644 --- a/src/shared/strv.h +++ b/src/shared/strv.h @@ -36,14 +36,12 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free); char **strv_copy(char * const *l); unsigned strv_length(char * const *l) _pure_; -char **strv_merge(char **a, char **b); -char **strv_merge_concat(char **a, char **b, const char *suffix); -char **strv_append(char **l, const char *s); +int strv_extend_strv(char ***a, char **b); +int strv_extend_strv_concat(char ***a, char **b, const char *suffix); int strv_extend(char ***l, const char *value); int strv_push(char ***l, char *value); char **strv_remove(char **l, const char *s); -char **strv_remove_prefix(char **l, const char *s); char **strv_uniq(char **l); #define strv_contains(l, s) (!!strv_find((l), (s))) |