strv: make iterator in STRV_FOREACH() declaread in the loop
This also avoids multiple evaluations in STRV_FOREACH_BACKWARDS()
This commit is contained in:
@@ -145,7 +145,7 @@ static int conf_files_list_strv_internal(
|
||||
|
||||
_cleanup_hashmap_free_ Hashmap *fh = NULL;
|
||||
_cleanup_set_free_free_ Set *masked = NULL;
|
||||
char **files, **p;
|
||||
char **files;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
@@ -202,11 +202,8 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
|
||||
int c;
|
||||
|
||||
c = base_cmp((char* const*) *strv + i, (char* const*) &path);
|
||||
if (c == 0) {
|
||||
char **dir;
|
||||
|
||||
if (c == 0)
|
||||
/* Oh, there already is an entry with a matching name (the last component). */
|
||||
|
||||
STRV_FOREACH(dir, dirs) {
|
||||
_cleanup_free_ char *rdir = NULL;
|
||||
char *p1, *p2;
|
||||
@@ -233,7 +230,7 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
|
||||
}
|
||||
}
|
||||
|
||||
} else if (c > 0)
|
||||
else if (c > 0)
|
||||
/* Following files have lower priority, let's go insert our
|
||||
* new entry. */
|
||||
break;
|
||||
|
||||
@@ -537,7 +537,6 @@ static void write_env_var(FILE *f, const char *v) {
|
||||
int write_env_file(const char *fname, char **l) {
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
_cleanup_free_ char *p = NULL;
|
||||
char **i;
|
||||
int r;
|
||||
|
||||
assert(fname);
|
||||
|
||||
+4
-15
@@ -96,8 +96,6 @@ bool env_assignment_is_valid(const char *e) {
|
||||
}
|
||||
|
||||
bool strv_env_is_valid(char **e) {
|
||||
char **p, **q;
|
||||
|
||||
STRV_FOREACH(p, e) {
|
||||
size_t k;
|
||||
|
||||
@@ -115,8 +113,6 @@ bool strv_env_is_valid(char **e) {
|
||||
}
|
||||
|
||||
bool strv_env_name_is_valid(char **l) {
|
||||
char **p;
|
||||
|
||||
STRV_FOREACH(p, l) {
|
||||
if (!env_name_is_valid(*p))
|
||||
return false;
|
||||
@@ -129,8 +125,6 @@ bool strv_env_name_is_valid(char **l) {
|
||||
}
|
||||
|
||||
bool strv_env_name_or_assignment_is_valid(char **l) {
|
||||
char **p;
|
||||
|
||||
STRV_FOREACH(p, l) {
|
||||
if (!env_assignment_is_valid(*p) && !env_name_is_valid(*p))
|
||||
return false;
|
||||
@@ -272,7 +266,7 @@ static bool env_entry_has_name(const char *entry, const char *name) {
|
||||
|
||||
char **strv_env_delete(char **x, size_t n_lists, ...) {
|
||||
size_t n, i = 0;
|
||||
char **k, **r;
|
||||
char **r;
|
||||
va_list ap;
|
||||
|
||||
/* Deletes every entry from x that is mentioned in the other
|
||||
@@ -287,7 +281,7 @@ char **strv_env_delete(char **x, size_t n_lists, ...) {
|
||||
STRV_FOREACH(k, x) {
|
||||
va_start(ap, n_lists);
|
||||
for (size_t v = 0; v < n_lists; v++) {
|
||||
char **l, **j;
|
||||
char **l;
|
||||
|
||||
l = va_arg(ap, char**);
|
||||
STRV_FOREACH(j, l)
|
||||
@@ -379,7 +373,6 @@ char **strv_env_unset_many(char **l, ...) {
|
||||
|
||||
int strv_env_replace_consume(char ***l, char *p) {
|
||||
const char *t, *name;
|
||||
char **f;
|
||||
int r;
|
||||
|
||||
assert(p);
|
||||
@@ -467,8 +460,6 @@ int strv_env_assign(char ***l, const char *key, const char *value) {
|
||||
}
|
||||
|
||||
char *strv_env_get_n(char **l, const char *name, size_t k, unsigned flags) {
|
||||
char **i;
|
||||
|
||||
assert(name);
|
||||
|
||||
if (k <= 0)
|
||||
@@ -496,7 +487,7 @@ char *strv_env_get(char **l, const char *name) {
|
||||
}
|
||||
|
||||
char *strv_env_pairs_get(char **l, const char *name) {
|
||||
char **key, **value, *result = NULL;
|
||||
char *result = NULL;
|
||||
|
||||
assert(name);
|
||||
|
||||
@@ -508,7 +499,6 @@ char *strv_env_pairs_get(char **l, const char *name) {
|
||||
}
|
||||
|
||||
char **strv_env_clean_with_callback(char **e, void (*invalid_callback)(const char *p, void *userdata), void *userdata) {
|
||||
char **p, **q;
|
||||
int k = 0;
|
||||
|
||||
STRV_FOREACH(p, e) {
|
||||
@@ -702,7 +692,7 @@ char *replace_env_n(const char *format, size_t n, char **env, unsigned flags) {
|
||||
}
|
||||
|
||||
char **replace_env_argv(char **argv, char **env) {
|
||||
char **ret, **i;
|
||||
char **ret;
|
||||
size_t k = 0, l = 0;
|
||||
|
||||
l = strv_length(argv);
|
||||
@@ -832,7 +822,6 @@ int setenv_systemd_exec_pid(bool update_only) {
|
||||
int getenv_path_list(const char *name, char ***ret_paths) {
|
||||
_cleanup_strv_free_ char **l = NULL;
|
||||
const char *e;
|
||||
char **p;
|
||||
int r;
|
||||
|
||||
assert(name);
|
||||
|
||||
@@ -549,7 +549,6 @@ char* quote_command_line(char **argv, ShellEscapeFlags flags) {
|
||||
|
||||
assert(argv);
|
||||
|
||||
char **a;
|
||||
STRV_FOREACH(a, argv) {
|
||||
_cleanup_free_ char *t = NULL;
|
||||
|
||||
|
||||
@@ -1042,8 +1042,6 @@ static int search_and_fopen_internal(
|
||||
FILE **ret,
|
||||
char **ret_path) {
|
||||
|
||||
char **i;
|
||||
|
||||
assert(path);
|
||||
assert(mode);
|
||||
assert(ret);
|
||||
|
||||
@@ -1864,7 +1864,6 @@ int _set_put_strdup_full(Set **s, const struct hash_ops *hash_ops, const char *p
|
||||
|
||||
int _set_put_strdupv_full(Set **s, const struct hash_ops *hash_ops, char **l HASHMAP_DEBUG_PARAMS) {
|
||||
int n = 0, r;
|
||||
char **i;
|
||||
|
||||
assert(s);
|
||||
|
||||
|
||||
@@ -58,7 +58,6 @@ int _ordered_set_put_strdup(OrderedSet **s, const char *p HASHMAP_DEBUG_PARAMS)
|
||||
|
||||
int _ordered_set_put_strdupv(OrderedSet **s, char **l HASHMAP_DEBUG_PARAMS) {
|
||||
int n = 0, r;
|
||||
char **i;
|
||||
|
||||
STRV_FOREACH(i, l) {
|
||||
r = _ordered_set_put_strdup(s, *i HASHMAP_DEBUG_PASS_ARGS);
|
||||
|
||||
@@ -273,7 +273,6 @@ int load_os_release_pairs(const char *root, char ***ret) {
|
||||
|
||||
int load_os_release_pairs_with_prefix(const char *root, const char *prefix, char ***ret) {
|
||||
_cleanup_strv_free_ char **os_release_pairs = NULL, **os_release_pairs_prefixed = NULL;
|
||||
char **p, **q;
|
||||
int r;
|
||||
|
||||
r = load_os_release_pairs(root, &os_release_pairs);
|
||||
|
||||
@@ -463,7 +463,6 @@ static int patch_root_prefix(char **p, const char *root_dir) {
|
||||
}
|
||||
|
||||
static int patch_root_prefix_strv(char **l, const char *root_dir) {
|
||||
char **i;
|
||||
int r;
|
||||
|
||||
if (!root_dir)
|
||||
|
||||
+7
-13
@@ -202,9 +202,9 @@ int path_make_relative(const char *from, const char *to, char **ret) {
|
||||
}
|
||||
|
||||
char* path_startswith_strv(const char *p, char **set) {
|
||||
char **s, *t;
|
||||
|
||||
STRV_FOREACH(s, set) {
|
||||
char *t;
|
||||
|
||||
t = path_startswith(p, *s);
|
||||
if (t)
|
||||
return t;
|
||||
@@ -214,7 +214,6 @@ char* path_startswith_strv(const char *p, char **set) {
|
||||
}
|
||||
|
||||
int path_strv_make_absolute_cwd(char **l) {
|
||||
char **s;
|
||||
int r;
|
||||
|
||||
/* Goes through every item in the string list and makes it
|
||||
@@ -236,7 +235,6 @@ int path_strv_make_absolute_cwd(char **l) {
|
||||
}
|
||||
|
||||
char **path_strv_resolve(char **l, const char *root) {
|
||||
char **s;
|
||||
unsigned k = 0;
|
||||
bool enomem = false;
|
||||
int r;
|
||||
@@ -700,12 +698,12 @@ int find_executable_full(const char *name, const char *root, char **exec_search_
|
||||
p = DEFAULT_PATH;
|
||||
|
||||
if (exec_search_path) {
|
||||
char **element;
|
||||
|
||||
STRV_FOREACH(element, exec_search_path) {
|
||||
_cleanup_free_ char *full_path = NULL;
|
||||
|
||||
if (!path_is_absolute(*element))
|
||||
continue;
|
||||
|
||||
full_path = path_join(*element, name);
|
||||
if (!full_path)
|
||||
return -ENOMEM;
|
||||
@@ -754,7 +752,6 @@ int find_executable_full(const char *name, const char *root, char **exec_search_
|
||||
|
||||
bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) {
|
||||
bool changed = false, originally_unset;
|
||||
const char* const* i;
|
||||
|
||||
assert(timestamp);
|
||||
|
||||
@@ -1339,7 +1336,7 @@ int systemd_installation_has_version(const char *root, unsigned minimal_version)
|
||||
|
||||
_cleanup_strv_free_ char **names = NULL;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
char *c, **name;
|
||||
char *c;
|
||||
|
||||
path = path_join(root, pattern);
|
||||
if (!path)
|
||||
@@ -1411,8 +1408,6 @@ bool empty_or_root(const char *path) {
|
||||
}
|
||||
|
||||
bool path_strv_contains(char **l, const char *path) {
|
||||
char **i;
|
||||
|
||||
STRV_FOREACH(i, l)
|
||||
if (path_equal(*i, path))
|
||||
return true;
|
||||
@@ -1421,10 +1416,9 @@ bool path_strv_contains(char **l, const char *path) {
|
||||
}
|
||||
|
||||
bool prefixed_path_strv_contains(char **l, const char *path) {
|
||||
char **i, *j;
|
||||
|
||||
STRV_FOREACH(i, l) {
|
||||
j = *i;
|
||||
const char *j = *i;
|
||||
|
||||
if (*j == '-')
|
||||
j++;
|
||||
if (*j == '+')
|
||||
|
||||
@@ -215,7 +215,6 @@ int get_process_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags
|
||||
assert(!(flags & PROCESS_CMDLINE_USE_LOCALE));
|
||||
|
||||
_cleanup_strv_free_ char **args = NULL;
|
||||
char **p;
|
||||
|
||||
args = strv_parse_nulstr(t, k);
|
||||
if (!args)
|
||||
|
||||
+4
-29
@@ -17,8 +17,6 @@
|
||||
#include "strv.h"
|
||||
|
||||
char* strv_find(char * const *l, const char *name) {
|
||||
char * const *i;
|
||||
|
||||
assert(name);
|
||||
|
||||
STRV_FOREACH(i, l)
|
||||
@@ -29,8 +27,6 @@ char* strv_find(char * const *l, const char *name) {
|
||||
}
|
||||
|
||||
char* strv_find_case(char * const *l, const char *name) {
|
||||
char * const *i;
|
||||
|
||||
assert(name);
|
||||
|
||||
STRV_FOREACH(i, l)
|
||||
@@ -41,8 +37,6 @@ char* strv_find_case(char * const *l, const char *name) {
|
||||
}
|
||||
|
||||
char* strv_find_prefix(char * const *l, const char *name) {
|
||||
char * const *i;
|
||||
|
||||
assert(name);
|
||||
|
||||
STRV_FOREACH(i, l)
|
||||
@@ -53,14 +47,14 @@ char* strv_find_prefix(char * const *l, const char *name) {
|
||||
}
|
||||
|
||||
char* strv_find_startswith(char * const *l, const char *name) {
|
||||
char * const *i, *e;
|
||||
|
||||
assert(name);
|
||||
|
||||
/* Like strv_find_prefix, but actually returns only the
|
||||
* suffix, not the whole item */
|
||||
|
||||
STRV_FOREACH(i, l) {
|
||||
char *e;
|
||||
|
||||
e = startswith(*i, name);
|
||||
if (e)
|
||||
return e;
|
||||
@@ -70,8 +64,6 @@ char* strv_find_startswith(char * const *l, const char *name) {
|
||||
}
|
||||
|
||||
char** strv_free(char **l) {
|
||||
char **k;
|
||||
|
||||
STRV_FOREACH(k, l)
|
||||
free(*k);
|
||||
|
||||
@@ -79,8 +71,6 @@ char** strv_free(char **l) {
|
||||
}
|
||||
|
||||
char** strv_free_erase(char **l) {
|
||||
char **i;
|
||||
|
||||
STRV_FOREACH(i, l)
|
||||
erase_and_freep(i);
|
||||
|
||||
@@ -89,7 +79,7 @@ char** strv_free_erase(char **l) {
|
||||
|
||||
char** strv_copy(char * const *l) {
|
||||
_cleanup_strv_free_ char **result = NULL;
|
||||
char **k, * const *i;
|
||||
char **k;
|
||||
|
||||
result = new(char*, strv_length(l) + 1);
|
||||
if (!result)
|
||||
@@ -108,7 +98,6 @@ char** strv_copy(char * const *l) {
|
||||
}
|
||||
|
||||
size_t strv_length(char * const *l) {
|
||||
char * const *i;
|
||||
size_t n = 0;
|
||||
|
||||
STRV_FOREACH(i, l)
|
||||
@@ -168,8 +157,8 @@ char** strv_new_internal(const char *x, ...) {
|
||||
}
|
||||
|
||||
int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates) {
|
||||
char * const *s, **t;
|
||||
size_t p, q, i = 0;
|
||||
char **t;
|
||||
|
||||
assert(a);
|
||||
|
||||
@@ -214,7 +203,6 @@ rollback:
|
||||
}
|
||||
|
||||
int strv_extend_strv_concat(char ***a, char * const *b, const char *suffix) {
|
||||
char * const *s;
|
||||
int r;
|
||||
|
||||
STRV_FOREACH(s, b) {
|
||||
@@ -364,7 +352,6 @@ int strv_split_colon_pairs(char ***t, const char *s) {
|
||||
}
|
||||
|
||||
char* strv_join_full(char * const *l, const char *separator, const char *prefix, bool unescape_separators) {
|
||||
char * const *s;
|
||||
char *r, *e;
|
||||
size_t n, k, m;
|
||||
|
||||
@@ -592,8 +579,6 @@ int strv_extend_front(char ***l, const char *value) {
|
||||
}
|
||||
|
||||
char** strv_uniq(char **l) {
|
||||
char **i;
|
||||
|
||||
/* Drops duplicate entries. The first identical string will be
|
||||
* kept, the others dropped */
|
||||
|
||||
@@ -604,8 +589,6 @@ char** strv_uniq(char **l) {
|
||||
}
|
||||
|
||||
bool strv_is_uniq(char * const *l) {
|
||||
char * const *i;
|
||||
|
||||
STRV_FOREACH(i, l)
|
||||
if (strv_contains(i+1, *i))
|
||||
return false;
|
||||
@@ -714,7 +697,6 @@ int strv_make_nulstr(char * const *l, char **ret, size_t *ret_size) {
|
||||
*/
|
||||
|
||||
_cleanup_free_ char *m = NULL;
|
||||
char * const *i;
|
||||
size_t n = 0;
|
||||
|
||||
assert(ret);
|
||||
@@ -751,8 +733,6 @@ int strv_make_nulstr(char * const *l, char **ret, size_t *ret_size) {
|
||||
}
|
||||
|
||||
bool strv_overlap(char * const *a, char * const *b) {
|
||||
char * const *i;
|
||||
|
||||
STRV_FOREACH(i, a)
|
||||
if (strv_contains(b, *i))
|
||||
return true;
|
||||
@@ -792,8 +772,6 @@ int strv_compare(char * const *a, char * const *b) {
|
||||
}
|
||||
|
||||
void strv_print(char * const *l) {
|
||||
char * const *s;
|
||||
|
||||
STRV_FOREACH(s, l)
|
||||
puts(*s);
|
||||
}
|
||||
@@ -827,8 +805,6 @@ char** strv_reverse(char **l) {
|
||||
}
|
||||
|
||||
char** strv_shell_escape(char **l, const char *bad) {
|
||||
char **s;
|
||||
|
||||
/* Escapes every character in every string in l that is in bad,
|
||||
* edits in-place, does not roll-back on error. */
|
||||
|
||||
@@ -911,7 +887,6 @@ rollback:
|
||||
|
||||
int fputstrv(FILE *f, char * const *l, const char *separator, bool *space) {
|
||||
bool b = false;
|
||||
char * const *s;
|
||||
int r;
|
||||
|
||||
/* Like fputs(), but for strv, and with a less stupid argument order */
|
||||
|
||||
+23
-12
@@ -122,19 +122,30 @@ static inline int strv_from_nulstr(char ***a, const char *nulstr) {
|
||||
|
||||
bool strv_overlap(char * const *a, char * const *b) _pure_;
|
||||
|
||||
#define _STRV_FOREACH(s, l, i) \
|
||||
for (typeof(*(l)) *s, *i = (l); (s = i) && *i; i++)
|
||||
|
||||
#define STRV_FOREACH(s, l) \
|
||||
for ((s) = (l); (s) && *(s); (s)++)
|
||||
_STRV_FOREACH(s, l, UNIQ_T(i, UNIQ))
|
||||
|
||||
#define STRV_FOREACH_BACKWARDS(s, l) \
|
||||
for (s = ({ \
|
||||
typeof(l) _l = l; \
|
||||
_l ? _l + strv_length(_l) - 1U : NULL; \
|
||||
}); \
|
||||
(l) && ((s) >= (l)); \
|
||||
(s)--)
|
||||
#define _STRV_FOREACH_BACKWARDS(s, l, h, i) \
|
||||
for (typeof(*(l)) *s, *h = (l), *i = ({ \
|
||||
size_t _len = strv_length(h); \
|
||||
_len > 0 ? h + _len - 1 : NULL; \
|
||||
}); \
|
||||
i && (s = i) >= h; \
|
||||
i--)
|
||||
|
||||
#define STRV_FOREACH_PAIR(x, y, l) \
|
||||
for ((x) = (l), (y) = (x) ? (x+1) : NULL; (x) && *(x) && *(y); (x) += 2, (y) = (x + 1))
|
||||
#define STRV_FOREACH_BACKWARDS(s, l) \
|
||||
_STRV_FOREACH_BACKWARDS(s, l, UNIQ_T(h, UNIQ), UNIQ_T(i, UNIQ))
|
||||
|
||||
#define _STRV_FOREACH_PAIR(x, y, l, i) \
|
||||
for (typeof(*l) *x, *y, *i = (l); \
|
||||
i && *(x = i) && *(y = i + 1); \
|
||||
i += 2)
|
||||
|
||||
#define STRV_FOREACH_PAIR(x, y, l) \
|
||||
_STRV_FOREACH_PAIR(x, y, l, UNIQ_T(i, UNIQ))
|
||||
|
||||
char** strv_sort(char **l);
|
||||
void strv_print(char * const *l);
|
||||
@@ -185,7 +196,7 @@ void strv_print(char * const *l);
|
||||
#define STARTSWITH_SET(p, ...) \
|
||||
({ \
|
||||
const char *_p = (p); \
|
||||
char *_found = NULL, **_i; \
|
||||
char *_found = NULL; \
|
||||
STRV_FOREACH(_i, STRV_MAKE(__VA_ARGS__)) { \
|
||||
_found = startswith(_p, *_i); \
|
||||
if (_found) \
|
||||
@@ -197,7 +208,7 @@ void strv_print(char * const *l);
|
||||
#define ENDSWITH_SET(p, ...) \
|
||||
({ \
|
||||
const char *_p = (p); \
|
||||
char *_found = NULL, **_i; \
|
||||
char *_found = NULL; \
|
||||
STRV_FOREACH(_i, STRV_MAKE(__VA_ARGS__)) { \
|
||||
_found = endswith(_p, *_i); \
|
||||
if (_found) \
|
||||
|
||||
@@ -209,8 +209,7 @@ bool lookup_paths_timestamp_hash_same(const LookupPaths *lp, uint64_t timestamp_
|
||||
|
||||
siphash24_init(&state, HASH_KEY.bytes);
|
||||
|
||||
char **dir;
|
||||
STRV_FOREACH(dir, (char**) lp->search_path) {
|
||||
STRV_FOREACH(dir, lp->search_path) {
|
||||
struct stat st;
|
||||
|
||||
if (lookup_paths_mtime_exclude(lp, *dir))
|
||||
@@ -281,7 +280,6 @@ int unit_file_build_name_map(
|
||||
_cleanup_hashmap_free_ Hashmap *ids = NULL, *names = NULL;
|
||||
_cleanup_set_free_free_ Set *paths = NULL;
|
||||
uint64_t timestamp_hash;
|
||||
char **dir;
|
||||
int r;
|
||||
|
||||
/* Before doing anything, check if the timestamp hash that was passed is still valid.
|
||||
@@ -299,7 +297,7 @@ int unit_file_build_name_map(
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
STRV_FOREACH(dir, (char**) lp->search_path) {
|
||||
STRV_FOREACH(dir, lp->search_path) {
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
|
||||
d = opendir(*dir);
|
||||
@@ -550,7 +548,7 @@ static int add_names(
|
||||
Set **names,
|
||||
const char *name) {
|
||||
|
||||
char **aliases, **alias;
|
||||
char **aliases;
|
||||
int r;
|
||||
|
||||
assert(name_type == UNIT_NAME_PLAIN || instance);
|
||||
|
||||
Reference in New Issue
Block a user