home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / mod.std.unix.v3 / text0001.txt < prev    next >
Encoding:
Text File  |  1987-06-30  |  2.5 KB  |  93 lines

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