home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 443.lha / pcl2english_v2.0 / getopt.c < prev    next >
C/C++ Source or Header  |  1990-12-02  |  4KB  |  122 lines

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