home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3399 / getopt.c
Encoding:
C/C++ Source or Header  |  1991-05-22  |  1.8 KB  |  94 lines

  1. /*
  2. **    This getopt behaves pretty much like you would expect.
  3. **    It does handle arguments like '-ab-' a little differently
  4. **  then normal;  I think the -- 'stop option processing' should
  5. **  be treated like just another option,  so that's what mine does.
  6. **  Other getopts seem to ignore the second '-' in '-ab-'.
  7. **
  8. **  I hereby place this version of getopt in
  9. **  the public domain.  Do with this what you will.
  10. **  I'm sure there is a nicer and faster version out there
  11. **  somewhere but I don't care!
  12. **
  13. **  Robert Osborne, May 1991.
  14. */
  15. #include <stdio.h>
  16.  
  17. int optind = 1;
  18. int opterr = 1;
  19. char *optarg = (char *) 0;
  20.  
  21. static char *next_arg = (char *) 0;
  22.  
  23. #define NO_OPT        0
  24. #define OPT_PLAIN    1
  25. #define OPT_ARG        2
  26.  
  27.     
  28. int
  29. getopt(argc, argv, optstring)
  30.     int argc;
  31.     char *argv[];
  32.     char *optstring;
  33. {
  34.     int ret;
  35.     int which = NO_OPT;
  36.     
  37.     if( next_arg == (char *)0 )
  38.     {
  39.         if( argv[optind] == (char *) 0 || argv[optind][0] != '-' )
  40.             return -1;
  41.         next_arg = &argv[optind][1];
  42.     }
  43.     if( *next_arg == '\0' || *next_arg == '-' )
  44.     {
  45.         optind++;
  46.         return -1;
  47.     }
  48.  
  49.     while(*optstring)
  50.         if( *next_arg == *optstring++ )
  51.             which = (*optstring == ':') ? OPT_ARG : OPT_PLAIN;
  52.             
  53.     switch( which )
  54.     {
  55.     case NO_OPT:
  56.     case OPT_PLAIN:
  57.         ret = *next_arg++;
  58.         if( *next_arg == '\0' )
  59.         {
  60.             optind++;
  61.             next_arg = (char *)0;
  62.         }
  63.         if( which == OPT_PLAIN )
  64.             return ret;
  65.         if( opterr )
  66.             fprintf(stderr, "%s: illegal option -- %c\n", argv[0], ret);
  67.         return '?';
  68.     case OPT_ARG:
  69.         ret = *next_arg++;
  70.         optind++;
  71.         if( *next_arg != '\0' )
  72.         {
  73.             optarg = next_arg;
  74.             next_arg = (char *)0;
  75.             return ret;
  76.         }
  77.         else if( argv[optind] != (char *) 0 )
  78.         {
  79.             optarg = argv[optind];
  80.             optind++;
  81.             next_arg = (char *) 0;
  82.             return ret;
  83.         }
  84.         else
  85.         {
  86.             next_arg = (char *) 0;
  87.             if( opterr )
  88.                 fprintf(stderr, "%s: option requires an option -- %c\n",
  89.                         argv[0], ret);
  90.             return '?';
  91.         }
  92.     }
  93. }
  94.