Skip to content

Commit 71ca0dd

Browse files
committed
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 <emil.l.velikov@gmail.com>
1 parent 990db69 commit 71ca0dd

6 files changed

Lines changed: 16 additions & 40 deletions

File tree

testsuite/testsuite.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ static void test_list(const struct test *start, const struct test *stop)
8080
int test_init(const struct test *start, const struct test *stop, int argc,
8181
char *const argv[])
8282
{
83+
int c;
84+
8385
progname = argv[0];
8486

8587
/* An empty testsuite is not likely intended */
@@ -88,11 +90,7 @@ int test_init(const struct test *start, const struct test *stop, int argc,
8890
return -EINVAL;
8991
}
9092

91-
for (;;) {
92-
int c, idx = 0;
93-
c = getopt_long(argc, argv, options_short, options, &idx);
94-
if (c == -1)
95-
break;
93+
while ((c = getopt_long(argc, argv, options_short, options, NULL)) != -1) {
9694
switch (c) {
9795
case 'l':
9896
test_list(start, stop);

tools/depmod.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,7 +2879,7 @@ static int is_version_number(const char *version)
28792879
static int do_depmod(int argc, char *argv[])
28802880
{
28812881
FILE *out = NULL;
2882-
int err = 0, all = 0, maybe_all = 0, n_config_paths = 0;
2882+
int c, err = 0, all = 0, maybe_all = 0, n_config_paths = 0;
28832883
_cleanup_free_ char *root_arg = NULL;
28842884
_cleanup_free_ char *out_root = NULL;
28852885
_cleanup_free_ const char **config_paths = NULL;
@@ -2896,11 +2896,7 @@ static int do_depmod(int argc, char *argv[])
28962896
memset(&cfg, 0, sizeof(cfg));
28972897
memset(&depmod, 0, sizeof(depmod));
28982898

2899-
for (;;) {
2900-
int c, idx = 0;
2901-
c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
2902-
if (c == -1)
2903-
break;
2899+
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
29042900
switch (c) {
29052901
case 'a':
29062902
all = 1;

tools/kmod.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdio.h>
88
#include <stdlib.h>
99
#include <string.h>
10+
#include <unistd.h>
1011

1112
#include <shared/util.h>
1213
#include <shared/missing.h>
@@ -81,16 +82,10 @@ static const struct kmod_cmd kmod_cmd_help = {
8182
static int handle_kmod_commands(int argc, char *argv[])
8283
{
8384
const char *cmd;
84-
int err = 0;
85+
int err = 0, c;
8586
size_t i;
8687

87-
for (;;) {
88-
int c;
89-
90-
c = getopt_long(argc, argv, options_s, options, NULL);
91-
if (c == -1)
92-
break;
93-
88+
while ((c = getopt_long(argc, argv, options_s, options, NULL)) != -1) {
9489
switch (c) {
9590
case 'h':
9691
kmod_help(argc, argv);

tools/modinfo.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <stdio.h>
1313
#include <stdlib.h>
1414
#include <string.h>
15+
#include <unistd.h>
1516
#include <sys/stat.h>
1617
#include <sys/utsname.h>
1718

@@ -364,13 +365,9 @@ static int do_modinfo(int argc, char *argv[])
364365
const char *root = NULL;
365366
const char *null_config = NULL;
366367
bool arg_is_modname = false;
367-
int i, err;
368+
int i, err, c;
368369

369-
for (;;) {
370-
int c, idx = 0;
371-
c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
372-
if (c == -1)
373-
break;
370+
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
374371
switch (c) {
375372
case 'a':
376373
field = "author";

tools/modprobe.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ static int do_modprobe(int argc, char **orig_argv)
803803
int do_show_config = 0;
804804
int do_show_modversions = 0;
805805
int do_show_exports = 0;
806-
int err;
806+
int err, c;
807807
struct stat stat_buf;
808808
bool use_syslog = false;
809809

@@ -813,11 +813,7 @@ static int do_modprobe(int argc, char **orig_argv)
813813
return EXIT_FAILURE;
814814
}
815815

816-
for (;;) {
817-
int c, idx = 0;
818-
c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
819-
if (c == -1)
820-
break;
816+
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
821817
switch (c) {
822818
case 'a':
823819
log_priority = LOG_WARNING;

tools/static-nodes.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,17 @@ static int do_static_nodes(int argc, char *argv[])
149149
FILE *in = NULL, *out = NULL;
150150
const struct static_nodes_format *format = &static_nodes_format_human;
151151
int r, ret = EXIT_SUCCESS;
152+
int c, valid;
152153

153-
for (;;) {
154-
int c, idx = 0, valid;
155-
size_t i;
156-
157-
c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
158-
if (c == -1) {
159-
break;
160-
}
154+
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
161155
switch (c) {
162156
case 'o':
163157
output = optarg;
164158
break;
165159
case 'f':
166160
valid = 0;
167161

168-
for (i = 0; i < ARRAY_SIZE(static_nodes_formats); i++) {
162+
for (size_t i = 0; i < ARRAY_SIZE(static_nodes_formats); i++) {
169163
if (streq(static_nodes_formats[i]->name, optarg)) {
170164
format = static_nodes_formats[i];
171165
valid = 1;

0 commit comments

Comments
 (0)