123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "getopt.h"
- int opterr = 1;
- int optind = 1;
- int optopt;
- int optreset;
- char *optarg;
- #define BADCH (int)'?'
- #define BADARG (int)':'
- #define EMSG ""
- int
- getopt(
- int nargc,
- char * const *nargv,
- const char *ostr
- )
- {
- static char *place = EMSG;
- char *oli;
- if (optreset || !*place)
- {
-
- optreset = 0;
- if (optind >= nargc || *(place = nargv[optind]) != '-')
- {
- place = EMSG;
- return -1;
- }
- if (place[1] && *++place == '-')
- {
-
- ++optind;
- place = EMSG;
- return -1;
- }
- }
-
- if ((optopt = (int)*place++) == (int)':' ||
- !(oli = strchr(ostr, optopt)))
- {
-
-
-
-
- if (optopt == (int)'-')
- return -1;
- if (!*place)
- ++optind;
- if (opterr && *ostr != ':')
- fprintf(stderr, "%s: illegal option -- %c\n", nargv[0], optopt);
- return BADCH;
- }
- if (*++oli != ':')
- {
-
- optarg = NULL;
- if (!*place)
- ++optind;
- }
- else
- {
-
- if (*place)
- optarg = place;
- else if (nargc <= ++optind)
- {
-
- place = EMSG;
- if (*ostr == ':')
- return BADARG;
- if (opterr)
- {
- fprintf(stderr, "%s: option requires an argument -- %c\n",
- nargv[0], optopt);
- }
- return BADCH;
- }
- else
- optarg = nargv[optind];
- place = EMSG;
- ++optind;
- }
- return optopt;
- }
|