home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / string.arc / GETOPT.C < prev    next >
Text File  |  1984-12-31  |  1KB  |  50 lines

  1. /*  File   : getopt.c
  2.     Author : Henry Spencer, University of Toronto
  3.     Updated: 28 April 1984
  4.     Purpose: get option letter from argv.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include "strings.h"
  9.  
  10. char    *optarg;    /* Global argument pointer. */
  11. int    optind = 0;    /* Global argv index. */
  12.  
  13. int getopt(argc, argv, optstring)
  14.     int argc;
  15.     char *argv[];
  16.     char *optstring;
  17.     {
  18.     register int c;
  19.     register char *place;
  20.     static char *scan = NullS;    /* Private scan pointer. */
  21.  
  22.     optarg = NullS;
  23.  
  24.     if (scan == NullS || *scan == '\0') {
  25.         if (optind == 0) optind++;
  26.         if (optind >= argc) return EOF;
  27.         place = argv[optind];
  28.         if (place[0] != '-' || place[1] == '\0') return EOF;
  29.         optind++;
  30.         if (place[1] == '-' && place[2] == '\0') return EOF;
  31.         scan = place+1;
  32.     }
  33.  
  34.     c = *scan++;
  35.     place = index(optstring, c);
  36.     if (place == NullS || c == ':') {
  37.         fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
  38.         return '?';
  39.     }
  40.     if (*++place == ':') {
  41.         if (*scan != '\0') {
  42.         optarg = scan, scan = NullS;
  43.         } else {
  44.         optarg = argv[optind], optind++;
  45.         }
  46.     }
  47.     return c;
  48.     }
  49.