1
0
mirror of https://github.com/systemd/systemd synced 2026-04-09 00:24:49 +02:00

Compare commits

..

6 Commits

Author SHA1 Message Date
Yu Watanabe
f5caacec1c
Merge pull request #21533 from yuwata/network-trivial-follow-ups
network: trivial follow-ups
2021-11-27 06:42:19 +09:00
Yu Watanabe
e76d491c87
Merge pull request #21530 from keszybz/strv-cleanup
Modernize style and drop strv_free_free
2021-11-27 06:41:56 +09:00
Zbigniew Jędrzejewski-Szmek
1ba193d73e basic/strv: drop strv_free_free
I think the function name is confusing: we generally say "free_free" when
both keys and values are freed in a hash map, but here the type is an
array of strvs, so the name should be something like strv_array_free.

The function is unused since 143fadf369a18449464956206226761e49be1928 (2018),
let's just drop it.
2021-11-26 14:58:44 +01:00
Zbigniew Jędrzejewski-Szmek
14337c374a basic/strv: inline variables and modernize style a bit 2021-11-26 14:52:03 +01:00
Yu Watanabe
36edc2c956 network: update comment
Addresses https://github.com/systemd/systemd/pull/21517#discussion_r757096584.
2021-11-26 21:05:52 +09:00
Yu Watanabe
4bd2c4e8e0 netif-util: update log message
Follow-up for 37593b7c488f7b957936500158f200af16534c6b.
2021-11-26 21:05:52 +09:00
5 changed files with 57 additions and 93 deletions

View File

