home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd7.lzh / SRC / getopt.c < prev    next >
Text File  |  1990-02-18  |  2KB  |  67 lines

  1. /*
  2.  * getopt - get option letter from argv
  3.  *      This software is in the public domain
  4.  *      Originally written by Henry Spencer at the U. of Toronto
  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(); /* obsolete, used strchr (JDC). */
  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. #ifndef OSK
  44.         place = strchr(optstring, c);
  45. #else
  46.         place = index(optstring, c);
  47. #endif
  48.  
  49.         if (place == NULL || c == ':') {
  50.                 fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
  51.                 return('?');
  52.         }
  53.  
  54.         place++;
  55.         if (*place == ':') {
  56.                 if (*scan != '\0') {
  57.                         optarg = scan;
  58.                         scan = NULL;
  59.                 } else {
  60.                         optarg = argv[optind];
  61.                         optind++;
  62.                 }
  63.         }
  64.  
  65.         return(c);
  66. }
  67.