]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Consistently handle getopt
authorEmil Velikov <emil.l.velikov@gmail.com>
Sat, 19 Oct 2024 17:11:17 +0000 (18:11 +0100)
committerLucas De Marchi <ldemarchi@kernel.org>
Wed, 5 Aug 2026 15:49:50 +0000 (10:49 -0500)
Pull the unistd.h for optind and adjust the loop to follow the
in-documentation example.

In addition, we don't need the unused idx.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/453
Signed-off-by: Lucas De Marchi <ldemarchi@kernel.org>
testsuite/testsuite.c
tools/depmod.c
tools/kmod.c
tools/modinfo.c
tools/modprobe.c
tools/static-nodes.c

index 87fcefd50c30c1f4674f094c66dad824d72d4381..8837ecbe3b7c7e1879a7219fbd1947b38026622e 100644 (file)
@@ -80,6 +80,8 @@ static void test_list(const struct test *start, const struct test *stop)
 int test_init(const struct test *start, const struct test *stop, int argc,
              char *const argv[])
 {
+       int c;
+
        progname = argv[0];
 
        /* An empty testsuite is not likely intended */
@@ -88,11 +90,7 @@ int test_init(const struct test *start, const struct test *stop, int argc,
                return -EINVAL;
        }
 
-       for (;;) {
-               int c, idx = 0;
-               c = getopt_long(argc, argv, options_short, options, &idx);
-               if (c == -1)
-                       break;
+       while ((c = getopt_long(argc, argv, options_short, options, NULL)) != -1) {
                switch (c) {
                case 'l':
                        test_list(start, stop);
index 66fa37aa111cc1892be426b02022076e1869a682..1349bef677f182bb4e9a9ee16a74e3568533034c 100644 (file)
@@ -2879,7 +2879,7 @@ static int is_version_number(const char *version)
 static int do_depmod(int argc, char *argv[])
 {
        FILE *out = NULL;
-       int err = 0, all = 0, maybe_all = 0, n_config_paths = 0;
+       int c, err = 0, all = 0, maybe_all = 0, n_config_paths = 0;
        _cleanup_free_ char *root_arg = NULL;
        _cleanup_free_ char *out_root = NULL;
        _cleanup_free_ const char **config_paths = NULL;
@@ -2896,11 +2896,7 @@ static int do_depmod(int argc, char *argv[])
        memset(&cfg, 0, sizeof(cfg));
        memset(&depmod, 0, sizeof(depmod));
 
-       for (;;) {
-               int c, idx = 0;
-               c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
-               if (c == -1)
-                       break;
+       while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 'a':
                        all = 1;
index 9624ea3c2d86a62093f8904094b2d06d1869b20f..eda91e62ea328bb2a5d385404f6922077ca14f74 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <shared/util.h>
 #include <shared/missing.h>
@@ -81,16 +82,10 @@ static const struct kmod_cmd kmod_cmd_help = {
 static int handle_kmod_commands(int argc, char *argv[])
 {
        const char *cmd;
-       int err = 0;
+       int err = 0, c;
        size_t i;
 
-       for (;;) {
-               int c;
-
-               c = getopt_long(argc, argv, options_s, options, NULL);
-               if (c == -1)
-                       break;
-
+       while ((c = getopt_long(argc, argv, options_s, options, NULL)) != -1) {
                switch (c) {
                case 'h':
                        kmod_help(argc, argv);
index 334e1a7fe122c7a2b4496a2825517d42710e5be4..f5cb30745e73d2ed343765ef1ccd67fcdb86e849 100644 (file)
@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/stat.h>
 #include <sys/utsname.h>
 
@@ -364,13 +365,9 @@ static int do_modinfo(int argc, char *argv[])
        const char *root = NULL;
        const char *null_config = NULL;
        bool arg_is_modname = false;
-       int i, err;
+       int i, err, c;
 
-       for (;;) {
-               int c, idx = 0;
-               c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
-               if (c == -1)
-                       break;
+       while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 'a':
                        field = "author";
index 1f655c22f58cb0eddf058dcf3ba627a8278bb09b..6eb06cd4dea6c75dd8eeb692e710decf97272abc 100644 (file)
@@ -803,7 +803,7 @@ static int do_modprobe(int argc, char **orig_argv)
        int do_show_config = 0;
        int do_show_modversions = 0;
        int do_show_exports = 0;
-       int err;
+       int err, c;
        struct stat stat_buf;
        bool use_syslog = false;
 
@@ -813,11 +813,7 @@ static int do_modprobe(int argc, char **orig_argv)
                return EXIT_FAILURE;
        }
 
-       for (;;) {
-               int c, idx = 0;
-               c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
-               if (c == -1)
-                       break;
+       while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 'a':
                        log_priority = LOG_WARNING;
index 18c3d04e0e921fddff436a920f784fe1a64070d4..e64cd1b0a7de37ef7bfce7ef3550a138c6566738 100644 (file)
@@ -149,15 +149,9 @@ static int do_static_nodes(int argc, char *argv[])
        FILE *in = NULL, *out = NULL;
        const struct static_nodes_format *format = &static_nodes_format_human;
        int r, ret = EXIT_SUCCESS;
+       int c, valid;
 
-       for (;;) {
-               int c, idx = 0, valid;
-               size_t i;
-
-               c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
-               if (c == -1) {
-                       break;
-               }
+       while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 'o':
                        output = optarg;
@@ -165,7 +159,7 @@ static int do_static_nodes(int argc, char *argv[])
                case 'f':
                        valid = 0;
 
-                       for (i = 0; i < ARRAY_SIZE(static_nodes_formats); i++) {
+                       for (size_t i = 0; i < ARRAY_SIZE(static_nodes_formats); i++) {
                                if (streq(static_nodes_formats[i]->name, optarg)) {
                                        format = static_nodes_formats[i];
                                        valid = 1;