Annotation of 43BSD/contrib/xns/examples/print/getopt.c, revision 1.1.1.1

1.1       root        1: 
                      2: #include <stdio.h>
                      3: 
                      4: /*
                      5:  * get option letter from argument vector
                      6:  */
                      7: int    opterr = 1,             /* useless, never set or used */
                      8:        optind = 1,             /* index into parent argv vector */
                      9:        optopt;                 /* character checked for validity */
                     10: char   *optarg;                /* argument associated with option */
                     11: 
                     12: #define BADCH  (int)'?'
                     13: #define EMSG   ""
                     14: #define tell(s)        fputs(*nargv,stderr);fputs(s,stderr); \
                     15:                fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
                     16: 
                     17: getopt(nargc,nargv,ostr)
                     18: int    nargc;
                     19: char   **nargv,
                     20:        *ostr;
                     21: {
                     22:        static char     *place = EMSG;  /* option letter processing */
                     23:        register char   *oli;           /* option letter list index */
                     24:        char    *index();
                     25: 
                     26:        if(!*place) {                   /* update scanning pointer */
                     27:                if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
                     28:                if (*place == '-') {    /* found "--" */
                     29:                        ++optind;
                     30:                        return(EOF);
                     31:                }
                     32:        }                               /* option letter okay? */
                     33:        if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
                     34:                if(!*place) ++optind;
                     35:                tell(": illegal option -- ");
                     36:        }
                     37:        if (*++oli != ':') {            /* don't need argument */
                     38:                optarg = NULL;
                     39:                if (!*place) ++optind;
                     40:        }
                     41:        else {                          /* need an argument */
                     42:                if (*place) optarg = place;     /* no white space */
                     43:                else if (nargc <= ++optind) {   /* no arg */
                     44:                        place = EMSG;
                     45:                        tell(": option requires an argument -- ");
                     46:                }
                     47:                else optarg = nargv[optind];    /* white space */
                     48:                place = EMSG;
                     49:                ++optind;
                     50:        }
                     51:        return(optopt);                 /* dump back option letter */
                     52: }
                     53: 
                     54: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.