forked from fritz0705/dhcpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argv.c
129 lines (99 loc) · 2.43 KB
/
argv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "argv.h"
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdio.h>
static const struct option long_options[] =
{
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{"user", required_argument, 0, 'u'},
{"group", required_argument, 0, 'g'},
{"debug", no_argument, 0, 'd'},
// {"log", no_argument, 0, 'l'},
{"interface", required_argument, 0, 'i'},
{"prefixlen", required_argument, 0, 'p'},
{"start", required_argument, 0, 's'},
{"end", required_argument, 0, 'e'},
{"leasetime", required_argument, 0, 't'},
{"ltime", required_argument, 0, 't'},
{"gw", required_argument, 0, 0x10000},
{"gateway", required_argument, 0, 0x10000},
{"ns", required_argument, 0, 0x10001},
{"nameserver", required_argument, 0, 0x10001},
{0, 0, 0, 0}
};
void *(* const argv_realloc)(void *, size_t) = realloc;
bool argv_parse(int argc, char **argv, struct argv *out)
{
out->argv = argv;
out->argc = argc;
out->arg0 = argv[0];
optind = 0;
while(1) {
/* getopt_long stores the option index here. */
int option_index = 0;
int idx = getopt_long (argc, argv, "Vdg:hi:p:s:e:t:u:", long_options, &option_index);
if(-1 == idx) {
break;
}
switch (idx)
{
case 'h':
out->help = true;
break;
case 'V':
out->version = true;
break;
case 'd':
out->debug = true;
break;
case 'i':
out->interface = optarg;
break;
case 'u':
out->user = optarg;
break;
case 'g':
out->group = optarg;
break;
case 'p':
out->prefixlen = optarg;
break;
case 's':
out->iprange[0] = optarg;
break;
case 'e':
out->iprange[1] = optarg;
break;
case 't':
out->leasetime = optarg;
break;
case 0x10000:
out->routers = argv_realloc(
out->routers,
++out->routers_cnt * sizeof(char*));
out->routers[out->routers_cnt - 1] = optarg;
break;
case 0x10001:
out->nameservers = argv_realloc(
out->nameservers,
++out->nameservers_cnt * sizeof(char*));
out->nameservers[out->nameservers_cnt - 1] = optarg;
break;
default:
out->argerror = -1;
return false;
}
}
if (optind < argc) {
printf ("non-option ARGV-elements: ");
while (optind < argc) {
printf ("%s ", argv[optind++]);
putchar ('\n');
}
out->argerror = -1;
return false;
}
return true;
}