3 Newsgroups: mod.std.unix
4 Subject: public domain AT&T getopt source
5 Date: 3 Nov 85 19:34:15 GMT
7 Here's something you've all been waiting for: the AT&T public domain
8 source for getopt(3). It is the code which was given out at the 1985
9 UNIFORUM conference in Dallas. I obtained it by electronic mail
10 directly from AT&T. The people there assure me that it is indeed
18 #define ERR(s, c) if(opterr){\
19 extern int strlen(), write();\
21 errbuf[0] = c; errbuf[1] = '\n';\
22 (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
23 (void) write(2, s, (unsigned)strlen(s));\
24 (void) write(2, errbuf, 2);}
27 extern char *strchr();
34 int getopt(int argc
, char **argv
, char **opts
) {
41 argv
[optind
][0] != '-' || argv
[optind
][1] == '\0')
43 else if(strcmp(argv
[optind
], "--") == NULL
) {
47 optopt
= c
= argv
[optind
][sp
];
48 if(c
== ':' || (cp
=strchr(opts
, c
)) == NULL
) {
49 ERR(": illegal option -- ", c
);
50 if(argv
[optind
][++sp
] == '\0') {
57 if(argv
[optind
][sp
+1] != '\0')
58 optarg
= &argv
[optind
++][sp
+1];
59 else if(++optind
>= argc
) {
60 ERR(": option requires an argument -- ", c
);
64 optarg
= argv
[optind
++];
67 if(argv
[optind
][++sp
] == '\0') {