mkfs: symlink support
[minix.git] / commands / cawf / getopt.c
blob734e64b74e1a38ed5382ad2eecbc40f6d953e155
1 /*
2 Newsgroups: mod.std.unix
3 Subject: public domain AT&T getopt source
4 Date: 3 Nov 85 19:34:15 GMT
6 Here's something you've all been waiting for: the AT&T public domain
7 source for getopt(3). It is the code which was given out at the 1985
8 UNIFORUM conference in Dallas. I obtained it by electronic mail
9 directly from AT&T. The people there assure me that it is indeed
10 in the public domain.
14 /*LINTLIBRARY*/
15 #define NULL 0
16 #define EOF (-1)
17 #define ERR(s, c) if(opterr){\
18 extern int strlen(), write();\
19 char errbuf[2];\
20 errbuf[0] = c; errbuf[1] = '\n';\
21 (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
22 (void) write(2, s, (unsigned)strlen(s));\
23 (void) write(2, errbuf, 2);}
25 extern int strcmp();
26 extern char *strchr();
28 int opterr = 1;
29 int optind = 1;
30 int optopt;
31 char *optarg;
33 int
34 getopt(argc, argv, opts)
35 int argc;
36 char **argv, *opts;
38 static int sp = 1;
39 register int c;
40 register char *cp;
42 if(sp == 1)
43 if(optind >= argc ||
44 argv[optind][0] != '-' || argv[optind][1] == '\0')
45 return(EOF);
46 else if(strcmp(argv[optind], "--") == NULL) {
47 optind++;
48 return(EOF);
50 optopt = c = argv[optind][sp];
51 if(c == ':' || (cp=strchr(opts, c)) == NULL) {
52 ERR(": illegal option -- ", c);
53 if(argv[optind][++sp] == '\0') {
54 optind++;
55 sp = 1;
57 return('?');
59 if(*++cp == ':') {
60 if(argv[optind][sp+1] != '\0')
61 optarg = &argv[optind++][sp+1];
62 else if(++optind >= argc) {
63 ERR(": option requires an argument -- ", c);
64 sp = 1;
65 return('?');
66 } else
67 optarg = argv[optind++];
68 sp = 1;
69 } else {
70 if(argv[optind][++sp] == '\0') {
71 sp = 1;
72 optind++;
74 optarg = NULL;
76 return(c);