@ -16,7 +16,7 @@
#include "string-util.h"
#include "strv.h"
char *strv_find(char * const *l, const char *name) {
char* strv_find(char * const *l, const char *name) {
char * const *i;
assert(name);
@ -28,7 +28,7 @@ char *strv_find(char * const *l, const char *name) {
return NULL;
}
char *strv_find_case(char * const *l, const char *name) {
char* strv_find_case(char * const *l, const char *name) {
char * const *i;
assert(name);
@ -40,7 +40,7 @@ char *strv_find_case(char * const *l, const char *name) {
return NULL;
}
char *strv_find_prefix(char * const *l, const char *name) {
char* strv_find_prefix(char * const *l, const char *name) {
char * const *i;
assert(name);
@ -52,7 +52,7 @@ char *strv_find_prefix(char * const *l, const char *name) {
return NULL;
}
char *strv_find_startswith(char * const *l, const char *name) {
char* strv_find_startswith(char * const *l, const char *name) {
char * const *i, *e;
assert(name);
@ -69,19 +69,17 @@ char *strv_find_startswith(char * const *l, const char *name) {
return NULL;
}
char **strv_free(char **l) {
char **k;
char** strv_free(char **l) {
if (!l)
return NULL;
for (k = l; *k; k++)
for (char **k = l; *k; k++)
free(*k);
return mfree(l);
}
char **strv_free_erase(char **l) {
char** strv_free_erase(char **l) {
char **i;
STRV_FOREACH(i, l)
@ -90,7 +88,7 @@ char **strv_free_erase(char **l) {
return mfree(l);
}
char **strv_copy(char * const *l) {
char** strv_copy(char * const *l) {
char **r, **k;
k = r = new(char*, strv_length(l) + 1);
@ -122,7 +120,7 @@ size_t strv_length(char * const *l) {
return n;
}
char **strv_new_ap(const char *x, va_list ap) {
char** strv_new_ap(const char *x, va_list ap) {
_cleanup_strv_free_ char **a = NULL;
size_t n = 0, i = 0;
va_list aq;
@ -161,7 +159,7 @@ char **strv_new_ap(const char *x, va_list ap) {
return TAKE_PTR(a);
}
char **strv_new_internal(const char *x, ...) {
char** strv_new_internal(const char *x, ...) {
char **r;
va_list ap;
@ -174,7 +172,7 @@ 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, j;
size_t p, q, i = 0;
assert(a);
@ -195,7 +193,6 @@ int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates) {
*a = t;
STRV_FOREACH(s, b) {
if (filter_duplicates && strv_contains(t, *s))
continue;
@ -212,7 +209,7 @@ int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates) {
return (int) i;
rollback:
for (j = 0; j < i; j++)
for (size_t j = 0; j < i; j++)
free(t[p + j]);
t[p] = NULL;
@ -285,7 +282,6 @@ int strv_split_full(char ***t, const char *s, const char *separators, ExtractFla
return -ENOMEM;
l[n++] = TAKE_PTR(word);
l[n] = NULL;
}
@ -370,7 +366,7 @@ int strv_split_colon_pairs(char ***t, const char *s) {
return (int) n;
}
char *strv_join_full(char * const *l, const char *separator, const char *prefix, bool unescape_separators) {
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;
@ -477,7 +473,7 @@ int strv_push_pair(char ***l, char *a, char *b) {
int strv_insert(char ***l, size_t position, char *value) {
char **c;
size_t n, m, i;
size_t n, m;
if (!value)
return 0;
@ -494,18 +490,14 @@ int strv_insert(char ***l, size_t position, char *value) {
if (!c)
return -ENOMEM;
for (i = 0; i < position; i++)
for (size_t i = 0; i < position; i++)
c[i] = (*l)[i];
c[position] = value;
for (i = position; i < n; i++)
for (size_t i = position; i < n; i++)
c[i+1] = (*l)[i];
c[n+1] = NULL;
free(*l);
*l = c;
return 0;
return free_and_replace(*l, c);
}
int strv_consume(char ***l, char *value) {
@ -602,7 +594,7 @@ int strv_extend_front(char ***l, const char *value) {
return 0;
}
char **strv_uniq(char **l) {
char** strv_uniq(char **l) {
char **i;
/* Drops duplicate entries. The first identical string will be
@ -624,7 +616,7 @@ bool strv_is_uniq(char * const *l) {
return true;
}
char **strv_remove(char **l, const char *s) {
char** strv_remove(char **l, const char *s) {
char **f, **t;
if (!l)
@ -645,7 +637,7 @@ char **strv_remove(char **l, const char *s) {
return l;
}
char **strv_parse_nulstr(const char *s, size_t l) {
char** strv_parse_nulstr(const char *s, size_t l) {
/* l is the length of the input data, which will be split at NULs into
* elements of the resulting strv. Hence, the number of items in the resulting strv
* will be equal to one plus the number of NUL bytes in the l bytes starting at s,
@ -657,7 +649,6 @@ char **strv_parse_nulstr(const char *s, size_t l) {
* empty strings in s.
*/
const char *p;
size_t c = 0, i = 0;
char **v;
@ -666,7 +657,7 @@ char **strv_parse_nulstr(const char *s, size_t l) {
if (l <= 0)
return new0(char*, 1);
for (p = s; p < s + l; p++)
for (const char *p = s; p < s + l; p++)
if (*p == 0)
c++;
@ -677,8 +668,7 @@ char **strv_parse_nulstr(const char *s, size_t l) {
if (!v)
return NULL;
p = s;
while (p < s + l) {
for (const char *p = s; p < s + l; ) {
const char *e;
e = memchr(p, 0, s + l - p);
@ -702,7 +692,7 @@ char **strv_parse_nulstr(const char *s, size_t l) {
return v;
}
char **strv_split_nulstr(const char *s) {
char** strv_split_nulstr(const char *s) {
const char *i;
char **r = NULL;
@ -777,7 +767,7 @@ static int str_compare(char * const *a, char * const *b) {
return strcmp(*a, *b);
}
char **strv_sort(char **l) {
char** strv_sort(char **l) {
typesafe_qsort(l, strv_length(l), str_compare);
return l;
}
@ -826,20 +816,20 @@ int strv_extendf(char ***l, const char *format, ...) {
return strv_consume(l, x);
}
char **strv_reverse(char **l) {
size_t n, i;
char** strv_reverse(char **l) {
size_t n;
n = strv_length(l);
if (n <= 1)
return l;
for (i = 0; i < n / 2; i++)
for (size_t i = 0; i < n / 2; i++)
SWAP_TWO(l[i], l[n-1-i]);
return l;
}
char **strv_shell_escape(char **l, const char *bad) {
char** strv_shell_escape(char **l, const char *bad) {
char **s;
/* Escapes every character in every string in l that is in bad,
@ -870,19 +860,7 @@ bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *
return false;
}
char ***strv_free_free(char ***l) {
char ***i;
if (!l)
return NULL;
for (i = l; *i; i++)
strv_free(*i);
return mfree(l);
}
char **strv_skip(char **l, size_t n) {
char** strv_skip(char **l, size_t n) {
while (n > 0) {
if (strv_isempty(l))
@ -895,7 +873,7 @@ char **strv_skip(char **l, size_t n) {
}
int strv_extend_n(char ***l, const char *value, size_t n) {
size_t i, j, k;
size_t i, k;
char **nl;
assert(l);
@ -922,15 +900,15 @@ int strv_extend_n(char ***l, const char *value, size_t n) {
if (!nl[i])
goto rollback;
}
nl[i] = NULL;
return 0;
rollback:
for (j = k; j < i; j++)
for (size_t j = k; j < i; j++)
free(nl[j]);
nl[k] = NULL;
return -ENOMEM;
}

View File

@ -13,23 +13,23 @@
#include "macro.h"
#include "string-util.h"
char *strv_find(char * const *l, const char *name) _pure_;
char *strv_find_case(char * const *l, const char *name) _pure_;
char *strv_find_prefix(char * const *l, const char *name) _pure_;
char *strv_find_startswith(char * const *l, const char *name) _pure_;
char* strv_find(char * const *l, const char *name) _pure_;
char* strv_find_case(char * const *l, const char *name) _pure_;
char* strv_find_prefix(char * const *l, const char *name) _pure_;
char* strv_find_startswith(char * const *l, const char *name) _pure_;
#define strv_contains(l, s) (!!strv_find((l), (s)))
#define strv_contains_case(l, s) (!!strv_find_case((l), (s)))
char **strv_free(char **l);
char** strv_free(char **l);
DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
#define _cleanup_strv_free_ _cleanup_(strv_freep)
char **strv_free_erase(char **l);
char** strv_free_erase(char **l);
DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free_erase);
#define _cleanup_strv_free_erase_ _cleanup_(strv_free_erasep)
char **strv_copy(char * const *l);
char** strv_copy(char * const *l);
size_t strv_length(char * const *l) _pure_;
int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates);
@ -50,8 +50,8 @@ int strv_consume(char ***l, char *value);
int strv_consume_pair(char ***l, char *a, char *b);
int strv_consume_prepend(char ***l, char *value);
char **strv_remove(char **l, const char *s);
char **strv_uniq(char **l);
char** strv_remove(char **l, const char *s);
char** strv_uniq(char **l);
bool strv_is_uniq(char * const *l);
int strv_compare(char * const *a, char * const *b);
@ -59,8 +59,8 @@ static inline bool strv_equal(char * const *a, char * const *b) {
return strv_compare(a, b) == 0;
}
char **strv_new_internal(const char *x, ...) _sentinel_;
char **strv_new_ap(const char *x, va_list ap);
char** strv_new_internal(const char *x, ...) _sentinel_;
char** strv_new_ap(const char *x, va_list ap);
#define strv_new(...) strv_new_internal(__VA_ARGS__, NULL)
#define STRV_IGNORE ((const char *) POINTER_MAX)
@ -74,7 +74,7 @@ static inline bool strv_isempty(char * const *l) {
}
int strv_split_full(char ***t, const char *s, const char *separators, ExtractFlags flags);
static inline char **strv_split(const char *s, const char *separators) {
static inline char** strv_split(const char *s, const char *separators) {
char **ret;
if (strv_split_full(&ret, s, separators, 0) < 0)
@ -87,7 +87,7 @@ int strv_split_and_extend_full(char ***t, const char *s, const char *separators,
#define strv_split_and_extend(t, s, sep, dup) strv_split_and_extend_full(t, s, sep, dup, 0)
int strv_split_newlines_full(char ***ret, const char *s, ExtractFlags flags);
static inline char **strv_split_newlines(const char *s) {
static inline char** strv_split_newlines(const char *s) {
char **ret;
if (strv_split_newlines_full(&ret, s, 0) < 0)
@ -101,13 +101,13 @@ static inline char **strv_split_newlines(const char *s) {
* string in the vector is an empty string. */
int strv_split_colon_pairs(char ***t, const char *s);
char *strv_join_full(char * const *l, const char *separator, const char *prefix, bool escape_separtor);
char* strv_join_full(char * const *l, const char *separator, const char *prefix, bool escape_separtor);
static inline char *strv_join(char * const *l, const char *separator) {
return strv_join_full(l, separator, NULL, false);
}
char **strv_parse_nulstr(const char *s, size_t l);
char **strv_split_nulstr(const char *s);
char** strv_parse_nulstr(const char *s, size_t l);
char** strv_split_nulstr(const char *s);
int strv_make_nulstr(char * const *l, char **p, size_t *n);
static inline int strv_from_nulstr(char ***a, const char *nulstr) {
@ -136,7 +136,7 @@ bool strv_overlap(char * const *a, char * const *b) _pure_;
#define STRV_FOREACH_PAIR(x, y, l) \
for ((x) = (l), (y) = (x) ? (x+1) : NULL; (x) && *(x) && *(y); (x) += 2, (y) = (x + 1))
char **strv_sort(char **l);
char** strv_sort(char **l);
void strv_print(char * const *l);
#define strv_from_stdarg_alloca(first) \
@ -214,8 +214,8 @@ void strv_print(char * const *l);
#define FOREACH_STRING(x, y, ...) \
_FOREACH_STRING(UNIQ, x, y, ##__VA_ARGS__)
char **strv_reverse(char **l);
char **strv_shell_escape(char **l, const char *bad);
char** strv_reverse(char **l);
char** strv_shell_escape(char **l, const char *bad);
bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *matched_pos);
static inline bool strv_fnmatch(char* const* patterns, const char *s) {
@ -228,10 +228,7 @@ static inline bool strv_fnmatch_or_empty(char* const* patterns, const char *s, i
strv_fnmatch_full(patterns, s, flags, NULL);
}
char ***strv_free_free(char ***l);
DEFINE_TRIVIAL_CLEANUP_FUNC(char***, strv_free_free);
char **strv_skip(char **l, size_t n);
char** strv_skip(char **l, size_t n);
int strv_extend_n(char ***l, const char *value, size_t n);

View File

@ -543,9 +543,9 @@ static bool link_is_ready_to_call_set_link(Request *req) {
break;
case SET_LINK_MAC:
if (req->netlink_handler == link_set_mac_handler) {
/* This is the second trial to set hardware address. On the first attempt
/* This is the second attempt to set hardware address. On the first attempt
* req->netlink_handler points to link_set_mac_allow_retry_handler().
* The first trial failed as the interface was up. */
* The first attempt failed as the interface was up. */
r = link_down(link);
if (r < 0) {
link_enter_failed(link);

View File

@ -148,14 +148,14 @@ int net_verify_hardware_address(
if (ether_addr_is_multicast(&new_hw_addr->ether)) {
if (warn_invalid)
log_link_warning(&link, "Specified MAC address has multicast bit set, clearing the bit.");
log_link_warning(&link, "Specified MAC address has the multicast bit set, clearing the bit.");
new_hw_addr->bytes[0] &= 0xfe;
}
if (!ether_addr_is_local(&new_hw_addr->ether)) {
if (warn_invalid)
log_link_warning(&link, "Specified MAC address has not local assignment bit set, setting the bit.");
log_link_warning(&link, "Specified MAC address does not have the local assignment bit set, setting the bit.");
new_hw_addr->bytes[0] |= 0x02;
}

View File

@ -917,17 +917,6 @@ TEST(strv_make_nulstr) {
test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux"));
}
TEST(strv_free_free) {
char ***t;
assert_se(t = new(char**, 3));
assert_se(t[0] = strv_new("a", "b"));
assert_se(t[1] = strv_new("c", "d", "e"));
t[2] = NULL;
t = strv_free_free(t);
}
TEST(foreach_string) {
const char * const t[] = {
"foo",