Annotation of coherent/a/tmp/smail/getopt.c, revision 1.1.1.1

1.1       root        1: /*
                      2: **     @(#)getopt.c    2.5 (smail) 9/15/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: /* This include is needed only to get "index" defined as "strchr" on Sys V. */
                     26: #include "defs.h"
                     27: 
                     28: /*LINTLIBRARY*/
                     29: #define NULL   0
                     30: #define EOF    (-1)
                     31: #define ERR(s, c)      if(opterr){\
                     32:        extern int write();\
                     33:        char errbuf[2];\
                     34:        errbuf[0] = c; errbuf[1] = '\n';\
                     35:        (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
                     36:        (void) write(2, s, (unsigned)strlen(s));\
                     37:        (void) write(2, errbuf, 2);}
                     38: 
                     39: extern char *index();
                     40: 
                     41: int    opterr = 1;
                     42: int    optind = 1;
                     43: int    optopt;
                     44: char   *optarg;
                     45: 
                     46: int
                     47: getopt(argc, argv, opts)
                     48: int    argc;
                     49: char   **argv, *opts;
                     50: {
                     51:        static int sp = 1;
                     52:        register int c;
                     53:        register char *cp;
                     54: 
                     55:        if(sp == 1)
                     56:                if(optind >= argc ||
                     57:                   argv[optind][0] != '-' || argv[optind][1] == '\0')
                     58:                        return(EOF);
                     59:                else if(strcmp(argv[optind], "--") == NULL) {
                     60:                        optind++;
                     61:                        return(EOF);
                     62:                }
                     63:        optopt = c = argv[optind][sp];
                     64:        if(c == ':' || (cp=index(opts, c)) == NULL) {
                     65:                ERR(": illegal option -- ", c);
                     66:                if(argv[optind][++sp] == '\0') {
                     67:                        optind++;
                     68:                        sp = 1;
                     69:                }
                     70:                return('?');
                     71:        }
                     72:        if(*++cp == ':') {
                     73:                if(argv[optind][sp+1] != '\0')
                     74:                        optarg = &argv[optind++][sp+1];
                     75:                else if(++optind >= argc) {
                     76:                        ERR(": option requires an argument -- ", c);
                     77:                        sp = 1;
                     78:                        return('?');
                     79:                } else
                     80:                        optarg = argv[optind++];
                     81:                sp = 1;
                     82:        } else {
                     83:                if(argv[optind][++sp] == '\0') {
                     84:                        sp = 1;
                     85:                        optind++;
                     86:                }
                     87:                optarg = NULL;
                     88:        }
                     89:        return(c);
                     90: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.