home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / byte-benchmarks3.1 / part01 / src / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-01  |  3.1 KB  |  100 lines

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