1: /*
   2:  * Copyright (c) 1985 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if defined(LIBC_SCCS) && !defined(lint)
   8: static char sccsid[] = "@(#)getopt.c	4.3 (Berkeley) 3/9/86";
   9: #endif LIBC_SCCS and not lint
  10: 
  11: #include <stdio.h>
  12: 
  13: /*
  14:  * get option letter from argument vector
  15:  */
  16: int opterr = 1,     /* if error message should be printed */
  17:     optind = 1,     /* index into parent argv vector */
  18:     optopt;         /* character checked for validity */
  19: char    *optarg;        /* argument associated with option */
  20: 
  21: #define BADCH   (int)'?'
  22: #define EMSG    ""
  23: #define tell(s) if (opterr) {fputs(*nargv,stderr);fputs(s,stderr); \
  24:         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);}
  25: 
  26: getopt(nargc,nargv,ostr)
  27: int nargc;
  28: char    **nargv,
  29:     *ostr;
  30: {
  31:     static char *place = EMSG;  /* option letter processing */
  32:     register char   *oli;       /* option letter list index */
  33:     char    *index();
  34: 
  35:     if(!*place) {           /* update scanning pointer */
  36:         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  37:         if (*place == '-') {    /* found "--" */
  38:             ++optind;
  39:             return(EOF);
  40:         }
  41:     }               /* option letter okay? */
  42:     if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  43:         if(!*place) ++optind;
  44:         tell(": illegal option -- ");
  45:     }
  46:     if (*++oli != ':') {        /* don't need argument */
  47:         optarg = NULL;
  48:         if (!*place) ++optind;
  49:     }
  50:     else {              /* need an argument */
  51:         if (*place) optarg = place; /* no white space */
  52:         else if (nargc <= ++optind) {   /* no arg */
  53:             place = EMSG;
  54:             tell(": option requires an argument -- ");
  55:         }
  56:         else optarg = nargv[optind];    /* white space */
  57:         place = EMSG;
  58:         ++optind;
  59:     }
  60:     return(optopt);         /* dump back option letter */
  61: }

Defined functions

getopt defined in line 26; used 3 times

Defined variables

optarg defined in line 19; used 8 times
opterr defined in line 16; used 1 times
  • in line 23
sccsid defined in line 8; never used

Defined macros

BADCH defined in line 21; used 1 times
  • in line 24
EMSG defined in line 22; used 3 times
tell defined in line 23; used 2 times
Last modified: 1986-03-10
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: ?E00
Valid CSS Valid XHTML 1.0 Strict