getopt.c revision 81746
144764Swpaul/*
244764Swpaul * Copyright (c) 1987, 1993, 1994
344764Swpaul *	The Regents of the University of California.  All rights reserved.
444764Swpaul *
544764Swpaul * Redistribution and use in source and binary forms, with or without
644764Swpaul * modification, are permitted provided that the following conditions
744764Swpaul * are met:
844764Swpaul * 1. Redistributions of source code must retain the above copyright
944764Swpaul *    notice, this list of conditions and the following disclaimer.
1044764Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1144764Swpaul *    notice, this list of conditions and the following disclaimer in the
1244764Swpaul *    documentation and/or other materials provided with the distribution.
1344764Swpaul * 3. All advertising materials mentioning features or use of this software
1444764Swpaul *    must display the following acknowledgement:
1544764Swpaul *	This product includes software developed by the University of
1644764Swpaul *	California, Berkeley and its contributors.
1744764Swpaul * 4. Neither the name of the University nor the names of its contributors
1844764Swpaul *    may be used to endorse or promote products derived from this software
1944764Swpaul *    without specific prior written permission.
2044764Swpaul *
2144764Swpaul * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2244764Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2344764Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2444764Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2544764Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2644764Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2744764Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2844764Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2944764Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3044764Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3144764Swpaul * SUCH DAMAGE.
3244764Swpaul */
3344764Swpaul
3444764Swpaul#if defined(LIBC_SCCS) && !defined(lint)
3544764Swpaul#if 0
3644764Swpaulstatic char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 4/27/95";
3744764Swpaul#endif
3844764Swpaulstatic const char rcsid[] = "$FreeBSD: head/lib/libc/stdlib/getopt.c 81746 2001-08-16 03:27:03Z jkoshy $";
3944764Swpaul#endif /* LIBC_SCCS and not lint */
4044764Swpaul
4144764Swpaul#include <stdio.h>
4244764Swpaul#include <stdlib.h>
4344764Swpaul#include <string.h>
4444764Swpaul
4544764Swpaulint	opterr = 1,		/* if error message should be printed */
4644764Swpaul	optind = 1,		/* index into parent argv vector */
4744764Swpaul	optopt,			/* character checked for validity */
4844764Swpaul	optreset;		/* reset getopt */
4944764Swpaulchar	*optarg;		/* argument associated with option */
5044764Swpaul
5144764Swpaul#define	BADCH	(int)'?'
5244764Swpaul#define	BADARG	(int)':'
5344764Swpaul#define	EMSG	""
5444764Swpaul
5544764Swpaul/*
5644764Swpaul * getopt --
5744764Swpaul *	Parse argc/argv argument vector.
5844764Swpaul */
5950476Speterint
6044764Swpaulgetopt(nargc, nargv, ostr)
6144764Swpaul	int nargc;
62160196Ssam	char * const *nargv;
63156498Syar	const char *ostr;
64160196Ssam{
65160196Ssam	extern char *__progname;
66160196Ssam	static char *place = EMSG;		/* option letter processing */
67160196Ssam	char *oli;				/* option letter list index */
68160196Ssam
69160196Ssam	if (optreset || !*place) {		/* update scanning pointer */
70160196Ssam		optreset = 0;
71160196Ssam		if (optind >= nargc || *(place = nargv[optind]) != '-') {
72160196Ssam			place = EMSG;
73160196Ssam			return (-1);
74160196Ssam		}
75160196Ssam		if (place[1] && *++place == '-') {	/* found "--" */
76160196Ssam			++optind;
77138593Ssam			place = EMSG;
78139494Ssam			return (-1);
7944764Swpaul		}
8044764Swpaul	}					/* option letter okay? */
8144764Swpaul	if ((optopt = (int)*place++) == (int)':' ||
82160196Ssam	    !(oli = strchr(ostr, optopt))) {
83160196Ssam		/*
84160196Ssam		 * if the user didn't specify '-' as an option,
85160196Ssam		 * assume it means -1.
86160196Ssam		 */
8744764Swpaul		if (optopt == (int)'-')
88160196Ssam			return (-1);
89160196Ssam		if (!*place)
90160196Ssam			++optind;
91160196Ssam		if (opterr && *ostr != ':' && optopt != BADCH)
92160196Ssam			(void)fprintf(stderr,
93160196Ssam			    "%s: illegal option -- %c\n", __progname, optopt);
94160196Ssam		return (BADCH);
95160196Ssam	}
96160196Ssam	if (*++oli != ':') {			/* don't need argument */
97160196Ssam		optarg = NULL;
98160196Ssam		if (!*place)
99160196Ssam			++optind;
100160196Ssam	}
101160196Ssam	else {					/* need an argument */
102160196Ssam		if (*place)			/* no white space */
103160196Ssam			optarg = place;
10444764Swpaul		else if (nargc <= ++optind) {	/* no arg */
105160196Ssam			place = EMSG;
106160196Ssam			if (*ostr == ':')
107160196Ssam				return (BADARG);
108160196Ssam			if (opterr)
109160196Ssam				(void)fprintf(stderr,
11044764Swpaul				    "%s: option requires an argument -- %c\n",
11144764Swpaul				    __progname, optopt);
112138593Ssam			return (BADCH);
113160196Ssam		}
11444764Swpaul	 	else				/* white space */
115160196Ssam			optarg = nargv[optind];
116160196Ssam		place = EMSG;
117160196Ssam		++optind;
118160196Ssam	}
119160196Ssam	return (optopt);			/* dump back option letter */
120160196Ssam}
12144764Swpaul