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: }

Defined functions

getopt defined in line 17; used 1 times

Defined variables

optarg defined in line 10; used 3 times
opterr defined in line 7; never used

Defined macros

BADCH defined in line 12; used 1 times
  • in line 15
EMSG defined in line 13; used 3 times
tell defined in line 14; used 2 times
Last modified: 1986-03-13
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 947
Valid CSS Valid XHTML 1.0 Strict