Annotation of mstools/posix/samples/psxarc/getopt.c, revision 1.1

1.1     ! root        1: /* got this off net.sources */
        !             2: #include <stdio.h>
        !             3: #include <string.h>
        !             4: #include "getopt.h"
        !             5: 
        !             6: /*
        !             7:  * get option letter from argument vector
        !             8:  */
        !             9: int
        !            10:        opterr = 1,             // should error messages be printed?
        !            11:        optind = 1,             // index into parent argv vector
        !            12:        optopt;                 // character checked for validity
        !            13: char
        !            14:        *optarg;                // argument associated with option
        !            15: 
        !            16: #define EMSG   ""
        !            17: 
        !            18: char *progname;                        // may also be defined elsewhere
        !            19: 
        !            20: static void
        !            21: error(char *pch)
        !            22: {
        !            23:        if (!opterr) {
        !            24:                return;         // without printing
        !            25:        }
        !            26:        fprintf(stderr, "%s: %s: %c\n",
        !            27:                (NULL != progname) ? progname : "getopt", pch, optopt);
        !            28: }
        !            29: 
        !            30: int
        !            31: getopt(int argc, char **argv, char *ostr)
        !            32: {
        !            33:        static char *place = EMSG;      /* option letter processing */
        !            34:        register char *oli;                     /* option letter list index */
        !            35: 
        !            36:        if (!*place) {
        !            37:                // update scanning pointer
        !            38:                if (optind >= argc || *(place = argv[optind]) != '-' || !*++place) {
        !            39:                        return EOF; 
        !            40:                }
        !            41:                if (*place == '-') {
        !            42:                        // found "--"
        !            43:                        ++optind;
        !            44:                        return EOF;
        !            45:                }
        !            46:        }
        !            47: 
        !            48:        /* option letter okay? */
        !            49:        if ((optopt = (int)*place++) == (int)':'
        !            50:                || !(oli = strchr(ostr, optopt))) {
        !            51:                if (!*place) {
        !            52:                        ++optind;
        !            53:                }
        !            54:                error("illegal option");
        !            55:                return BADCH;
        !            56:        }
        !            57:        if (*++oli != ':') {    
        !            58:                /* don't need argument */
        !            59:                optarg = NULL;
        !            60:                if (!*place)
        !            61:                        ++optind;
        !            62:        } else {
        !            63:                /* need an argument */
        !            64:                if (*place) {
        !            65:                        optarg = place;         /* no white space */
        !            66:                } else  if (argc <= ++optind) {
        !            67:                        /* no arg */
        !            68:                        place = EMSG;
        !            69:                        error("option requires an argument");
        !            70:                        return BADCH;
        !            71:                } else {
        !            72:                        optarg = argv[optind];          /* white space */
        !            73:                }
        !            74:                place = EMSG;
        !            75:                ++optind;
        !            76:        }
        !            77:        return optopt;                  // return option letter
        !            78: }

unix.superglobalmegacorp.com

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