chiark / gitweb /
systemctl: let's make use of FOREACH_STRING() where we can
[elogind.git] / src / systemctl / systemctl.c
index d1b7f8ad7bb04e6d9d66caa1159e9107211343f3..773b4ccd87f19e139023adf2a00bbfe2086c7bf8 100644 (file)
@@ -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(patterns, u->id, 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(patterns, basename(u->path), 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(patterns, name, 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(patterns, job->name, FNM_NOESCAPE);
 }
 
 static int list_jobs(sd_bus *bus, char **args) {
@@ -5808,7 +5771,6 @@ static int get_file_to_edit(const char *name, const char *user_home, const char
         return 0;
 }
 
-
 static int unit_file_create_dropin(const char *unit_name, const char *user_home, const char *user_runtime, char **ret_new_path, char **ret_tmp_path) {
         char *tmp_new_path, *ending;
         char *tmp_tmp_path;
@@ -5835,12 +5797,14 @@ static int unit_file_create_dropin(const char *unit_name, const char *user_home,
         return 0;
 }
 
-static int unit_file_create_copy(const char *unit_name,
-                                 const char *fragment_path,
-                                 const char *user_home,
-                                 const char *user_runtime,
-                                 char **ret_new_path,
-                                 char **ret_tmp_path) {
+static int unit_file_create_copy(
+                const char *unit_name,
+                const char *fragment_path,
+                const char *user_home,
+                const char *user_runtime,
+                char **ret_new_path,
+                char **ret_tmp_path) {
+
         char *tmp_new_path;
         char *tmp_tmp_path;
         int r;
@@ -5896,9 +5860,8 @@ static int run_editor(char **paths) {
 
         if (pid == 0) {
                 const char **args;
-                char **backup_editors = STRV_MAKE("nano", "vim", "vi");
                 char *editor;
-                char **tmp_path, **original_path, **p;
+                char **tmp_path, **original_path, *p;
                 unsigned i = 1;
                 size_t argc;
 
@@ -5927,9 +5890,9 @@ static int run_editor(char **paths) {
                         execvp(editor, (char* const*) args);
                 }
 
-                STRV_FOREACH(p, backup_editors) {
-                        args[0] = *p;
-                        execvp(*p, (char* const*) args);
+                FOREACH_STRING(p, "nano", "vim", "vi") {
+                        args[0] = p;
+                        execvp(p, (char* const*) args);
                         /* We do not fail if the editor doesn't exist
                          * because we want to try each one of them before
                          * failing.
@@ -5940,7 +5903,7 @@ static int run_editor(char **paths) {
                         }
                 }
 
-                log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR or $EDITOR or $VISUAL.");
+                log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL.");
                 _exit(EXIT_FAILURE);
         }