2 * * @(#)getopt.c 2.3 (smail) 5/30/87
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 directly
9 * from AT&T. The people there assure me that it is indeed in the public
12 * There is no manual page. That is because the one they gave out at UNIFORUM
13 * was slightly different from the current System V Release 2 manual page.
14 * The difference apparently involved a note about the famous rules 5 and 6,
15 * recommending using white space between an option and its first argument,
16 * and not grouping options that have arguments. Getopt itself is currently
17 * lenient about both of these things White space is allowed, but not
18 * mandatory, and the last option in a group can have an argument. That
19 * particular version of the man page evidently has no official existence,
20 * and my source at AT&T did not send a copy. The current SVR2 man page
21 * reflects the actual behavior of this getopt. However, I am not about to
22 * post a copy of anything licensed by AT&T.
38 #define ERR(s, c) if(opterr){\
39 extern int write(int, void *, unsigned);\
41 errbuf[0] = (char)c; errbuf[1] = '\n';\
42 (void) write(2, strlwr(argv[0]), (unsigned)strlen(argv[0]));\
43 (void) write(2, s, (unsigned)strlen(s));\
44 (void) write(2, errbuf, 2);}
53 int getopt(int argc
, char *argv
[], char *opts
)
61 if (optind
>= argc
|| argv
[optind
][0] != '-' ||
62 argv
[optind
][1] == '\0')
64 else if (strcmp(argv
[optind
], "--") == 0)
70 optopt
= c
= argv
[optind
][sp
];
71 if (c
== ':' || (cp
= index(opts
, c
)) == NULL
)
73 ERR(": illegal option -- ", c
);
74 if (argv
[optind
][++sp
] == '\0')
83 if (argv
[optind
][sp
+ 1] != '\0')
84 optarg
= &argv
[optind
++][sp
+ 1];
85 else if (++optind
>= argc
)
87 ERR(": option requires an argument -- ", c
);
91 else optarg
= argv
[optind
++];
96 if (argv
[optind
][++sp
] == '\0')