]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: modernize strv_fnmatch() a bit
authorLennart Poettering <lennart@poettering.net>
Mon, 29 Aug 2022 09:06:39 +0000 (11:06 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 29 Aug 2022 16:09:12 +0000 (01:09 +0900)
src/basic/strv.c
src/basic/strv.h

index b2fe8d9d4eac991d5c1d8682d28f9bf437f0485d..eea34ca68d9a98f6317b190b5927f3536c40e7e6 100644 (file)
@@ -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;
 }
index 87ec6337bde12083f63f439aa7f16345ebe561cb..d6f5ac6ba581958471b9499c356020b59ba061b1 100644 (file)
@@ -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);
 }