From: Emil Velikov Date: Sat, 19 Oct 2024 17:11:17 +0000 (+0100) Subject: Consistently handle getopt X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=af79c1fc8ae22d28dff0fb164a128b7396ae8fcf;p=thirdparty%2Fkmod.git Consistently handle getopt 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 Link: https://github.com/kmod-project/kmod/pull/453 Signed-off-by: Lucas De Marchi --- diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index 87fcefd5..8837ecbe 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -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); diff --git a/tools/depmod.c b/tools/depmod.c index 66fa37aa..1349bef6 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -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; diff --git a/tools/kmod.c b/tools/kmod.c index 9624ea3c..eda91e62 100644 --- a/tools/kmod.c +++ b/tools/kmod.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -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); diff --git a/tools/modinfo.c b/tools/modinfo.c index 334e1a7f..f5cb3074 100644 --- a/tools/modinfo.c +++ b/tools/modinfo.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -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"; diff --git a/tools/modprobe.c b/tools/modprobe.c index 1f655c22..6eb06cd4 100644 --- a/tools/modprobe.c +++ b/tools/modprobe.c @@ -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; diff --git a/tools/static-nodes.c b/tools/static-nodes.c index 18c3d04e..e64cd1b0 100644 --- a/tools/static-nodes.c +++ b/tools/static-nodes.c @@ -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;