3 * getopt.c - Unix compatible command line option parsing
7 ** @(#)getopt.c 2.5 (smail) 9/15/87
11 * Here's something you've all been waiting for: the AT&T public domain
12 * source for getopt(3). It is the code which was given out at the 1985
13 * UNIFORUM conference in Dallas. I obtained it by electronic mail
14 * directly from AT&T. The people there assure me that it is indeed
15 * in the public domain.
17 * There is no manual page. That is because the one they gave out at
18 * UNIFORUM was slightly different from the current System V Release 2
19 * manual page. The difference apparently involved a note about the
20 * famous rules 5 and 6, recommending using white space between an option
21 * and its first argument, and not grouping options that have arguments.
22 * Getopt itself is currently lenient about both of these things White
23 * space is allowed, but not mandatory, and the last option in a group can
24 * have an argument. That particular version of the man page evidently
25 * has no official existence, and my source at AT&T did not send a copy.
26 * The current SVR2 man page reflects the actual behavor of this getopt.
27 * However, I am not about to post a copy of anything licensed by AT&T.
36 if(opterr) { fprintf(stderr, "%s%s%lc\n", argv[0], s, c); }
44 * Using the 'const' on the argv below increases code size on SAS/C 6.51 by
45 * 24 bytes, even when it should have no effect on the generated code!
48 getopt(int argc
, char * /*const*/ argv
[], char const *opts
)
56 argv
[optind
][0] != '-' || argv
[optind
][1] == '\0')
58 else if(strcmp(argv
[optind
], "--") == NULL
) {
62 optopt
= c
= argv
[optind
][sp
];
63 if(c
== ':' || (cp
=index(opts
, c
)) == NULL
) {
64 ERR(": illegal option -- ", c
);
65 if(argv
[optind
][++sp
] == '\0') {
72 if(argv
[optind
][sp
+1] != '\0')
73 optarg
= &argv
[optind
++][sp
+1];
74 else if(++optind
>= argc
) {
75 ERR(": option requires an argument -- ", c
);
79 optarg
= argv
[optind
++];
82 if(argv
[optind
][++sp
] == '\0') {