From: Lennart Poettering Date: Mon, 29 Aug 2022 09:06:39 +0000 (+0200) Subject: strv: modernize strv_fnmatch() a bit X-Git-Tag: v252-rc1~301 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bcfc0e8872053bb6cd14fa704829fe7aee4adbc6;p=thirdparty%2Fsystemd.git strv: modernize strv_fnmatch() a bit --- diff --git a/src/basic/strv.c b/src/basic/strv.c index b2fe8d9d4ea..eea34ca68d9 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -828,13 +828,26 @@ char** strv_shell_escape(char **l, const char *bad) { return l; } -bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *matched_pos) { - for (size_t i = 0; patterns && patterns[i]; i++) - if (fnmatch(patterns[i], s, flags) == 0) { - if (matched_pos) - *matched_pos = i; - return true; - } +bool strv_fnmatch_full( + char* const* patterns, + const char *s, + int flags, + size_t *ret_matched_pos) { + + assert(s); + + if (patterns) + for (size_t i = 0; patterns[i]; i++) + /* NB: We treat all fnmatch() errors as equivalent to FNM_NOMATCH, i.e. if fnmatch() fails to + * process the pattern for some reason we'll consider this equivalent to non-matching. */ + if (fnmatch(patterns[i], s, flags) == 0) { + if (ret_matched_pos) + *ret_matched_pos = i; + return true; + } + + if (ret_matched_pos) + *ret_matched_pos = SIZE_MAX; return false; } diff --git a/src/basic/strv.h b/src/basic/strv.h index 87ec6337bde..d6f5ac6ba58 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -240,7 +240,7 @@ void strv_print(char * const *l); 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); +bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *ret_matched_pos); static inline bool strv_fnmatch(char* const* patterns, const char *s) { return strv_fnmatch_full(patterns, s, 0, NULL); }