home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 316 / libsrc / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  2.6 KB  |  102 lines

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