home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / UNSHAR.ZIP / GETOPT.C next >
C/C++ Source or Header  |  1990-07-17  |  3KB  |  94 lines

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