]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: use STRV_FOREACH in consistent fashion
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 19 Nov 2024 14:48:31 +0000 (15:48 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 27 Mar 2025 11:04:27 +0000 (12:04 +0100)
Also put positive condition first.

src/analyze/analyze-architectures.c
src/analyze/analyze-cat-config.c
src/analyze/analyze-exit-status.c
src/analyze/analyze-filesystems.c
src/analyze/analyze-malloc.c
src/analyze/analyze-pcrs.c
src/analyze/analyze-syscall-filter.c
src/analyze/analyze-timestamp.c

index 08c62e628594d3efa08941856b5c5f2da3a6dc08..832d50c5b7163cfb81936d9e902e5cbc2103d979 100644 (file)
@@ -47,30 +47,25 @@ int verb_architectures(int argc, char *argv[], void *userdata) {
 
         (void) table_hide_column_from_display(table, (size_t) 0);
 
-        if (strv_isempty(strv_skip(argv, 1)))
-                for (Architecture a = 0; a < _ARCHITECTURE_MAX; a++) {
-                        r = add_arch(table, a);
-                        if (r < 0)
-                                return r;
-                }
-        else {
-                STRV_FOREACH(as, strv_skip(argv, 1)) {
+        char **args = strv_skip(argv, 1);
+        if (args) {
+                STRV_FOREACH(arg, args) {
                         Architecture a;
 
-                        if (streq(*as, "native"))
+                        if (streq(*arg, "native"))
                                 a = native_architecture();
-                        else if (streq(*as, "uname"))
+                        else if (streq(*arg, "uname"))
                                 a = uname_architecture();
-                        else if (streq(*as, "secondary")) {
+                        else if (streq(*arg, "secondary")) {
 #ifdef ARCHITECTURE_SECONDARY
                                 a = ARCHITECTURE_SECONDARY;
 #else
                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No secondary architecture.");
 #endif
                         } else
-                                a = architecture_from_string(*as);
+                                a = architecture_from_string(*arg);
                         if (a < 0)
-                                return log_error_errno(a, "Architecture \"%s\" not known.", *as);
+                                return log_error_errno(a, "Architecture \"%s\" not known.", *arg);
 
                         r = add_arch(table, a);
                         if (r < 0)
@@ -78,7 +73,12 @@ int verb_architectures(int argc, char *argv[], void *userdata) {
                 }
 
                 (void) table_set_sort(table, (size_t) 0);
-        }
+        } else
+                for (Architecture a = 0; a < _ARCHITECTURE_MAX; a++) {
+                        r = add_arch(table, a);
+                        if (r < 0)
+                                return r;
+                }
 
         r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
         if (r < 0)
index b480d4a0097085f51066e9e15087c5ef5efec624..8b86ef9328e1498a4283f01c327ec0568339d5a1 100644 (file)
@@ -9,12 +9,11 @@
 #include "strv.h"
 
 int verb_cat_config(int argc, char *argv[], void *userdata) {
-        char **list;
         int r;
 
         pager_open(arg_pager_flags);
 
-        list = strv_skip(argv, 1);
+        char **list = strv_skip(argv, 1);
         STRV_FOREACH(arg, list) {
                 const char *t = NULL;
 
index 1032f1a4b7af4e7cd3a85113e0cc6f52879fd5eb..eb60aa0051de6c19ad03d5d168219d97442940b3 100644 (file)
@@ -17,25 +17,14 @@ int verb_exit_status(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return log_error_errno(r, "Failed to right-align status: %m");
 
-        if (strv_isempty(strv_skip(argv, 1)))
-                for (size_t i = 0; i < ELEMENTSOF(exit_status_mappings); i++) {
-                        if (!exit_status_mappings[i].name)
-                                continue;
-
-                        r = table_add_many(table,
-                                           TABLE_STRING, exit_status_mappings[i].name,
-                                           TABLE_INT, (int) i,
-                                           TABLE_STRING, exit_status_class(i));
-                        if (r < 0)
-                                return table_log_add_error(r);
-                }
-        else
-                for (int i = 1; i < argc; i++) {
+        char **args = strv_skip(argv, 1);
+        if (args)
+                STRV_FOREACH(arg, args) {
                         int status;
 
-                        status = exit_status_from_string(argv[i]);
+                        status = exit_status_from_string(*arg);
                         if (status < 0)
-                                return log_error_errno(status, "Invalid exit status \"%s\".", argv[i]);
+                                return log_error_errno(status, "Invalid exit status \"%s\".", *arg);
 
                         assert(status >= 0 && (size_t) status < ELEMENTSOF(exit_status_mappings));
                         r = table_add_many(table,
@@ -45,6 +34,18 @@ int verb_exit_status(int argc, char *argv[], void *userdata) {
                         if (r < 0)
                                 return table_log_add_error(r);
                 }
+        else
+                for (size_t i = 0; i < ELEMENTSOF(exit_status_mappings); i++) {
+                        if (!exit_status_mappings[i].name)
+                                continue;
+
+                        r = table_add_many(table,
+                                           TABLE_STRING, exit_status_mappings[i].name,
+                                           TABLE_INT, (int) i,
+                                           TABLE_STRING, exit_status_class(i));
+                        if (r < 0)
+                                return table_log_add_error(r);
+                }
 
         r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
         if (r < 0)
index b09cd9fa0ee87ee4fa547b3b6a9a5f8fdfd07715..8ec542ec7a2571072f2aaba880642e6686f11039 100644 (file)
@@ -106,15 +106,30 @@ static void dump_filesystem_set(const FilesystemSet *set) {
 }
 
 int verb_filesystems(int argc, char *argv[], void *userdata) {
-        bool first = true;
-
 #if ! HAVE_LIBBPF
         return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Not compiled with libbpf support, sorry.");
 #endif
 
         pager_open(arg_pager_flags);
 
-        if (strv_isempty(strv_skip(argv, 1))) {
+        char **args = strv_skip(argv, 1);
+        if (args)
+                STRV_FOREACH(name, args) {
+                        if (name != args)
+                                puts("");
+
+                        const FilesystemSet *set = filesystem_set_find(*name);
+                        if (!set) {
+                                /* make sure the error appears below normal output */
+                                fflush(stdout);
+
+                                return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
+                                                       "Filesystem set \"%s\" not found.", *name);
+                        }
+
+                        dump_filesystem_set(set);
+                }
+        else {
                 _cleanup_set_free_ Set *kernel = NULL, *known = NULL;
                 int k;
 
@@ -126,27 +141,24 @@ int verb_filesystems(int argc, char *argv[], void *userdata) {
 
                 for (FilesystemGroups i = 0; i < _FILESYSTEM_SET_MAX; i++) {
                         const FilesystemSet *set = filesystem_sets + i;
-                        if (!first)
+                        if (i > 0)
                                 puts("");
 
                         dump_filesystem_set(set);
                         filesystem_set_remove(kernel, set);
                         if (i != FILESYSTEM_SET_KNOWN)
                                 filesystem_set_remove(known, set);
-                        first = false;
                 }
 
                 if (arg_quiet)  /* Let's not show the extra stuff in quiet mode */
                         return 0;
 
                 if (!set_isempty(known)) {
-                        _cleanup_free_ char **l = NULL;
-
                         printf("\n"
                                "# %sUngrouped filesystems%s (known but not included in any of the groups except @known):\n",
                                ansi_highlight(), ansi_normal());
 
-                        l = set_get_strv(known);
+                        _cleanup_free_ char **l = set_get_strv(known);
                         if (!l)
                                 return log_oom();
 
@@ -197,25 +209,7 @@ int verb_filesystems(int argc, char *argv[], void *userdata) {
                         STRV_FOREACH(filesystem, l)
                                 printf("#   %s\n", *filesystem);
                 }
-        } else
-                STRV_FOREACH(name, strv_skip(argv, 1)) {
-                        const FilesystemSet *set;
-
-                        if (!first)
-                                puts("");
-
-                        set = filesystem_set_find(*name);
-                        if (!set) {
-                                /* make sure the error appears below normal output */
-                                fflush(stdout);
-
-                                return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
-                                                       "Filesystem set \"%s\" not found.", *name);
-                        }
-
-                        dump_filesystem_set(set);
-                        first = false;
-                }
+        }
 
         return EXIT_SUCCESS;
 }
index 9eb234e6690edfdcfa1bd4bc7c1a62504a69eaf1..0438d03ae8e83dac80f22c3610a1e57955e5c36f 100644 (file)
@@ -35,11 +35,12 @@ int verb_malloc(int argc, char *argv[], void *userdata) {
         char **services = STRV_MAKE("org.freedesktop.systemd1");
         int r;
 
-        if (!strv_isempty(strv_skip(argv, 1))) {
-                services = strv_skip(argv, 1);
-                STRV_FOREACH(service, services)
+        char **args = strv_skip(argv, 1);
+        if (args) {
+                STRV_FOREACH(service, args)
                         if (!service_name_is_valid(*service))
                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "D-Bus service name '%s' is not valid.", *service);
+                services = args;
         }
 
         r = acquire_bus(&bus, NULL);
index fb67a733ddf5f8497b940240f010e99cd821d7c8..23f8f0773d8cd5f92a5ec5c64194f21f63be1e6d 100644 (file)
@@ -114,19 +114,14 @@ int verb_pcrs(int argc, char *argv[], void *userdata) {
         if (!alg) /* hide hash column if we couldn't acquire it */
                 (void) table_set_display(table, 0, 1);
 
-        if (strv_isempty(strv_skip(argv, 1)))
-                for (uint32_t pi = 0; pi < _TPM2_PCR_INDEX_MAX_DEFINED; pi++) {
-                        r = add_pcr_to_table(table, alg, pi);
-                        if (r < 0)
-                                return r;
-                }
-        else {
-                for (int i = 1; i < argc; i++) {
+        char **args = strv_skip(argv, 1);
+        if (args) {
+                STRV_FOREACH(arg, args) {
                         int pi;
 
-                        pi = tpm2_pcr_index_from_string(argv[i]);
+                        pi = tpm2_pcr_index_from_string(*arg);
                         if (pi < 0)
-                                return log_error_errno(pi, "PCR index \"%s\" not known.", argv[i]);
+                                return log_error_errno(pi, "PCR index \"%s\" not known.", *arg);
 
                         r = add_pcr_to_table(table, alg, pi);
                         if (r < 0)
@@ -134,7 +129,12 @@ int verb_pcrs(int argc, char *argv[], void *userdata) {
                 }
 
                 (void) table_set_sort(table, (size_t) 0);
-        }
+        } else
+                for (uint32_t pi = 0; pi < _TPM2_PCR_INDEX_MAX_DEFINED; pi++) {
+                        r = add_pcr_to_table(table, alg, pi);
+                        if (r < 0)
+                                return r;
+                }
 
         r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */true);
         if (r < 0)
index 66a52da8976d504bf7d50daf353f1921253e24e2..fceeff82753db9c4abaa00c44c8ce5131e2ccacc 100644 (file)
@@ -103,12 +103,30 @@ static void dump_syscall_filter(const SyscallFilterSet *set) {
 }
 
 int verb_syscall_filters(int argc, char *argv[], void *userdata) {
-        bool first = true;
         int r;
 
         pager_open(arg_pager_flags);
 
-        if (strv_isempty(strv_skip(argv, 1))) {
+        char **args = strv_skip(argv, 1);
+        if (args)
+                STRV_FOREACH(name, args) {
+                        const SyscallFilterSet *set;
+
+                        if (name != args)
+                                puts("");
+
+                        set = syscall_filter_set_find(*name);
+                        if (!set) {
+                                /* make sure the error appears below normal output */
+                                fflush(stdout);
+
+                                return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
+                                                       "Filter set \"%s\" not found.", *name);
+                        }
+
+                        dump_syscall_filter(set);
+                }
+        else {
                 _cleanup_set_free_ Set *kernel = NULL, *known = NULL;
                 int k = 0;  /* explicit initialization to appease gcc */
 
@@ -121,27 +139,24 @@ int verb_syscall_filters(int argc, char *argv[], void *userdata) {
 
                 for (int i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) {
                         const SyscallFilterSet *set = syscall_filter_sets + i;
-                        if (!first)
+                        if (i > 0)
                                 puts("");
 
                         dump_syscall_filter(set);
                         syscall_set_remove(kernel, set);
                         if (i != SYSCALL_FILTER_SET_KNOWN)
                                 syscall_set_remove(known, set);
-                        first = false;
                 }
 
                 if (arg_quiet)  /* Let's not show the extra stuff in quiet mode */
                         return 0;
 
                 if (!set_isempty(known)) {
-                        _cleanup_free_ char **l = NULL;
-
                         printf("\n"
                                "# %sUngrouped System Calls%s (known but not included in any of the groups except @known):\n",
                                ansi_highlight(), ansi_normal());
 
-                        l = set_get_strv(known);
+                        _cleanup_free_ char **l = set_get_strv(known);
                         if (!l)
                                 return log_oom();
 
@@ -157,13 +172,11 @@ int verb_syscall_filters(int argc, char *argv[], void *userdata) {
                         if (!arg_quiet)
                                 log_notice_errno(k, "# Not showing unlisted system calls, couldn't retrieve kernel system call list: %m");
                 } else if (!set_isempty(kernel)) {
-                        _cleanup_free_ char **l = NULL;
-
                         printf("\n"
                                "# %sUnlisted System Calls%s (supported by the local kernel, but not included in any of the groups listed above):\n",
                                ansi_highlight(), ansi_normal());
 
-                        l = set_get_strv(kernel);
+                        _cleanup_free_ char **l = set_get_strv(kernel);
                         if (!l)
                                 return log_oom();
 
@@ -172,25 +185,7 @@ int verb_syscall_filters(int argc, char *argv[], void *userdata) {
                         STRV_FOREACH(syscall, l)
                                 printf("#   %s\n", *syscall);
                 }
-        } else
-                STRV_FOREACH(name, strv_skip(argv, 1)) {
-                        const SyscallFilterSet *set;
-
-                        if (!first)
-                                puts("");
-
-                        set = syscall_filter_set_find(*name);
-                        if (!set) {
-                                /* make sure the error appears below normal output */
-                                fflush(stdout);
-
-                                return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
-                                                       "Filter set \"%s\" not found.", *name);
-                        }
-
-                        dump_syscall_filter(set);
-                        first = false;
-                }
+        }
 
         return EXIT_SUCCESS;
 }
index 8c96a8ec55ddec465522c517158369cae9b17bb9..791fa28b1fff55a85a0e3e9c1d777ab90a50b3b9 100644 (file)
@@ -75,11 +75,12 @@ static int test_timestamp_one(const char *p) {
 int verb_timestamp(int argc, char *argv[], void *userdata) {
         int r = 0;
 
-        STRV_FOREACH(p, strv_skip(argv, 1)) {
-                RET_GATHER(r, test_timestamp_one(*p));
+        char **args = strv_skip(argv, 1);
+        STRV_FOREACH(arg, args) {
+                if (arg != args)
+                        puts("");
 
-                if (p[1])
-                        putchar('\n');
+                RET_GATHER(r, test_timestamp_one(*arg));
         }
 
         return r;