-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.c
116 lines (104 loc) · 2.16 KB
/
options.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
#ifdef __ORCAC__
#pragma optimize 79
#pragma noroot
#endif
#include <stdio.h>
#include <stdlib.h>
#include "options.h"
extern void help(void);
extern void version(void);
/* global */
struct Options flags;
int GetOptions(int argc, char **argv,
struct Options *options)
{
int i, j;
int eof = 0;
int mindex = 1; /* mutation index */
for (i = 1; i < argc; ++i)
{
char *cp = argv[i];
char c = cp[0];
if (eof || c != '-')
{
if (mindex != i) argv[mindex] = argv[i];
++mindex;
continue;
}
// long opt check would go here...
if (cp[1] == '-' && cp[2] == 0)
{
eof = 1;
continue;
}
// special case for '-'
j = 0;
if (cp[1] != 0) j = 1;
for (; ; ++j)
{
char *optarg = 0;
c = cp[j];
if (!c) break;
switch(c)
{
case '0':
options->_0 = 1;
options->_1 = 0;
options->_9 = 0;
break;
case '1':
options->_1 = 1;
options->_0 = 0;
options->_9 = 0;
break;
case '9':
options->_9 = 1;
options->_0 = 0;
options->_1 = 0;
break;
case 'I':
options->_I = 1;
break;
case 'O':
options->_O = 1;
break;
case 'V':
version();
exit(0);
break;
case 'h':
help();
exit(0);
break;
case 'i':
options->_i = 1;
break;
case 'o':
++j;
if (cp[j]) {
optarg = cp + j;
} else {
++i;
if (i < argc) optarg = argv[i];
}
if (!optarg) {
fputs("-o requires an argument\n", stderr);
exit(1);
}
options->_o = optarg;
options->_O = 0;
break;
case 'v':
options->_v = 1;
break;
default:
fprintf(stderr, "-%c : invalid option\n", c);
exit(1);
break;
}
// could optimize out if no options have flags.
if (optarg) break;
}
}
return mindex;
}