[systemd-devel] [PATCH 1/3] Add helper for fnmatch over strv

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Fri Feb 13 15:53:24 PST 2015


No functional change intended.
---
 src/analyze/analyze.c                     | 46 ++++-----------------------
 src/libsystemd-network/network-internal.c | 53 +++++--------------------------
 src/shared/strv.c                         | 10 ++++++
 src/shared/strv.h                         |  9 ++++++
 src/systemctl/systemctl.c                 | 51 ++++-------------------------
 5 files changed, 41 insertions(+), 128 deletions(-)

diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
index 46a97eb8e7..672a0d7976 100644
--- a/src/analyze/analyze.c
+++ b/src/analyze/analyze.c
@@ -25,7 +25,6 @@
 #include <getopt.h>
 #include <locale.h>
 #include <sys/utsname.h>
-#include <fnmatch.h>
 
 #include "sd-bus.h"
 #include "bus-util.h"
@@ -985,46 +984,15 @@ static int graph_one_property(sd_bus *bus, const UnitInfo *u, const char* prop,
                 return r;
 
         STRV_FOREACH(unit, units) {
-                char **p;
-                bool match_found;
-
-                if (!strv_isempty(arg_dot_from_patterns)) {
-                        match_found = false;
-
-                        STRV_FOREACH(p, arg_dot_from_patterns)
-                                if (fnmatch(*p, u->id, 0) == 0) {
-                                        match_found = true;
-                                        break;
-                                }
-
-                        if (!match_found)
-                                continue;
-                }
-
-                if (!strv_isempty(arg_dot_to_patterns)) {
-                        match_found = false;
-
-                        STRV_FOREACH(p, arg_dot_to_patterns)
-                                if (fnmatch(*p, *unit, 0) == 0) {
-                                        match_found = true;
-                                        break;
-                                }
-
-                        if (!match_found)
-                                continue;
-                }
+                if (!strv_fnmatch_or_empty(u->id, arg_dot_from_patterns, 0))
+                        continue;
 
-                if (!strv_isempty(patterns)) {
-                        match_found = false;
+                if (!strv_fnmatch_or_empty(*unit, arg_dot_to_patterns, 0))
+                        continue;
 
-                        STRV_FOREACH(p, patterns)
-                                if (fnmatch(*p, u->id, 0) == 0 || fnmatch(*p, *unit, 0) == 0) {
-                                        match_found = true;
-                                        break;
-                                }
-                        if (!match_found)
-                                continue;
-                }
+                if (!strv_fnmatch_or_empty(u->id, patterns, 0) &&
+                    !strv_fnmatch_or_empty(*unit, patterns, 0))
+                        continue;
 
                 printf("\t\"%s\"->\"%s\" [color=\"%s\"];\n", u->id, *unit, color);
         }
diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c
index 41f43d3389..5867aef662 100644
--- a/src/libsystemd-network/network-internal.c
+++ b/src/libsystemd-network/network-internal.c
@@ -22,7 +22,6 @@
 #include <netinet/ether.h>
 #include <linux/if.h>
 #include <arpa/inet.h>
-#include <fnmatch.h>
 
 #include "strv.h"
 #include "siphash24.h"
@@ -97,10 +96,6 @@ bool net_match_config(const struct ether_addr *match_mac,
                       const char *dev_driver,
                       const char *dev_type,
                       const char *dev_name) {
-        char * const *match_path;
-        char * const *match_driver;
-        char * const *match_type;
-        char * const *match_name;
 
         if (match_host && !condition_test(match_host))
                 return false;
@@ -117,49 +112,17 @@ bool net_match_config(const struct ether_addr *match_mac,
         if (match_mac && (!dev_mac || memcmp(match_mac, dev_mac, ETH_ALEN)))
                 return false;
 
-        if (!strv_isempty(match_paths)) {
-                if (!dev_path)
-                        return false;
+        if (!strv_isempty(match_paths))
+                return strv_fnmatch(dev_path, match_paths, 0);
 
-                STRV_FOREACH(match_path, match_paths)
-                        if (fnmatch(*match_path, dev_path, 0) == 0)
-                                return true;
+        if (!strv_isempty(match_drivers))
+                return strv_fnmatch(dev_driver, match_drivers, 0);
 
-                return false;
-        }
-
-        if (!strv_isempty(match_drivers)) {
-                if (!dev_driver)
-                        return false;
-
-                STRV_FOREACH(match_driver, match_drivers)
-                        if (fnmatch(*match_driver, dev_driver, 0) == 0)
-                                return true;
-
-                return false;
-        }
-
-        if (!strv_isempty(match_types)) {
-                if (!dev_type)
-                        return false;
+        if (!strv_isempty(match_types))
+                return strv_fnmatch(dev_type, match_types, 0);
 
-                STRV_FOREACH(match_type, match_types)
-                        if (fnmatch(*match_type, dev_type, 0) == 0)
-                                return true;
-
-                return false;
-        }
-
-        if (!strv_isempty(match_names)) {
-                if (!dev_name)
-                        return false;
-
-                STRV_FOREACH(match_name, match_names)
-                        if (fnmatch(*match_name, dev_name, 0) == 0)
-                                return true;
-
-                return false;
-        }
+        if (!strv_isempty(match_names))
+                return strv_fnmatch(dev_name, match_names, 0);
 
         return true;
 }
diff --git a/src/shared/strv.c b/src/shared/strv.c
index e418312d52..2268bf61db 100644
--- a/src/shared/strv.c
+++ b/src/shared/strv.c
@@ -692,3 +692,13 @@ char **strv_reverse(char **l) {
 
         return l;
 }
+
+bool strv_fnmatch(const char *s, char* const* patterns, int flags) {
+        char* const* p;
+
+        STRV_FOREACH(p, patterns)
+                if (fnmatch(*p, s, 0) == 0)
+                        return true;
+
+        return false;
+}
diff --git a/src/shared/strv.h b/src/shared/strv.h
index e385bf73b8..a899395ba4 100644
--- a/src/shared/strv.h
+++ b/src/shared/strv.h
@@ -23,6 +23,7 @@
 
 #include <stdarg.h>
 #include <stdbool.h>
+#include <fnmatch.h>
 
 #include "util.h"
 
@@ -144,3 +145,11 @@ void strv_print(char **l);
         }))
 
 char **strv_reverse(char **l);
