Annotation of researchv10no/cmd/gre/getopt.c, revision 1.1

1.1     ! root        1: #include "re.h"
        !             2: #include "lre.h"
        !             3: #include "hdr.h"
        !             4: 
        !             5: #define ERR(str, chr)       if(opterr){fprint(2, "%s%s%c\n", argv[0], str, chr);}
        !             6: #define        EOF     -1
        !             7: #define        NULL    0
        !             8: int     opterr = 1;
        !             9: int     optind = 1;
        !            10: int    optopt;
        !            11: char    *optarg;
        !            12: 
        !            13: int
        !            14: getopt(int argc, char **argv, char *opts)
        !            15: {
        !            16:        static int sp = 1;
        !            17:        register c;
        !            18:        register char *cp;
        !            19: 
        !            20:        if (sp == 1)
        !            21:                if (optind >= argc ||
        !            22:                   argv[optind][0] != '-' || argv[optind][1] == '\0')
        !            23:                        return EOF;
        !            24:                else if (strcmp(argv[optind], "--") == NULL) {
        !            25:                        optind++;
        !            26:                        return EOF;
        !            27:                }
        !            28:        optopt = c = argv[optind][sp];
        !            29:        if (c == ':' || (cp=strchr(opts, c)) == NULL) {
        !            30:                ERR (": illegal option -- ", c);
        !            31:                if (argv[optind][++sp] == '\0') {
        !            32:                        optind++;
        !            33:                        sp = 1;
        !            34:                }
        !            35:                return '?';
        !            36:        }
        !            37:        if (*++cp == ':') {
        !            38:                if (argv[optind][sp+1] != '\0')
        !            39:                        optarg = &argv[optind++][sp+1];
        !            40:                else if (++optind >= argc) {
        !            41:                        ERR (": option requires an argument -- ", c);
        !            42:                        sp = 1;
        !            43:                        return '?';
        !            44:                } else
        !            45:                        optarg = argv[optind++];
        !            46:                sp = 1;
        !            47:        } else {
        !            48:                if (argv[optind][++sp] == '\0') {
        !            49:                        sp = 1;
        !            50:                        optind++;
        !            51:                }
        !            52:                optarg = NULL;
        !            53:        }
        !            54:        return c;
        !            55: }

unix.superglobalmegacorp.com

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