-
Notifications
You must be signed in to change notification settings - Fork 0
/
getopt_simplest.c
41 lines (40 loc) · 1.03 KB
/
getopt_simplest.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
#include <getopt_nh7lll77vb62ycgwzwf30zlln.h>
#include <string.h>
#include <assert.h>
int getopt_simplest(
int *optind_ref, int *optpos_ref, int argc, char const *const *argv
) {
int c, i= *optpos_ref, optind= *optind_ref;
if (argc <= 1) {
assert(optind == 0);
assert(i == 0);
end_of_options:
c= 0;
goto done;
}
if (optind == 0) optind= 1; /* Start parsing after argv[0]. */
for (;;) {
assert(optind < argc);
assert((size_t)i <= strlen(argv[optind]));
switch (c= argv[optind][i]) {
case 0:
if (i == 0 || ++optind == argc) goto end_of_options;
i= 0;
continue;
case '-':
if (i == 0 && argv[optind][1] == '\0') goto end_of_options;
if (i == 1 && argv[optind][i + 1] == '\0') {
++optind; goto end_of_options;
}
++i;
continue;
}
break;
}
if (i == 0) goto end_of_options;
++i;
done:
*optind_ref= optind;
*optpos_ref= i;
return c;
}