123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #if 0
- static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
- #endif
- #include <assert.h>
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #define __P(x) x
- #define _DIAGASSERT(x) assert(x)
- #ifdef __weak_alias
- __weak_alias(getopt,_getopt);
- #endif
- int opterr = 1,
- optind = 1,
- optopt,
- optreset;
- char *optarg;
- static char * _progname __P((char *));
- int getopt_internal __P((int, char * const *, const char *));
- static char *
- _progname(nargv0)
- char * nargv0;
- {
- char * tmp;
- _DIAGASSERT(nargv0 != NULL);
- tmp = strrchr(nargv0, '/');
- if (tmp)
- tmp++;
- else
- tmp = nargv0;
- return(tmp);
- }
- #define BADCH (int)'?'
- #define BADARG (int)':'
- #define EMSG ""
- int
- getopt(nargc, nargv, ostr)
- int nargc;
- char * const nargv[];
- const char *ostr;
- {
- static char *__progname = 0;
- static char *place = EMSG;
- char *oli;
- __progname = __progname?__progname:_progname(*nargv);
- _DIAGASSERT(nargv != NULL);
- _DIAGASSERT(ostr != NULL);
- if (optreset || !*place) {
- optreset = 0;
- if (optind >= nargc || *(place = nargv[optind]) != '-') {
- place = EMSG;
- return (-1);
- }
- if (place[1] && *++place == '-'
- && place[1] == '\0') {
- ++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 != ':')
- (void)fprintf(stderr,
- "%s: illegal option -- %c\n", __progname, 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)
- (void)fprintf(stderr,
- "%s: option requires an argument -- %c\n",
- __progname, optopt);
- return (BADCH);
- }
- else
- optarg = nargv[optind];
- place = EMSG;
- ++optind;
- }
- return (optopt);
- }
|