Annotation of 43BSDReno/lib/libc/gen/getopt.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1987 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that: (1) source distributions retain this entire copyright
        !             7:  * notice and comment, and (2) distributions including binaries display
        !             8:  * the following acknowledgement:  ``This product includes software
        !             9:  * developed by the University of California, Berkeley and its contributors''
        !            10:  * in the documentation or other materials provided with the distribution
        !            11:  * and in all advertising materials mentioning features or use of this
        !            12:  * software. Neither the name of the University nor the names of its
        !            13:  * contributors may be used to endorse or promote products derived
        !            14:  * from this software without specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #if defined(LIBC_SCCS) && !defined(lint)
        !            21: static char sccsid[] = "@(#)getopt.c   4.12 (Berkeley) 6/1/90";
        !            22: #endif /* LIBC_SCCS and not lint */
        !            23: 
        !            24: #include <stdio.h>
        !            25: 
        !            26: /*
        !            27:  * get option letter from argument vector
        !            28:  */
        !            29: int    opterr = 1,             /* if error message should be printed */
        !            30:        optind = 1,             /* index into parent argv vector */
        !            31:        optopt;                 /* character checked for validity */
        !            32: char   *optarg;                /* argument associated with option */
        !            33: 
        !            34: #define        BADCH   (int)'?'
        !            35: #define        EMSG    ""
        !            36: 
        !            37: getopt(nargc, nargv, ostr)
        !            38:        int nargc;
        !            39:        char **nargv, *ostr;
        !            40: {
        !            41:        static char *place = EMSG;              /* option letter processing */
        !            42:        register char *oli;                     /* option letter list index */
        !            43:        char *p, *index(), *rindex();
        !            44: 
        !            45:        if (!*place) {                          /* update scanning pointer */
        !            46:                if (optind >= nargc || *(place = nargv[optind]) != '-') {
        !            47:                        place = EMSG;
        !            48:                        return(EOF);
        !            49:                }
        !            50:                if (place[1] && *++place == '-') {      /* found "--" */
        !            51:                        ++optind;
        !            52:                        place = EMSG;
        !            53:                        return(EOF);
        !            54:                }
        !            55:        }                                       /* option letter okay? */
        !            56:        if ((optopt = (int)*place++) == (int)':' ||
        !            57:            !(oli = index(ostr, optopt))) {
        !            58:                /*
        !            59:                 * if the user didn't specify '-' as an option,
        !            60:                 * assume it means EOF.
        !            61:                 */
        !            62:                if (optopt == (int)'-')
        !            63:                        return(EOF);
        !            64:                if (!*place)
        !            65:                        ++optind;
        !            66:                if (opterr) {
        !            67:                        if (!(p = rindex(*nargv, '/')))
        !            68:                                p = *nargv;
        !            69:                        else
        !            70:                                ++p;
        !            71:                        (void)fprintf(stderr, "%s: illegal option -- %c\n",
        !            72:                            p, optopt);
        !            73:                }
        !            74:                return(BADCH);
        !            75:        }
        !            76:        if (*++oli != ':') {                    /* don't need argument */
        !            77:                optarg = NULL;
        !            78:                if (!*place)
        !            79:                        ++optind;
        !            80:        }
        !            81:        else {                                  /* need an argument */
        !            82:                if (*place)                     /* no white space */
        !            83:                        optarg = place;
        !            84:                else if (nargc <= ++optind) {   /* no arg */
        !            85:                        place = EMSG;
        !            86:                        if (!(p = rindex(*nargv, '/')))
        !            87:                                p = *nargv;
        !            88:                        else
        !            89:                                ++p;
        !            90:                        if (opterr)
        !            91:                                (void)fprintf(stderr,
        !            92:                                    "%s: option requires an argument -- %c\n",
        !            93:                                    p, optopt);
        !            94:                        return(BADCH);
        !            95:                }
        !            96:                else                            /* white space */
        !            97:                        optarg = nargv[optind];
        !            98:                place = EMSG;
        !            99:                ++optind;
        !           100:        }
        !           101:        return(optopt);                         /* dump back option letter */
        !           102: }

unix.superglobalmegacorp.com

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