home *** CD-ROM | disk | FTP | other *** search
/ Best Objectech Shareware Selections / UNTITLED.iso / boss / util / cdro / 003 / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-19  |  2.4 KB  |  96 lines

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