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
17 #define ERR(s, c) if(opterr){\
18 extern int strlen(), write();\
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);}
26 extern char *strchr();
34 getopt(argc
, argv
, opts
)
44 argv
[optind
][0] != '-' || argv
[optind
][1] == '\0')
46 else if(strcmp(argv
[optind
], "--") == NULL
) {
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') {
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
);
67 optarg
= argv
[optind
++];
70 if(argv
[optind
][++sp
] == '\0') {