Annotation of 43BSDReno/contrib/mh/miscellany/mem/getopt.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

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