4 * get option letter from argument vector
6 int opterr = 1, /* useless, never set or used */
7 optind = 1, /* index into parent argv vector */
8 optopt; /* character checked for validity */
9 char *optarg; /* argument associated with option */
11 #define BADCH (int)'?'
13 #define tell(s) fputs(*nargv,stderr);fputs(s,stderr); \
14 fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
16 getopt(nargc,nargv,ostr)
21 static char *place = EMSG; /* option letter processing */
22 register char *oli; /* option letter list index */
25 if(!*place) { /* update scanning pointer */
26 if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
27 if (*place == '-') { /* found "--" */
31 } /* option letter okay? */
32 if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
34 tell(": illegal option -- ");
36 if (*++oli != ':') { /* don't need argument */
38 if (!*place) ++optind;
40 else { /* need an argument */
41 if (*place) optarg = place; /* no white space */
42 else if (nargc <= ++optind) { /* no arg */
44 tell(": option requires an argument -- ");
46 else optarg = nargv[optind]; /* white space */
50 return(optopt); /* dump back option letter */