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