+
+bool strv_fnmatch(const char *s, char* const* patterns, int flags);
+
+static inline bool strv_fnmatch_or_empty(const char *s, char* const* patterns, int flags) {
+        assert(s);
+        return strv_isempty(patterns) ||
+               strv_fnmatch(s, patterns, flags);
+}
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 2d70ff1de6..975d834df8 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -37,7 +37,6 @@
 #include <sys/stat.h>
 #include <stddef.h>
 #include <sys/prctl.h>
-#include <fnmatch.h>
 
 #include "sd-daemon.h"
 #include "sd-shutdown.h"
@@ -311,16 +310,9 @@ static int compare_unit_info(const void *a, const void *b) {
 }
 
 static bool output_show_unit(const UnitInfo *u, char **patterns) {
-        if (!strv_isempty(patterns)) {
-                char **pattern;
-
-                STRV_FOREACH(pattern, patterns)
-                        if (fnmatch(*pattern, u->id, FNM_NOESCAPE) == 0)
-                                goto next;
+        if (!strv_fnmatch_or_empty(u->id, patterns, FNM_NOESCAPE))
                 return false;
-        }
 
-next:
         if (arg_types) {
                 const char *dot;
 
@@ -1255,16 +1247,9 @@ static int compare_unit_file_list(const void *a, const void *b) {
 }
 
 static bool output_show_unit_file(const UnitFileList *u, char **patterns) {
-        if (!strv_isempty(patterns)) {
-                char **pattern;
-
-                STRV_FOREACH(pattern, patterns)
-                        if (fnmatch(*pattern, basename(u->path), FNM_NOESCAPE) == 0)
-                                goto next;
+        if (!strv_fnmatch_or_empty(basename(u->path), patterns, FNM_NOESCAPE))
                 return false;
-        }
 
-next:
         if (!strv_isempty(arg_types)) {
                 const char *dot;
 
@@ -1276,10 +1261,9 @@ next:
                         return false;
         }
 
-        if (!strv_isempty(arg_states)) {
-                if (!strv_find(arg_states, unit_file_state_to_string(u->state)))
-                        return false;
-        }
+        if (!strv_isempty(arg_states) &&
+            !strv_find(arg_states, unit_file_state_to_string(u->state)))
+                return false;
 
         return true;
 }
@@ -1736,18 +1720,7 @@ static int get_machine_properties(sd_bus *bus, struct machine_info *mi) {
 }
 
 static bool output_show_machine(const char *name, char **patterns) {
-        char **i;
-
-        assert(name);
-
-        if (strv_isempty(patterns))
-                return true;
-
-        STRV_FOREACH(i, patterns)
-                if (fnmatch(*i, name, FNM_NOESCAPE) == 0)
-                        return true;
-
-        return false;
+        return strv_fnmatch_or_empty(name, patterns, FNM_NOESCAPE);
 }
 
 static int get_machine_list(
@@ -2100,17 +2073,7 @@ static void output_jobs_list(const struct job_info* jobs, unsigned n, bool skipp
 }
 
 static bool output_show_job(struct job_info *job, char **patterns) {
-        char **pattern;
-
-        assert(job);
-
-        if (strv_isempty(patterns))
-                return true;
-
-        STRV_FOREACH(pattern, patterns)
-                if (fnmatch(*pattern, job->name, FNM_NOESCAPE) == 0)
-                        return true;
-        return false;
+        return strv_fnmatch_or_empty(job->name, patterns, FNM_NOESCAPE);
 }
 
 static int list_jobs(sd_bus *bus, char **args) {
-- 
2.1.0



More information about the systemd-devel mailing list