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

Defined functions

Defined variables

optarg defined in line 9; used 3 times
opterr defined in line 6; never used

Defined macros

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