home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / file39a.zip / src / localsrc / getopt.c next >
C/C++ Source or Header  |  1993-04-05  |  2KB  |  92 lines

  1.  
  2. /*
  3.  * getopt - get option letter from argv
  4.  *
  5.  * Copyright (c) Henry Spencer.
  6.  * Written by Henry Spencer.
  7.  *
  8.  * This software is not subject to any license of the American Telephone
  9.  * and Telegraph Company or of the Regents of the University of California.
  10.  *
  11.  * Permission is granted to anyone to use this software for any purpose on
  12.  * any computer system, and to alter it and redistribute it freely, subject
  13.  * to the following restrictions:
  14.  *
  15.  * 1. The author is not responsible for the consequences of use of this
  16.  *    software, no matter how awful, even if they arise from flaws in it.
  17.  *
  18.  * 2. The origin of this software must not be misrepresented, either by
  19.  *    explicit claim or by omission.  Since few users ever read sources,
  20.  *    credits must appear in the documentation.
  21.  *
  22.  * 3. Altered versions must be plainly marked as such, and must not be
  23.  *    misrepresented as being the original software.  Since few users
  24.  *    ever read sources, credits must appear in the documentation.
  25.  *
  26.  * 4. This notice may not be removed or altered.
  27.  */
  28.  
  29. /*
  30.  * changed index() calls to strchr() - darwin, oct 87.
  31.  */
  32.  
  33. #include <stdio.h>
  34.  
  35. char    *optarg;    /* Global argument pointer. */
  36. int    optind = 0;    /* Global argv index. */
  37.  
  38. static char    *scan = NULL;    /* Private scan pointer. */
  39.  
  40. extern char    *strchr();
  41.  
  42. int
  43. getopt(argc, argv, optstring)
  44. int argc;
  45. char *argv[];
  46. char *optstring;
  47. {
  48.     register char c;
  49.     register char *place;
  50.  
  51.     optarg = NULL;
  52.  
  53.     if (scan == NULL || *scan == '\0') {
  54.         if (optind == 0)
  55.             optind++;
  56.     
  57.         if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
  58.             return(EOF);
  59.         if (strcmp(argv[optind], "--")==0) {
  60.             optind++;
  61.             return(EOF);
  62.         }
  63.     
  64.         scan = argv[optind]+1;
  65.         optind++;
  66.     }
  67.  
  68.     c = *scan++;
  69.     place = strchr(optstring, c);
  70.  
  71.     if (place == NULL || c == ':') {
  72.         fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
  73.         return('?');
  74.     }
  75.  
  76.     place++;
  77.     if (*place == ':') {
  78.         if (*scan != '\0') {
  79.             optarg = scan;
  80.             scan = NULL;
  81.         } else if (optind < argc) {
  82.             optarg = argv[optind];
  83.             optind++;
  84.         } else {
  85.             fprintf(stderr, "%s: -%c argument missing\n", argv[0], c);
  86.             return('?');
  87.         }
  88.     }
  89.  
  90.     return(c);
  91. }
  92.