138451Smsmith/*
238451Smsmith * Copyright (c) 1987, 1993, 1994
338451Smsmith *	The Regents of the University of California.  All rights reserved.
438451Smsmith *
538451Smsmith * Redistribution and use in source and binary forms, with or without
638451Smsmith * modification, are permitted provided that the following conditions
738451Smsmith * are met:
838451Smsmith * 1. Redistributions of source code must retain the above copyright
938451Smsmith *    notice, this list of conditions and the following disclaimer.
1038451Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1138451Smsmith *    notice, this list of conditions and the following disclaimer in the
1238451Smsmith *    documentation and/or other materials provided with the distribution.
1338451Smsmith * 4. Neither the name of the University nor the names of its contributors
1438451Smsmith *    may be used to endorse or promote products derived from this software
1538451Smsmith *    without specific prior written permission.
1638451Smsmith *
1738451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1838451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1938451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2038451Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2138451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2238451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2338451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2438451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2538451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2638451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2738451Smsmith * SUCH DAMAGE.
2838451Smsmith */
2938451Smsmith
3084221Sdillon#include <sys/cdefs.h>
3184221Sdillon__FBSDID("$FreeBSD$");
3284221Sdillon
3338451Smsmith#if defined(LIBC_SCCS) && !defined(lint)
3438451Smsmithstatic char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 4/27/95";
3538451Smsmith#endif /* LIBC_SCCS and not lint */
3638451Smsmith
3738451Smsmith#include "stand.h"
3838451Smsmith#include <string.h>
3938451Smsmith
4038451Smsmithint	opterr = 1,		/* if error message should be printed */
4138451Smsmith	optind = 1,		/* index into parent argv vector */
4238451Smsmith	optopt,			/* character checked for validity */
4338451Smsmith	optreset;		/* reset getopt */
4438451Smsmithchar	*optarg;		/* argument associated with option */
4538451Smsmith
4638451Smsmith#define	BADCH	(int)'?'
4738451Smsmith#define	BADARG	(int)':'
4838451Smsmith#define	EMSG	""
4938451Smsmith
5038451Smsmith/*
5138451Smsmith * getopt --
5238451Smsmith *	Parse argc/argv argument vector.
5338451Smsmith */
5438451Smsmithint
55221358Srodrigcgetopt(int nargc, char * const *nargv, const char *ostr)
5638451Smsmith{
5738451Smsmith	static char *place = EMSG;		/* option letter processing */
5838451Smsmith	char *oli;				/* option letter list index */
5938451Smsmith
6038451Smsmith	if (optreset || !*place) {		/* update scanning pointer */
6138451Smsmith		optreset = 0;
6238451Smsmith		if (optind >= nargc || *(place = nargv[optind]) != '-') {
6338451Smsmith			place = EMSG;
6438451Smsmith			return (-1);
6538451Smsmith		}
6638451Smsmith		if (place[1] && *++place == '-') {	/* found "--" */
6738451Smsmith			++optind;
6838451Smsmith			place = EMSG;
6938451Smsmith			return (-1);
7038451Smsmith		}
7138451Smsmith	}					/* option letter okay? */
7238451Smsmith	if ((optopt = (int)*place++) == (int)':' ||
7338451Smsmith	    !(oli = strchr(ostr, optopt))) {
7438451Smsmith		/*
7538451Smsmith		 * if the user didn't specify '-' as an option,
7638451Smsmith		 * assume it means -1.
7738451Smsmith		 */
7838451Smsmith		if (optopt == (int)'-')
7938451Smsmith			return (-1);
8038451Smsmith		if (!*place)
8138451Smsmith			++optind;
8238451Smsmith		if (opterr && *ostr != ':')
8338451Smsmith			(void)printf("illegal option -- %c\n", optopt);
8438451Smsmith		return (BADCH);
8538451Smsmith	}
8638451Smsmith	if (*++oli != ':') {			/* don't need argument */
8738451Smsmith		optarg = NULL;
8838451Smsmith		if (!*place)
8938451Smsmith			++optind;
9038451Smsmith	}
9138451Smsmith	else {					/* need an argument */
9238451Smsmith		if (*place)			/* no white space */
9338451Smsmith			optarg = place;
9438451Smsmith		else if (nargc <= ++optind) {	/* no arg */
9538451Smsmith			place = EMSG;
9638451Smsmith			if (*ostr == ':')
9738451Smsmith				return (BADARG);
9838451Smsmith			if (opterr)
9938451Smsmith				(void)printf("option requires an argument -- %c\n", optopt);
10038451Smsmith			return (BADCH);
10138451Smsmith		}
10238451Smsmith	 	else				/* white space */
10338451Smsmith			optarg = nargv[optind];
10438451Smsmith		place = EMSG;
10538451Smsmith		++optind;
10638451Smsmith	}
10738451Smsmith	return (optopt);			/* dump back option letter */
10838451Smsmith}
109