strv: add helper to extend strv from both sides

Also, use the more correct type of 'const char* const*' for the input strv.
This requires adding the cast in a few places, but also allows to remove some
casts in others.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2024-03-07 18:49:44 +01:00
parent 4bf32eac52
commit 9bc7493098
7 changed files with 29 additions and 10 deletions
+1 -1
View File
@@ -369,7 +369,7 @@ int conf_files_list_dropins(
assert(dirs);
suffix = strjoina("/", dropin_dirname);
r = strv_extend_strv_concat(&dropin_dirs, (char**) dirs, suffix);
r = strv_extend_strv_concat(&dropin_dirs, dirs, suffix);
if (r < 0)
return r;
+2 -2
View File
@@ -214,7 +214,7 @@ static char** user_dirs(
persistent_config) < 0)
return NULL;
if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0)
if (strv_extend_strv_concat(&res, (const char* const*) config_dirs, "/systemd/user") < 0)
return NULL;
/* global config has lower priority than the user config of the same type */
@@ -232,7 +232,7 @@ static char** user_dirs(
data_home) < 0)
return NULL;
if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0)
if (strv_extend_strv_concat(&res, (const char* const*) data_dirs, "/systemd/user") < 0)
return NULL;
if (strv_extend_strv(&res, (char**) user_data_unit_paths, false) < 0)
+2 -2
View File
@@ -242,13 +242,13 @@ rollback:
return -ENOMEM;
}
int strv_extend_strv_concat(char ***a, char * const *b, const char *suffix) {
int strv_extend_strv_biconcat(char ***a, const char *prefix, const char* const *b, const char *suffix) {
int r;
STRV_FOREACH(s, b) {
char *v;
v = strjoin(*s, suffix);
v = strjoin(strempty(prefix), *s, suffix);
if (!v)
return -ENOMEM;
+4 -1
View File
@@ -43,7 +43,10 @@ int strv_copy_unless_empty(char * const *l, char ***ret);
size_t strv_length(char * const *l) _pure_;
int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates);
int strv_extend_strv_concat(char ***a, char * const *b, const char *suffix);
int strv_extend_strv_biconcat(char ***a, const char *prefix, const char* const *b, const char *suffix);
static inline int strv_extend_strv_concat(char ***a, const char* const *b, const char *suffix) {
return strv_extend_strv_biconcat(a, NULL, b, suffix);
}
int strv_prepend(char ***l, const char *value);
/* _with_size() are lower-level functions where the size can be provided externally,