2 * This is part of a changed public domain getopt implementation that
3 * had the following text on top:
5 * I got this off net.sources from Henry Spencer.
6 * It is a public domain getopt(3) like in System V.
7 * I have made the following modifications:
9 * A test main program was added, ifdeffed by GETOPT.
10 * This main program is a public domain implementation
11 * of the getopt(1) program like in System V. The getopt
12 * program can be used to standardize shell option handling.
13 * e.g. cc -DGETOPT getopt.c -o getopt
25 int opterr = 1; /* useless, never set or used */
26 int optind = 1; /* index into parent argv vector */
27 int optopt; /* character checked for validity */
29 char *optarg; /* argument associated with option */
31 #define tell(s) fputs(*argv,stderr);fputs(s,stderr); \
32 fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
34 int __fastcall__ getopt (int argc, char* const* argv, const char* optstring)
35 /* Get option letter from argument vector */
37 static char *place = EMSG; /* option letter processing */
39 register char *oli; /* option letter list index */
41 if (!*place) { /* update scanning pointer */
42 if (optind >= argc || *(place = argv[optind]) != '-' || !*++place) {
52 /* option letter okay? */
53 if ((optopt = (int) *place++) == ARGCH ||
54 !(oli = strchr (optstring, optopt))) {
58 tell (": illegal option -- ");
60 if (*++oli != ARGCH) {
61 /* don't need argument */
67 /* need an argument */
72 else if (argc <= ++optind) { /* no arg */
74 tell (": option requires an argument -- ");
77 optarg = argv[optind];
82 return (optopt); /* dump back option letter */