home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / netsrcs / markov / getopt.c next >
C/C++ Source or Header  |  1987-03-06  |  1KB  |  63 lines

  1. /*
  2.  * getopt - get option letter from argv
  3.  * by Henry Spencer
  4.  * posted to Usenet net.sources list
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. char    *optarg;    /* Global argument pointer. */
  10. int    optind = 0;    /* Global argv index. */
  11.  
  12. static char    *scan = NULL;    /* Private scan pointer. */
  13.  
  14. extern char    *index();
  15.  
  16. int
  17. getopt(argc, argv, optstring)
  18. int argc;
  19. char *argv[];
  20. char *optstring;
  21. {
  22.     register char c;
  23.     register char *place;
  24.  
  25.     optarg = NULL;
  26.  
  27.     if (scan == NULL || *scan == '\0') {
  28.         if (optind == 0)
  29.             optind++;
  30.     
  31.         if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
  32.             return(EOF);
  33.         if (strcmp(argv[optind], "--")==0) {
  34.             optind++;
  35.             return(EOF);
  36.         }
  37.     
  38.         scan = argv[optind]+1;
  39.         optind++;
  40.     }
  41.  
  42.     c = *scan++;
  43.     place = index(optstring, c);
  44.  
  45.     if (place == NULL || c == ':') {
  46.         fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
  47.         return('?');
  48.     }
  49.  
  50.     place++;
  51.     if (*place == ':') {
  52.         if (*scan != '\0') {
  53.             optarg = scan;
  54.             scan = NULL;
  55.         } else {
  56.             optarg = argv[optind];
  57.             optind++;
  58.         }
  59.     }
  60.  
  61.     return(c);
  62. }
  63.