home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CRCDOS.ARJ / GETOPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-31  |  2.6 KB  |  85 lines

  1. /***************************************************************/
  2. /* Module name: getopt.c                                       */
  3. /* Date: 8 Oct 87                                              */
  4. /* Environment: Turbo C 1.0                                    */
  5. /* Author: Synchronistic Software (Was it JUST a coincidence?) */
  6. /* Notice: Public Domain -- May not be used to gain profit     */
  7. /***************************************************************/
  8. static char    *__ATH__ =
  9. "@(#)getopt() v2.0 (31Dec87): Public Domain (P) 1987 Synchronistic Software";
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <dir.h>
  14.  
  15. char    *optarg;
  16. int    opterr = 1;    /* Default is to emit error messages */
  17. int    optind = 1;    /* Start on the first argument after command name */
  18. int    optopt;        /* The real argument -- for AT&T compatibility */
  19.             /* This is the option the user selected, even if */
  20.             /* the option is invalid */
  21. char    SW = '-';    /* For semi-compatibility with Borland */
  22.  
  23. #define PRINT_ERROR(message)    if (opterr) { \
  24.         if (argv[0] != '\0') \
  25.             fprintf( stderr, "%s: ", argv[0] ); \
  26.         else \
  27.             fprintf( stderr, "getopt: " ); \
  28.         fprintf ( stderr, "'%c' %s\n", ch, message ); \
  29.     }
  30.  
  31. int getopt( int argc, char **argv, char *opts )
  32. {
  33.     register int    ch;
  34.     register char    *pt;
  35.     static char    *next = NULL;    /* Saves the next char */
  36.  
  37.     if (next == NULL) {
  38.         next = &argv[optind][1];
  39.  
  40.         /* If there are no more options or */
  41.         /* if this is not an option return EOF */
  42.         if (optind >= argc || next[-1] != SW || next[0] == '\0')
  43.             return EOF;
  44.  
  45.         /* If this is the special end-of-options indicator, */
  46.         /* return EOF */
  47.         if (next[0] == SW && next[1] == '\0') {
  48.             ++optind;    /* Skip this special indicator */
  49.             return EOF;
  50.         }
  51.     }
  52.  
  53.     /* Get the option letter, and see if it's valid */
  54.     /* Set next to point to the character AFTER ch */
  55.     /* ':' is ALWAYS an invalid character, by definition of getopt() */
  56.     if ((pt = strchr( opts, optopt = ch = *next++ )) == NULL || ch == ':') {
  57.         PRINT_ERROR( "is an illegal option" );
  58.         return '?';
  59.     }
  60.  
  61.     /* Should there be an option argument? */
  62.     if (*++pt == ':') {
  63.         /* Is the argument part of the option? */
  64.         if (*next != '\0') {
  65.             optarg = next, next = NULL;
  66.             ++optind;
  67.             return ch;
  68.         }
  69.         /* If there are no more arguments, return EOF */
  70.         if (++optind >= argc) {
  71.             PRINT_ERROR( "requires an argument" );
  72.             return '?';
  73.         }
  74.         /* Return the next argument in the command line */
  75.         optarg = argv[optind++], next = NULL;
  76.         return ch;
  77.     }
  78.  
  79.     /* There is no option argument */
  80.     optarg = NULL;
  81.     if (*next == '\0')
  82.         ++optind, next = NULL;
  83.     return ch;
  84. }
  85.