1199767fToomas Soome/*
2199767fToomas Soome * Copyright (c) 1987, 1993, 1994
3199767fToomas Soome *	The Regents of the University of California.  All rights reserved.
4199767fToomas Soome *
5199767fToomas Soome * Redistribution and use in source and binary forms, with or without
6199767fToomas Soome * modification, are permitted provided that the following conditions
7199767fToomas Soome * are met:
8199767fToomas Soome * 1. Redistributions of source code must retain the above copyright
9199767fToomas Soome *    notice, this list of conditions and the following disclaimer.
10199767fToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
11199767fToomas Soome *    notice, this list of conditions and the following disclaimer in the
12199767fToomas Soome *    documentation and/or other materials provided with the distribution.
131974da4Toomas Soome * 3. Neither the name of the University nor the names of its contributors
14199767fToomas Soome *    may be used to endorse or promote products derived from this software
15199767fToomas Soome *    without specific prior written permission.
16199767fToomas Soome *
17199767fToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18199767fToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19199767fToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20199767fToomas Soome * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21199767fToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22199767fToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23199767fToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24199767fToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25199767fToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26199767fToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27199767fToomas Soome * SUCH DAMAGE.
28199767fToomas Soome */
29199767fToomas Soome
30199767fToomas Soome#include <sys/cdefs.h>
31199767fToomas Soome__FBSDID("$FreeBSD$");
32199767fToomas Soome
33199767fToomas Soome#if defined(LIBC_SCCS) && !defined(lint)
34199767fToomas Soomestatic char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 4/27/95";
35199767fToomas Soome#endif /* LIBC_SCCS and not lint */
36199767fToomas Soome
37199767fToomas Soome#include "stand.h"
38199767fToomas Soome#include <string.h>
39199767fToomas Soome
40199767fToomas Soomeint	opterr = 1,		/* if error message should be printed */
41199767fToomas Soome	optind = 1,		/* index into parent argv vector */
42199767fToomas Soome	optopt,			/* character checked for validity */
43199767fToomas Soome	optreset;		/* reset getopt */
44199767fToomas Soomechar	*optarg;		/* argument associated with option */
45199767fToomas Soome
46199767fToomas Soome#define	BADCH	(int)'?'
47199767fToomas Soome#define	BADARG	(int)':'
48199767fToomas Soome#define	EMSG	""
49199767fToomas Soome
50199767fToomas Soome/*
51199767fToomas Soome * getopt --
52199767fToomas Soome *	Parse argc/argv argument vector.
53199767fToomas Soome */
54199767fToomas Soomeint
55199767fToomas Soomegetopt(int nargc, char * const *nargv, const char *ostr)
56199767fToomas Soome{
57199767fToomas Soome	static char *place = EMSG;		/* option letter processing */
58199767fToomas Soome	char *oli;				/* option letter list index */
59199767fToomas Soome
60199767fToomas Soome	if (optreset || !*place) {		/* update scanning pointer */
61199767fToomas Soome		optreset = 0;
62199767fToomas Soome		if (optind >= nargc || *(place = nargv[optind]) != '-') {
63199767fToomas Soome			place = EMSG;
64199767fToomas Soome			return (-1);
65199767fToomas Soome		}
66199767fToomas Soome		if (place[1] && *++place == '-') {	/* found "--" */
67199767fToomas Soome			++optind;
68199767fToomas Soome			place = EMSG;
69199767fToomas Soome			return (-1);
70199767fToomas Soome		}
71199767fToomas Soome	}					/* option letter okay? */
72199767fToomas Soome	if ((optopt = (int)*place++) == (int)':' ||
73199767fToomas Soome	    !(oli = strchr(ostr, optopt))) {
74199767fToomas Soome		/*
75199767fToomas Soome		 * if the user didn't specify '-' as an option,
76199767fToomas Soome		 * assume it means -1.
77199767fToomas Soome		 */
78199767fToomas Soome		if (optopt == (int)'-')
79199767fToomas Soome			return (-1);
80199767fToomas Soome		if (!*place)
81199767fToomas Soome			++optind;
82199767fToomas Soome		if (opterr && *ostr != ':')
83199767fToomas Soome			(void)printf("illegal option -- %c\n", optopt);
84199767fToomas Soome		return (BADCH);
85199767fToomas Soome	}
86199767fToomas Soome	if (*++oli != ':') {			/* don't need argument */
87199767fToomas Soome		optarg = NULL;
88199767fToomas Soome		if (!*place)
89199767fToomas Soome			++optind;
90199767fToomas Soome	}
91199767fToomas Soome	else {					/* need an argument */
92199767fToomas Soome		if (*place)			/* no white space */
93199767fToomas Soome			optarg = place;
94199767fToomas Soome		else if (nargc <= ++optind) {	/* no arg */
95199767fToomas Soome			place = EMSG;
96199767fToomas Soome			if (*ostr == ':')
97199767fToomas Soome				return (BADARG);
98199767fToomas Soome			if (opterr)
99199767fToomas Soome				(void)printf("option requires an argument -- %c\n", optopt);
100199767fToomas Soome			return (BADCH);
101199767fToomas Soome		}
102199767fToomas Soome	 	else				/* white space */
103199767fToomas Soome			optarg = nargv[optind];
104199767fToomas Soome		place = EMSG;
105199767fToomas Soome		++optind;
106199767fToomas Soome	}
107199767fToomas Soome	return (optopt);			/* dump back option letter */
108199767fToomas Soome}
109