1*a466cc55SCy Schubert /* $NetBSD: getopt.c,v 1.16 1999/12/02 13:15:56 kleink Exp $ */
2*a466cc55SCy Schubert
3*a466cc55SCy Schubert /*
4*a466cc55SCy Schubert * Copyright (c) 1987, 1993, 1994, 1995
5*a466cc55SCy Schubert * The Regents of the University of California. All rights reserved.
6*a466cc55SCy Schubert *
7*a466cc55SCy Schubert * Redistribution and use in source and binary forms, with or without
8*a466cc55SCy Schubert * modification, are permitted provided that the following conditions
9*a466cc55SCy Schubert * are met:
10*a466cc55SCy Schubert * 1. Redistributions of source code must retain the above copyright
11*a466cc55SCy Schubert * notice, this list of conditions and the following disclaimer.
12*a466cc55SCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
13*a466cc55SCy Schubert * notice, this list of conditions and the following disclaimer in the
14*a466cc55SCy Schubert * documentation and/or other materials provided with the distribution.
15*a466cc55SCy Schubert * 3. Neither the names of the copyright holders nor the names of its
16*a466cc55SCy Schubert * contributors may be used to endorse or promote products derived from
17*a466cc55SCy Schubert * this software without specific prior written permission.
18*a466cc55SCy Schubert *
19*a466cc55SCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20*a466cc55SCy Schubert * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21*a466cc55SCy Schubert * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*a466cc55SCy Schubert * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23*a466cc55SCy Schubert * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*a466cc55SCy Schubert * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*a466cc55SCy Schubert * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*a466cc55SCy Schubert * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*a466cc55SCy Schubert * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*a466cc55SCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*a466cc55SCy Schubert * POSSIBILITY OF SUCH DAMAGE.
30*a466cc55SCy Schubert */
31*a466cc55SCy Schubert
32*a466cc55SCy Schubert #if 0
33*a466cc55SCy Schubert static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
34*a466cc55SCy Schubert #endif
35*a466cc55SCy Schubert
36*a466cc55SCy Schubert #include <assert.h>
37*a466cc55SCy Schubert #include <errno.h>
38*a466cc55SCy Schubert #include <stdio.h>
39*a466cc55SCy Schubert #include <string.h>
40*a466cc55SCy Schubert
41*a466cc55SCy Schubert #define __P(x) x
42*a466cc55SCy Schubert #define _DIAGASSERT(x) assert(x)
43*a466cc55SCy Schubert
44*a466cc55SCy Schubert #ifdef __weak_alias
45*a466cc55SCy Schubert __weak_alias(getopt,_getopt);
46*a466cc55SCy Schubert #endif
47*a466cc55SCy Schubert
48*a466cc55SCy Schubert
49*a466cc55SCy Schubert int opterr = 1, /* if error message should be printed */
50*a466cc55SCy Schubert optind = 1, /* index into parent argv vector */
51*a466cc55SCy Schubert optopt, /* character checked for validity */
52*a466cc55SCy Schubert optreset; /* reset getopt */
53*a466cc55SCy Schubert char *optarg; /* argument associated with option */
54*a466cc55SCy Schubert
55*a466cc55SCy Schubert static char * _progname __P((char *));
56*a466cc55SCy Schubert int getopt_internal __P((int, char * const *, const char *));
57*a466cc55SCy Schubert
58*a466cc55SCy Schubert static char *
_progname(nargv0)59*a466cc55SCy Schubert _progname(nargv0)
60*a466cc55SCy Schubert char * nargv0;
61*a466cc55SCy Schubert {
62*a466cc55SCy Schubert char * tmp;
63*a466cc55SCy Schubert
64*a466cc55SCy Schubert _DIAGASSERT(nargv0 != NULL);
65*a466cc55SCy Schubert
66*a466cc55SCy Schubert tmp = strrchr(nargv0, '/');
67*a466cc55SCy Schubert if (tmp)
68*a466cc55SCy Schubert tmp++;
69*a466cc55SCy Schubert else
70*a466cc55SCy Schubert tmp = nargv0;
71*a466cc55SCy Schubert return(tmp);
72*a466cc55SCy Schubert }
73*a466cc55SCy Schubert
74*a466cc55SCy Schubert #define BADCH (int)'?'
75*a466cc55SCy Schubert #define BADARG (int)':'
76*a466cc55SCy Schubert #define EMSG ""
77*a466cc55SCy Schubert
78*a466cc55SCy Schubert /*
79*a466cc55SCy Schubert * getopt --
80*a466cc55SCy Schubert * Parse argc/argv argument vector.
81*a466cc55SCy Schubert */
82*a466cc55SCy Schubert int
getopt(nargc,nargv,ostr)83*a466cc55SCy Schubert getopt(nargc, nargv, ostr)
84*a466cc55SCy Schubert int nargc;
85*a466cc55SCy Schubert char * const nargv[];
86*a466cc55SCy Schubert const char *ostr;
87*a466cc55SCy Schubert {
88*a466cc55SCy Schubert static char *__progname = 0;
89*a466cc55SCy Schubert static char *place = EMSG; /* option letter processing */
90*a466cc55SCy Schubert char *oli; /* option letter list index */
91*a466cc55SCy Schubert __progname = __progname?__progname:_progname(*nargv);
92*a466cc55SCy Schubert
93*a466cc55SCy Schubert _DIAGASSERT(nargv != NULL);
94*a466cc55SCy Schubert _DIAGASSERT(ostr != NULL);
95*a466cc55SCy Schubert
96*a466cc55SCy Schubert if (optreset || !*place) { /* update scanning pointer */
97*a466cc55SCy Schubert optreset = 0;
98*a466cc55SCy Schubert if (optind >= nargc || *(place = nargv[optind]) != '-') {
99*a466cc55SCy Schubert place = EMSG;
100*a466cc55SCy Schubert return (-1);
101*a466cc55SCy Schubert }
102*a466cc55SCy Schubert if (place[1] && *++place == '-' /* found "--" */
103*a466cc55SCy Schubert && place[1] == '\0') {
104*a466cc55SCy Schubert ++optind;
105*a466cc55SCy Schubert place = EMSG;
106*a466cc55SCy Schubert return (-1);
107*a466cc55SCy Schubert }
108*a466cc55SCy Schubert } /* option letter okay? */
109*a466cc55SCy Schubert if ((optopt = (int)*place++) == (int)':' ||
110*a466cc55SCy Schubert !(oli = strchr(ostr, optopt))) {
111*a466cc55SCy Schubert /*
112*a466cc55SCy Schubert * if the user didn't specify '-' as an option,
113*a466cc55SCy Schubert * assume it means -1.
114*a466cc55SCy Schubert */
115*a466cc55SCy Schubert if (optopt == (int)'-')
116*a466cc55SCy Schubert return (-1);
117*a466cc55SCy Schubert if (!*place)
118*a466cc55SCy Schubert ++optind;
119*a466cc55SCy Schubert if (opterr && *ostr != ':')
120*a466cc55SCy Schubert (void)fprintf(stderr,
121*a466cc55SCy Schubert "%s: illegal option -- %c\n", __progname, optopt);
122*a466cc55SCy Schubert return (BADCH);
123*a466cc55SCy Schubert }
124*a466cc55SCy Schubert if (*++oli != ':') { /* don't need argument */
125*a466cc55SCy Schubert optarg = NULL;
126*a466cc55SCy Schubert if (!*place)
127*a466cc55SCy Schubert ++optind;
128*a466cc55SCy Schubert }
129*a466cc55SCy Schubert else { /* need an argument */
130*a466cc55SCy Schubert if (*place) /* no white space */
131*a466cc55SCy Schubert optarg = place;
132*a466cc55SCy Schubert else if (nargc <= ++optind) { /* no arg */
133*a466cc55SCy Schubert place = EMSG;
134*a466cc55SCy Schubert if (*ostr == ':')
135*a466cc55SCy Schubert return (BADARG);
136*a466cc55SCy Schubert if (opterr)
137*a466cc55SCy Schubert (void)fprintf(stderr,
138*a466cc55SCy Schubert "%s: option requires an argument -- %c\n",
139*a466cc55SCy Schubert __progname, optopt);
140*a466cc55SCy Schubert return (BADCH);
141*a466cc55SCy Schubert }
142*a466cc55SCy Schubert else /* white space */
143*a466cc55SCy Schubert optarg = nargv[optind];
144*a466cc55SCy Schubert place = EMSG;
145*a466cc55SCy Schubert ++optind;
146*a466cc55SCy Schubert }
147*a466cc55SCy Schubert return (optopt); /* dump back option letter */
148*a466cc55SCy Schubert }
149*a466cc55SCy Schubert
150