home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume11 / tab / part01 / getopt.c next >
C/C++ Source or Header  |  1990-03-10  |  3KB  |  89 lines

  1. /* ::[[ @(#) getopt.c 1.5 89/07/02 00:18:19 ]]:: */
  2. #ifndef LINT
  3. static char sccsid[]="::[[ @(#) getopt.c 1.5 89/07/02 00:18:19 ]]::";
  4. #endif
  5.  
  6. /*
  7.  * Here's something you've all been waiting for:  the AT&T public domain
  8.  * source for getopt(3).  It is the code which was given out at the 1985
  9.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  10.  * directly from AT&T.  The people there assure me that it is indeed
  11.  * in the public domain.
  12.  *
  13.  * There is no manual page.  That is because the one they gave out at
  14.  * UNIFORUM was slightly different from the current System V Release 2
  15.  * manual page.  The difference apparently involved a note about the
  16.  * famous rules 5 and 6, recommending using white space between an option
  17.  * and its first argument, and not grouping options that have arguments.
  18.  * Getopt itself is currently lenient about both of these things White
  19.  * space is allowed, but not mandatory, and the last option in a group can
  20.  * have an argument.  That particular version of the man page evidently
  21.  * has no official existence, and my source at AT&T did not send a copy.
  22.  * The current SVR2 man page reflects the actual behavor of this getopt.
  23.  * However, I am not about to post a copy of anything licensed by AT&T.
  24.  */
  25.  
  26. /*
  27. Minor modifications by Rahul Dhesi 1989/03/06
  28. */
  29.  
  30. #include <string.h>
  31. #include <stdio.h>
  32.  
  33. /* Avoid possible compiler warning if we simply redefine NULL or EOF */
  34. #define XNULL   0
  35. #define XEOF (-1)
  36.  
  37. #define ERR(szz,czz) if(opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
  38.  
  39. int   opterr = 1;
  40. int   optind = 1;
  41. int   optopt;
  42. char  *optarg;
  43.  
  44. int
  45. getopt(argc, argv, opts)
  46. int   argc;
  47. char  **argv, *opts;
  48. {
  49.    static int sp = 1;
  50.    register int c;
  51.    register char *cp;
  52.  
  53.    if(sp == 1)
  54.       if(optind >= argc ||
  55.          argv[optind][0] != '-' || argv[optind][1] == '\0')
  56.          return(XEOF);
  57.       else if(strcmp(argv[optind], "--") == XNULL) {
  58.          optind++;
  59.          return(XEOF);
  60.       }
  61.    optopt = c = argv[optind][sp];
  62.    if(c == ':' || (cp=strchr(opts, c)) == XNULL) {
  63.       ERR(": illegal option -- ", c);
  64.       if(argv[optind][++sp] == '\0') {
  65.          optind++;
  66.          sp = 1;
  67.       }
  68.       return('?');
  69.    }
  70.    if(*++cp == ':') {
  71.       if(argv[optind][sp+1] != '\0')
  72.          optarg = &argv[optind++][sp+1];
  73.       else if(++optind >= argc) {
  74.          ERR(": option requires an argument -- ", c);
  75.          sp = 1;
  76.          return('?');
  77.       } else
  78.          optarg = argv[optind++];
  79.       sp = 1;
  80.    } else {
  81.       if(argv[optind][++sp] == '\0') {
  82.          sp = 1;
  83.          optind++;
  84.       }
  85.       optarg = XNULL;
  86.    }
  87.    return(c);
  88. }
  89.