home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / OS2MNX1.ZIP / GETOPT.C < prev    next >
Text File  |  1988-04-12  |  3KB  |  95 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. #define index strchr
  26. #ifdef BSD
  27. #include <strings.h>
  28. #else
  29. #include <string.h>
  30. #endif
  31.  
  32. /*LINTLIBRARY*/
  33. #define NULL    0
  34. #define EOF    (-1)
  35. #define ERR(s, c)    if(opterr){\
  36.     extern int write();\
  37.     char errbuf[2];\
  38.     errbuf[0] = c; errbuf[1] = '\n';\
  39.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  40.     (void) write(2, s, (unsigned)strlen(s));\
  41.     (void) write(2, errbuf, 2);}
  42.  
  43. extern char *index();
  44.  
  45. int    opterr = 1;
  46. int    optind = 1;
  47. int    optopt;
  48. char    *optarg;
  49.  
  50. int
  51. getopt(argc, argv, opts)
  52. int    argc;
  53. char    **argv, *opts;
  54. {
  55.     static int sp = 1;
  56.     register int c;
  57.     register char *cp;
  58.  
  59.     if(sp == 1)
  60.         if(optind >= argc ||
  61.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  62.             return(EOF);
  63.         else if(strcmp(argv[optind], "--") == NULL) {
  64.             optind++;
  65.             return(EOF);
  66.         }
  67.     optopt = c = argv[optind][sp];
  68.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  69.         ERR(": illegal option -- ", c);
  70.         if(argv[optind][++sp] == '\0') {
  71.             optind++;
  72.             sp = 1;
  73.         }
  74.         return('?');
  75.     }
  76.     if(*++cp == ':') {
  77.         if(argv[optind][sp+1] != '\0')
  78.             optarg = &argv[optind++][sp+1];
  79.         else if(++optind >= argc) {
  80.             ERR(": option requires an argument -- ", c);
  81.             sp = 1;
  82.             return('?');
  83.         } else
  84.             optarg = argv[optind++];
  85.         sp = 1;
  86.     } else {
  87.         if(argv[optind][++sp] == '\0') {
  88.             sp = 1;
  89.             optind++;
  90.         }
  91.         optarg = NULL;
  92.     }
  93.     return(c);
  94. }
  95.