forked from JackHack96/logic-synthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetopt.c
48 lines (42 loc) · 1.08 KB
/
getopt.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
/* File : getopt.c
Author : Henry Spencer, University of Toronto
Updated: 28 April 1984
Purpose: get option letter from argv.
*/
#include "port.h"
#define NullS ((char *) 0)
char *optarg; /* Global argument pointer. */
int optind = 0; /* Global argv index. */
int getopt(argc, argv, optstring)
int argc;
char *argv[];
char *optstring;
{
register int c;
register char *place;
static char *scan = NullS; /* Private scan pointer. */
optarg = NullS;
if (scan == NullS || *scan == '\0') {
if (optind == 0) optind++;
if (optind >= argc) return EOF;
place = argv[optind];
if (place[0] != '-' || place[1] == '\0') return EOF;
optind++;
if (place[1] == '-' && place[2] == '\0') return EOF;
scan = place+1;
}
c = *scan++;
place = strchr(optstring, c);
if (place == NullS || c == ':') {
(void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
return '?';
}
if (*++place == ':') {
if (*scan != '\0') {
optarg = scan, scan = NullS;
} else {
optarg = argv[optind], optind++;
}
}
return c;
}