forked from leirenjieyi/crc32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.c
50 lines (45 loc) · 817 Bytes
/
args.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
#include <stddef.h>
static int argc, argi;
static char **argv, *flag;
static char *
nextarg()
{
char *temp = flag;
flag = NULL;
if (temp[1])
return temp + 1;
if (++argi == argc)
return NULL;
return argv[argi];
}
static void
args_parse(int argc_, char **argv_,
void flagfn(char, char*()), void plainfn(char*))
{
argc = argc_;
argv = argv_;
for (argi = 1; argi < argc; argi++) {
char *arg = argv[argi];
if (!arg[0])
continue;
/* if ... || arg == "-" */
if (arg[0] != '-' || !arg[1]) {
if (plainfn)
plainfn(arg);
continue;
}
/* if arg == "--" */
if (arg[1] == '-' && !arg[2]) {
argi++;
break;
}
for (flag = arg + 1; *flag; flag++) {
flagfn(*flag, nextarg);
if (!flag)
break;
}
}
for (; argi < argc; argi++)
if (plainfn)
plainfn(argv[argi]);
}