home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 317a.lha / RCS / diff / getopt.c < prev    next >
C/C++ Source or Header  |  1989-12-05  |  3KB  |  115 lines

  1. /*
  2.  * From std-unix@ut-sally.UUCP (Moderator, John Quarterman) Sun Nov  3 14:34:15
  3. 1985
  4.  * Relay-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site gatech.CSNET
  5.  * Posting-Version: version B 2.10.2 9/18/84; site ut-sally.UUCP
  6.  * Path: gatech!akgua!mhuxv!mhuxt!mhuxr!ulysses!allegra!mit-eddie!genrad!panda!
  7. alcott!harvard!seismo!ut-sally!std-unix
  8.  * From: std-unix@ut-sally.UUCP (Moderator, John Quarterman)
  9.  * Newsgroups: mod.std.unix
  10.  * Subject: public domain AT&T getopt source
  11.  * Message-ID: <3352@ut-sally.UUCP>
  12.  * Date: 3 Nov 85 19:34:15 GMT
  13.  * Date-Received: 4 Nov 85 12:25:09 GMT
  14.  * Organization: IEEE/P1003 Portable Operating System Environment Committee
  15.  * Lines: 91
  16.  * Approved: jsq@ut-sally.UUCP
  17.  * 
  18.  * Here's something you've all been waiting for:  the AT&T public domain
  19.  * source for getopt(3).  It is the code which was given out at the 1985
  20.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  21.  * directly from AT&T.  The people there assure me that it is indeed
  22.  * in the public domain.
  23.  * 
  24.  * There is no manual page.  That is because the one they gave out at
  25.  * UNIFORUM was slightly different from the current System V Release 2
  26.  * manual page.  The difference apparently involved a note about the
  27.  * famous rules 5 and 6, recommending using white space between an option
  28.  * and its first argument, and not grouping options that have arguments.
  29.  * Getopt itself is currently lenient about both of these things White
  30.  * space is allowed, but not mandatory, and the last option in a group can
  31.  * have an argument.  That particular version of the man page evidently
  32.  * has no official existence, and my source at AT&T did not send a copy.
  33.  * The current SVR2 man page reflects the actual behavor of this getopt.
  34.  * However, I am not about to post a copy of anything licensed by AT&T.
  35.  * 
  36.  * I will submit this source to Berkeley as a bug fix.
  37.  * 
  38.  * I, personally, make no claims or guarantees of any kind about the
  39.  * following source.  I did compile it to get some confidence that
  40.  * it arrived whole, but beyond that you're on your own.
  41.  * 
  42.  */
  43.  
  44. /*LINTLIBRARY*/
  45.  
  46. #ifndef NULL
  47. #define NULL    0
  48. #endif
  49.  
  50. #ifndef EOF
  51. #define EOF    (-1)
  52. #endif
  53.  
  54. #define ERR(s, c)    if(opterr){\
  55.     extern int strlen(), write();\
  56.     char errbuf[2];\
  57.     errbuf[0] = c; errbuf[1] = '\n';\
  58.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  59.     (void) write(2, s, (unsigned)strlen(s));\
  60.     (void) write(2, errbuf, (unsigned)2);}
  61.  
  62. extern int strcmp();
  63. extern char *strchr();
  64.  
  65. int    opterr = 1;
  66. int    optind = 1;
  67. int    optopt;
  68. char    *optarg;
  69.  
  70. int
  71. getopt(argc, argv, opts)
  72. int    argc;
  73. char    **argv, *opts;
  74. {
  75.     static int sp = 1;
  76.     register int c;
  77.     register char *cp;
  78.  
  79.     if(sp == 1)
  80.         if(optind >= argc ||
  81.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  82.             return(EOF);
  83.         else if(strcmp(argv[optind], "--") == NULL) {
  84.             optind++;
  85.             return(EOF);
  86.         }
  87.     optopt = c = argv[optind][sp];
  88.     if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  89.         ERR("illegal option: ", c);
  90.         if(argv[optind][++sp] == '\0') {
  91.             optind++;
  92.             sp = 1;
  93.         }
  94.         return('?');
  95.     }
  96.     if(*++cp == ':') {
  97.         if(argv[optind][sp+1] != '\0' && argv[optind][sp+1] != '-')
  98.             optarg = &argv[optind++][sp+1];
  99.         else if(++optind >= argc || argv[optind][0] == '-') {
  100.             ERR("option requires an argument: ", c);
  101.             sp = 1;
  102.             return('?');
  103.         } else
  104.             optarg = argv[optind++];
  105.         sp = 1;
  106.     } else {
  107.         if(argv[optind][++sp] == '\0') {
  108.             sp = 1;
  109.             optind++;
  110.         }
  111.         optarg = NULL;
  112.     }
  113.     return(c);
  114. }
  115.