7 #define _DIAGASSERT(x) assert(x)
10 __weak_alias(getopt,_getopt);
14 int opterr = 1, /* if error message should be printed */
15 optind = 1, /* index into parent argv vector */
16 optopt, /* character checked for validity */
17 optreset; /* reset getopt */
18 char *optarg; /* argument associated with option */
20 static char * _progname __P((char *));
21 int getopt_internal __P((int, char * const *, const char *));
29 _DIAGASSERT(nargv0 != NULL);
31 tmp = strrchr(nargv0, '/');
39 #define BADCH (int)'?'
40 #define BADARG (int)':'
45 * Parse argc/argv argument vector.
48 getopt(nargc, nargv, ostr)
53 static char *__progname = 0;
54 static char *place = EMSG; /* option letter processing */
55 char *oli; /* option letter list index */
56 __progname = __progname?__progname:_progname(*nargv);
58 _DIAGASSERT(nargv != NULL);
59 _DIAGASSERT(ostr != NULL);
61 if (optreset || !*place) { /* update scanning pointer */
63 if (optind >= nargc || *(place = nargv[optind]) != '-') {
67 if (place[1] && *++place == '-' /* found "--" */
68 && place[1] == '\0') {
73 } /* option letter okay? */
74 if ((optopt = (int)*place++) == (int)':' ||
75 !(oli = strchr(ostr, optopt))) {
77 * if the user didn't specify '-' as an option,
80 if (optopt == (int)'-')
84 if (opterr && *ostr != ':')
86 "%s: illegal option -- %c\n", __progname, optopt);
89 if (*++oli != ':') { /* don't need argument */
94 else { /* need an argument */
95 if (*place) /* no white space */
97 else if (nargc <= ++optind) { /* no arg */
102 (void)fprintf(stderr,
103 "%s: option requires an argument -- %c\n",
107 else /* white space */
108 optarg = nargv[optind];
112 return (optopt); /* dump back option letter */