home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / getopt.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  3KB  |  107 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     <compiler.h>
  15. #include     <stddef.h>
  16. #include    <stdio.h>
  17. #include    <string.h>
  18. #include    <unistd.h>
  19.  
  20. int    opterr = 1;            /* error => print message */
  21. int    optind = 1;            /* next argv[] index */
  22. char    *optarg = NULL;            /* option parameter if any */
  23.  
  24. static int Err __PROTO((char *, char *, int));
  25.  
  26. static int
  27. Err( name, mess, c )            /* returns '?' */
  28.     char    *name;            /* program name argv[0] */
  29.     char    *mess;            /* specific message */
  30.     int    c;            /* defective option letter */
  31.     {
  32.     if ( opterr )
  33.         (void) fprintf( stderr,
  34.                 "%s: %s -- %c\n",
  35.                 name, mess, c
  36.                   );
  37.  
  38.     return '?';            /* erroneous-option marker */
  39.     }
  40.  
  41. int
  42. getopt( argc, argv, optstring )        /* returns letter, '?', EOF */
  43.     int        argc;        /* argument count from main */
  44.         char * const    *argv;          /* argument vector from main */
  45.     const char    *optstring;    /* allowed args, e.g. "ab:c" */
  46.     {
  47.     static int    sp = 1;        /* position within argument */
  48.     register int    osp;        /* saved `sp' for param test */
  49.     register int    c;        /* option letter */
  50.     register char    *cp;        /* -> option in `optstring' */
  51.  
  52.     optarg = NULL;
  53.  
  54.     if ( sp == 1 )            /* fresh argument */
  55.         if ( optind >= argc        /* no more arguments */
  56.           || argv[optind][0] != '-'    /* no more options */
  57.           || argv[optind][1] == '\0'    /* not option; stdin */
  58.            )
  59.             return EOF;
  60.         else if ( strcmp( argv[optind], "--" ) == 0 )
  61.             {
  62.             ++optind;    /* skip over "--" */
  63.             return EOF;    /* "--" marks end of options */
  64.             }
  65.  
  66.     c = argv[optind][sp];        /* option letter */
  67.     osp = sp++;            /* get ready for next letter */
  68.  
  69.     if ( argv[optind][sp] == '\0' )    /* end of argument */
  70.         {
  71.         ++optind;        /* get ready for next try */
  72.         sp = 1;            /* beginning of next argument */
  73.         }
  74.  
  75.     if ( c == ':'            /* optstring syntax conflict */
  76.       || (cp = strchr( optstring, c )) == NULL    /* not found */
  77.        )
  78.         return Err( argv[0], "illegal option", c );
  79.  
  80.     if ( cp[1] == ':' )        /* option takes parameter */
  81.         {
  82.         if ( osp != 1 )
  83.             return Err( argv[0],
  84.                     "option must not be clustered",
  85.                     c
  86.                   );
  87.  
  88.         if ( sp != 1 )        /* reset by end of argument */
  89.             return Err( argv[0],
  90.                    "option must be followed by white space",
  91.                     c
  92.                   );
  93.  
  94.         if ( optind >= argc )
  95.             return Err( argv[0],
  96.                     "option requires an argument",
  97.                     c
  98.                   );
  99.  
  100.         optarg = (char *)argv[optind];    /* make parameter available */
  101.         ++optind;        /* skip over parameter */
  102.         }
  103.  
  104.     return c;
  105.     }
  106.  
  107.