Annotation of coherent/b/lib/libc/gen/getopt.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * getopt - get option letter from argv
        !             3:  */
        !             4: 
        !             5: #include <stdio.h>
        !             6: 
        !             7: char   *optarg;        /* Global argument pointer. */
        !             8: int    optind = 1;     /* Global argv index. */
        !             9: 
        !            10: static char    *scan = NULL;   /* Private scan pointer. */
        !            11: 
        !            12: extern char    *strchr();
        !            13: 
        !            14: int
        !            15: getopt(argc, argv, optstring)
        !            16: int argc;
        !            17: char *argv[];
        !            18: char *optstring;
        !            19: {
        !            20:        register char c;
        !            21:        register char *place;
        !            22: 
        !            23:        for (optarg = NULL; scan == NULL || !*scan; scan++, optind++) {
        !            24:                if ((optind >= argc) || (*(scan = argv[optind]) != '-')) {
        !            25:                        scan = NULL;
        !            26:                        return(EOF);
        !            27:                }
        !            28:        }
        !            29: 
        !            30:        if ((place = strchr(optstring, c = *scan++)) == NULL || c == ':') {
        !            31:                fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
        !            32:                return('?');
        !            33:        }
        !            34: 
        !            35:        if (place[1] == ':') {
        !            36:                if (*scan) {
        !            37:                        optarg = scan;
        !            38:                        scan = NULL;
        !            39:                } else if (optind < argc)
        !            40:                        optarg = argv[optind++];
        !            41:                else {
        !            42:                        fprintf(stderr, "%s: %c argument missing\n", argv[0], c);
        !            43:                        return('?');
        !            44:                }
        !            45:        }
        !            46: 
        !            47:        return(c);
        !            48: }
        !            49: 
        !            50: #ifdef TEST
        !            51: /*
        !            52:  * This test example shows how to use getopt in a program.
        !            53:  * Typical test lines are
        !            54:  * getopt -xyfxxx -f yyy a b c
        !            55:  * getopt -xj
        !            56:  */
        !            57: main(argc, argv)
        !            58: char *argv[];
        !            59: {
        !            60:        char c;
        !            61: 
        !            62:        while(EOF != (c = getopt(argc, argv, "xyf:")))
        !            63:                switch(c) {
        !            64:                case 'x':
        !            65:                        printf("option x\n");
        !            66:                        break;
        !            67:                case 'y':
        !            68:                        printf("option y\n");
        !            69:                        break;
        !            70:                case 'f':
        !            71:                        printf("option f with %s\n", optarg);
        !            72:                        break;
        !            73:                default:
        !            74:                        printf("usage: getopt [-xy] [-f filen]\n");
        !            75:                        exit(1);
        !            76:                }
        !            77:        for(;optind < argc; optind++)
        !            78:                printf("Trailing %s\n", argv[optind]);
        !            79:        exit(0);
        !            80: }
        !            81: #endif

unix.superglobalmegacorp.com

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