-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.c
133 lines (101 loc) · 2.85 KB
/
flags.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
130
131
132
133
#ifdef __ORCAC__
#pragma optimize 79
#pragma noroot
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "flags.h"
// like getopt.
Flags flags;
void help(void)
{
fputs("gopher [options] url\n", stdout);
fputs("-h display help information.\n", stdout);
fputs("-V display version information.\n", stdout);
fputs("-i display http headers.\n", stdout);
fputs("-I http HEAD\n", stdout);
fputs("-O write output to file.\n", stdout);
fputs("-o <file> write output to <file> instead of stdout.\n", stdout);
fputs("-0 use HTTP 1.0\n", stdout);
fputs("-9 use HTTP 0.9\n", stdout);
fputs("\n", stdout);
exit(0);
}
int ParseFlags(int argc, char **argv)
{
char *cp;
char c;
int i;
int j;
memset(&flags, 0, sizeof(flags));
for (i = 1; i < argc; ++i)
{
cp = argv[i];
c = cp[0];
if (c != '-')
return i;
// -- = end of options.
if (cp[1] == '-' && cp[2] == 0)
return i + 1;
// now scan all the flags in the string...
for (j = 1; ; ++j)
{
int skip = 0;
c = cp[j];
if (c == 0) break;
switch (c)
{
case 'h':
help();
break;
case 'i':
flags._i = 1;
break;
case 'I':
flags._I = 1;
break;
case 'O':
flags._O = 1;
flags._o = 0;
break;
case 'v':
flags._v = 1;
break;
case 'V':
flags._V = 1;
break;
case '0':
flags._0 = 1;
flags._9 = 0;
break;
case '9':
flags._9 = 1;
flags._0 = 0;
break;
// take an argument...
// -oarg or -o arg
case 'o':
flags._O = 0;
skip = 1;
if (cp[j + 1])
flags._o = cp + j + 1;
else
{
if (++i >= argc)
{
fprintf(stderr, "option requires an argument -- %c\n", c);
return -1;
}
flags._o = argv[i];
}
break;
default:
fprintf(stderr, "illegal option -- %c\n", c);
return -1;
}
if (skip) break;
}
}
return i;
}