Skip to content

Commit 8eb830d

Browse files
committed
use enum for option indices
1 parent 9ebeff6 commit 8eb830d

File tree

4 files changed

+612
-402
lines changed

4 files changed

+612
-402
lines changed

ip2net/ip2net.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,28 @@ static void exithelp(void)
225225
#define PRINT_VER printf("self-built version %s %s\n\n", __DATE__, __TIME__)
226226
#endif
227227

228+
enum opt_indices {
229+
IDX_HELP,
230+
IDX_H,
231+
IDX_4,
232+
IDX_6,
233+
IDX_PREFIX_LENGTH,
234+
IDX_V4_THRESHOLD,
235+
IDX_V6_THRESHOLD,
236+
IDX_LAST,
237+
};
238+
239+
static const struct option long_options[] = {
240+
[IDX_HELP] = {"help", no_argument, 0, 0},
241+
[IDX_H] = {"h", no_argument, 0, 0},
242+
[IDX_4] = {"4", no_argument, 0, 0},
243+
[IDX_6] = {"6", no_argument, 0, 0},
244+
[IDX_PREFIX_LENGTH] = {"prefix-length", required_argument, 0, 0},
245+
[IDX_V4_THRESHOLD] = {"v4-threshold", required_argument, 0, 0},
246+
[IDX_V6_THRESHOLD] = {"v6-threshold", required_argument, 0, 0},
247+
[IDX_LAST] = {NULL, 0, NULL, 0},
248+
};
249+
228250
static void parse_params(int argc, char *argv[])
229251
{
230252
int option_index = 0;
@@ -236,33 +258,23 @@ static void parse_params(int argc, char *argv[])
236258
params.pctdiv = DEFAULT_PCTDIV;
237259
params.v6_threshold = DEFAULT_V6_THRESHOLD;
238260

239-
const struct option long_options[] = {
240-
{ "help",no_argument,0,0 },// optidx=0
241-
{ "h",no_argument,0,0 },// optidx=1
242-
{ "4",no_argument,0,0 },// optidx=2
243-
{ "6",no_argument,0,0 },// optidx=3
244-
{ "prefix-length",required_argument,0,0 },// optidx=4
245-
{ "v4-threshold",required_argument,0,0 },// optidx=5
246-
{ "v6-threshold",required_argument,0,0 },// optidx=6
247-
{ NULL,0,NULL,0 }
248-
};
249261
while ((v = getopt_long_only(argc, argv, "", long_options, &option_index)) != -1)
250262
{
251263
if (v) exithelp();
252264
switch (option_index)
253265
{
254-
case 0:
255-
case 1:
266+
case IDX_HELP:
267+
case IDX_H:
256268
PRINT_VER;
257269
exithelp();
258270
break;
259-
case 2:
271+
case IDX_4:
260272
params.ipv6 = false;
261273
break;
262-
case 3:
274+
case IDX_6:
263275
params.ipv6 = true;
264276
break;
265-
case 4:
277+
case IDX_PREFIX_LENGTH:
266278
i = sscanf(optarg,"%u-%u",&plen1,&plen2);
267279
if (i == 1) plen2 = plen1;
268280
if (i<=0 || plen2<plen1 || !plen1 || !plen2)
@@ -271,15 +283,15 @@ static void parse_params(int argc, char *argv[])
271283
exit(1);
272284
}
273285
break;
274-
case 5:
286+
case IDX_V4_THRESHOLD:
275287
i = sscanf(optarg, "%u/%u", &params.pctmult, &params.pctdiv);
276288
if (i!=2 || params.pctdiv<2 || params.pctmult<1 || params.pctmult>=params.pctdiv)
277289
{
278290
fprintf(stderr, "invalid parameter for v4-threshold : %s\n", optarg);
279291
exit(1);
280292
}
281293
break;
282-
case 6:
294+
case IDX_V6_THRESHOLD:
283295
i = sscanf(optarg, "%u", &params.v6_threshold);
284296
if (i != 1 || params.v6_threshold<1)
285297
{

mdig/mdig.c

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -467,25 +467,38 @@ static void exithelp(void)
467467
#define PRINT_VER printf("self-built version %s %s\n\n", __DATE__, __TIME__)
468468
#endif
469469

470+
enum opt_indices {
471+
IDX_HELP,
472+
IDX_THREADS,
473+
IDX_FAMILY,
474+
IDX_VERBOSE,
475+
IDX_STATS,
476+
IDX_LOG_RESOLVED,
477+
IDX_LOG_FAILED,
478+
IDX_DNS_MAKE_QUERY,
479+
IDX_DNS_PARSE_QUERY,
480+
IDX_LAST,
481+
};
482+
483+
static const struct option long_options[] = {
484+
[IDX_HELP] = {"help", no_argument, 0, 0},
485+
[IDX_THREADS] = {"threads", required_argument, 0, 0},
486+
[IDX_FAMILY] = {"family", required_argument, 0, 0},
487+
[IDX_VERBOSE] = {"verbose", no_argument, 0, 0},
488+
[IDX_STATS] = {"stats", required_argument, 0, 0},
489+
[IDX_LOG_RESOLVED] = {"log-resolved", required_argument, 0, 0},
490+
[IDX_LOG_FAILED] = {"log-failed", required_argument, 0, 0},
491+
[IDX_DNS_MAKE_QUERY] = {"dns-make-query", required_argument, 0, 0},
492+
[IDX_DNS_PARSE_QUERY] = {"dns-parse-query", no_argument, 0, 0},
493+
[IDX_LAST] = {NULL, 0, NULL, 0},
494+
};
495+
470496
int main(int argc, char **argv)
471497
{
472498
int r, v, option_index = 0;
473499
char fn1[256],fn2[256];
474500
char dom[256];
475501

476-
static const struct option long_options[] = {
477-
{"help",no_argument,0,0}, // optidx=0
478-
{"threads",required_argument,0,0}, // optidx=1
479-
{"family",required_argument,0,0}, // optidx=2
480-
{"verbose",no_argument,0,0}, // optidx=3
481-
{"stats",required_argument,0,0}, // optidx=4
482-
{"log-resolved",required_argument,0,0}, // optidx=5
483-
{"log-failed",required_argument,0,0}, // optidx=6
484-
{"dns-make-query",required_argument,0,0}, // optidx=7
485-
{"dns-parse-query",no_argument,0,0}, // optidx=8
486-
{NULL,0,NULL,0}
487-
};
488-
489502
memset(&glob, 0, sizeof(glob));
490503
*fn1 = *fn2 = *dom = 0;
491504
glob.family = FAMILY4;
@@ -495,19 +508,19 @@ int main(int argc, char **argv)
495508
if (v) exithelp();
496509
switch (option_index)
497510
{
498-
case 0: /* help */
511+
case IDX_HELP:
499512
PRINT_VER;
500513
exithelp();
501514
break;
502-
case 1: /* threads */
515+
case IDX_THREADS:
503516
glob.threads = optarg ? atoi(optarg) : 0;
504517
if (glob.threads <= 0 || glob.threads > 100)
505518
{
506519
fprintf(stderr, "thread number must be within 1..100\n");
507520
return 1;
508521
}
509522
break;
510-
case 2: /* family */
523+
case IDX_FAMILY:
511524
if (!strcmp(optarg, "4"))
512525
glob.family = FAMILY4;
513526
else if (!strcmp(optarg, "6"))
@@ -520,25 +533,25 @@ int main(int argc, char **argv)
520533
return 1;
521534
}
522535
break;
523-
case 3: /* verbose */
536+
case IDX_VERBOSE:
524537
glob.verbose = '\1';
525538
break;
526-
case 4: /* stats */
539+
case IDX_STATS:
527540
glob.stats_every = optarg ? atoi(optarg) : 0;
528541
break;
529-
case 5: /* log-resolved */
542+
case IDX_LOG_RESOLVED:
530543
strncpy(fn1,optarg,sizeof(fn1));
531544
fn1[sizeof(fn1)-1] = 0;
532545
break;
533-
case 6: /* log-failed */
546+
case IDX_LOG_FAILED:
534547
strncpy(fn2,optarg,sizeof(fn2));
535548
fn2[sizeof(fn2)-1] = 0;
536549
break;
537-
case 7: /* dns-make-query */
550+
case IDX_DNS_MAKE_QUERY:
538551
strncpy(dom,optarg,sizeof(dom));
539552
dom[sizeof(dom)-1] = 0;
540553
break;
541-
case 8: /* dns-parse-query */
554+
case IDX_DNS_PARSE_QUERY:
542555
return dns_parse_query();
543556
}
544557
}

0 commit comments

Comments
 (0)