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