1 /* $NetBSD: getopt.c,v 1.16 1999/12/02 13:15:56 kleink Exp $ */
4 * Copyright (c) 1987, 1993, 1994, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the names of the copyright holders nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
42 #define _DIAGASSERT(x) assert(x)
45 __weak_alias(getopt,_getopt);
49 int opterr = 1, /* if error message should be printed */
50 optind = 1, /* index into parent argv vector */
51 optopt, /* character checked for validity */
52 optreset; /* reset getopt */
53 char *optarg; /* argument associated with option */
55 static char * _progname __P((char *));
56 int getopt_internal __P((int, char * const *, const char *));
64 _DIAGASSERT(nargv0 != NULL);
66 tmp = strrchr(nargv0, '/');
74 #define BADCH (int)'?'
75 #define BADARG (int)':'
80 * Parse argc/argv argument vector.
83 getopt(nargc, nargv, ostr)
88 static char *__progname = 0;
89 static char *place = EMSG; /* option letter processing */
90 char *oli; /* option letter list index */
91 __progname = __progname?__progname:_progname(*nargv);
93 _DIAGASSERT(nargv != NULL);
94 _DIAGASSERT(ostr != NULL);
96 if (optreset || !*place) { /* update scanning pointer */
98 if (optind >= nargc || *(place = nargv[optind]) != '-') {
102 if (place[1] && *++place == '-' /* found "--" */
103 && place[1] == '\0') {
108 } /* option letter okay? */
109 if ((optopt = (int)*place++) == (int)':' ||
110 !(oli = strchr(ostr, optopt))) {
112 * if the user didn't specify '-' as an option,
113 * assume it means -1.
115 if (optopt == (int)'-')
119 if (opterr && *ostr != ':')
120 (void)fprintf(stderr,
121 "%s: illegal option -- %c\n", __progname, optopt);
124 if (*++oli != ':') { /* don't need argument */
129 else { /* need an argument */
130 if (*place) /* no white space */
132 else if (nargc <= ++optind) { /* no arg */
137 (void)fprintf(stderr,
138 "%s: option requires an argument -- %c\n",
142 else /* white space */
143 optarg = nargv[optind];
147 return (optopt); /* dump back option letter */