home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / languags / c / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  2.5 KB  |  101 lines

  1. From: gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>)
  2. Newsgroups: net.sources
  3. Subject: getopt library routine
  4. Date: 30 Mar 85 04:45:33 GMT
  5.  
  6. /*
  7.     getopt -- public domain version of standard System V routine
  8.  
  9.     Strictly enforces the System V Command Syntax Standard;
  10.     provided by D A Gwyn of BRL for generic ANSI C implementations
  11. */
  12.  
  13. #include    <stdio.h>
  14. #include    <string.h>
  15.  
  16. int    opterr = 1;            /* error => print message */
  17. int    optind = 1;            /* next argv[] index */
  18. char    *optarg = NULL;            /* option parameter if any */
  19.  
  20. static int
  21. Err( name, mess, c )            /* returns '?' */
  22.     char    *name;            /* program name argv[0] */
  23.     char    *mess;            /* specific message */
  24.     int    c;            /* defective option letter */
  25.     {
  26.     if ( opterr )
  27.         (void) fprintf( stderr,
  28.                 "%s: %s -- %c\n",
  29.                 name, mess, c
  30.                   );
  31.  
  32.     return '?';            /* erroneous-option marker */
  33.     }
  34.  
  35. int
  36. getopt( argc, argv, optstring )        /* returns letter, '?', EOF */
  37.     int        argc;        /* argument count from main */
  38.     char        *argv[];    /* argument vector from main */
  39.     char        *optstring;    /* allowed args, e.g. "ab:c" */
  40.     {
  41.     static int    sp = 1;        /* position within argument */
  42.     register int    osp;        /* saved `sp' for param test */
  43.     register int    c;        /* option letter */
  44.     register char    *cp;        /* -> option in `optstring' */
  45.  
  46.     optarg = NULL;
  47.  
  48.     if ( sp == 1 )            /* fresh argument */
  49.         if ( optind >= argc        /* no more arguments */
  50.           || argv[optind][0] != '-'    /* no more options */
  51.           || argv[optind][1] == '\0'    /* not option; stdin */
  52.            )
  53.             return EOF;
  54.         else if ( strcmp( argv[optind], "--" ) == 0 )
  55.             {
  56.             ++optind;    /* skip over "--" */
  57.             return EOF;    /* "--" marks end of options */
  58.             }
  59.  
  60.     c = argv[optind][sp];        /* option letter */
  61.     osp = sp++;            /* get ready for next letter */
  62.  
  63.     if ( argv[optind][sp] == '\0' )    /* end of argument */
  64.         {
  65.         ++optind;        /* get ready for next try */
  66.         sp = 1;            /* beginning of next argument */
  67.         }
  68.  
  69.     if ( c == ':'            /* optstring syntax conflict */
  70.       || (cp = strchr( optstring, c )) == NULL    /* not found */
  71.        )
  72.         return Err( argv[0], "illegal option", c );
  73.  
  74.     if ( cp[1] == ':' )        /* option takes parameter */
  75.         {
  76.         if ( osp != 1 )
  77.             return Err( argv[0],
  78.                     "option must not be clustered",
  79.                     c
  80.                   );
  81.  
  82.         if ( sp != 1 )        /* reset by end of argument */
  83.             return Err( argv[0],
  84.                    "option must be followed by white space",
  85.                     c
  86.                   );
  87.  
  88.         if ( optind >= argc )
  89.             return Err( argv[0],
  90.                     "option requires an argument",
  91.                     c
  92.                   );
  93.  
  94.         optarg = argv[optind];    /* make parameter available */
  95.         ++optind;        /* skip over parameter */
  96.         }
  97.  
  98.     return c;
  99.     }
  100.  
  101.