1 /* getopt.c (slightly modified by O.Garcia, 12/4/92) */
2 /* now it accepts either '-' or '/' as switch character */
3
4 /*
5 Newsgroups: mod.std.unix
6 Subject: public domain AT&T getopt source
7 Date: 3 Nov 85 19:34:15 GMT
8
9 Here's something you've all been waiting for: the AT&T public domain
10 source for getopt(3). It is the code which was given out at the 1985
11 UNIFORUM conference in Dallas. I obtained it by electronic mail
12 directly from AT&T. The people there assure me that it is indeed
13 in the public domain.
14 */
15
16 #if defined(_MSC_VER)
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #define ERR(s, c) if(opterr){fprintf(stderr,"%s%s%c\n",argv[0],s,c);}
23
24 int opterr = 1;
25 int optind = 1;
26 int optopt;
27 char *optarg;
28
29 int
getopt(int argc,char ** argv,char * opts)30 getopt (int argc, char **argv, char *opts)
31 {
32 static int sp = 1;
33 register int c;
34 register char *cp;
35
36 if (sp == 1)
37 if (optind >= argc ||
38 (argv[optind][0] != '-' && argv[optind][0] != '/')
39 || argv[optind][1] == '\0')
40 return (EOF);
41 else if (strcmp (argv[optind], "--") == 0 ||
42 strcmp (argv[optind], "//") == 0)
43 {
44 optind++;
45 return (EOF);
46 }
47 optopt = c = argv[optind][sp];
48 if (c == ':' || (cp = strchr (opts, c)) == NULL)
49 {
50 ERR (": illegal option -- ", c);
51 if (argv[optind][++sp] == '\0')
52 {
53 optind++;
54 sp = 1;
55 }
56 return ('?');
57 }
58 if (*++cp == ':')
59 {
60 if (argv[optind][sp + 1] != '\0')
61 optarg = &argv[optind++][sp + 1];
62 else if (++optind >= argc)
63 {
64 ERR (": option requires an argument -- ", c);
65 sp = 1;
66 return ('?');
67 }
68 else
69 optarg = argv[optind++];
70 sp = 1;
71 }
72 else
73 {
74 if (argv[optind][++sp] == '\0')
75 {
76 sp = 1;
77 optind++;
78 }
79 optarg = NULL;
80 }
81 return (c);
82 }
83
84 #endif
85