155682Smarkm/*
255682Smarkm * Copyright (c) 1987, 1993
355682Smarkm *	The Regents of the University of California.  All rights reserved.
455682Smarkm *
555682Smarkm * Redistribution and use in source and binary forms, with or without
655682Smarkm * modification, are permitted provided that the following conditions
755682Smarkm * are met:
855682Smarkm * 1. Redistributions of source code must retain the above copyright
955682Smarkm *    notice, this list of conditions and the following disclaimer.
1055682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer in the
1255682Smarkm *    documentation and/or other materials provided with the distribution.
13178825Sdfr * 3. Neither the name of the University nor the names of its contributors
1455682Smarkm *    may be used to endorse or promote products derived from this software
1555682Smarkm *    without specific prior written permission.
1655682Smarkm *
1755682Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1855682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1955682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2055682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2155682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2255682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2355682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2455682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2555682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2655682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2755682Smarkm * SUCH DAMAGE.
2855682Smarkm */
2955682Smarkm
3055682Smarkm#if defined(LIBC_SCCS) && !defined(lint)
3155682Smarkmstatic char sccsid[] = "@(#)getopt.c	8.1 (Berkeley) 6/4/93";
3255682Smarkm#endif /* LIBC_SCCS and not lint */
3355682Smarkm
3455682Smarkm#ifndef __STDC__
3555682Smarkm#define const
3655682Smarkm#endif
37233294Sstas#include <config.h>
38233294Sstas#include "roken.h"
39233294Sstas
4055682Smarkm#include <stdio.h>
4155682Smarkm#include <stdlib.h>
4255682Smarkm#include <string.h>
4355682Smarkm
4455682Smarkm/*
4555682Smarkm * get option letter from argument vector
4655682Smarkm */
47233294SstasROKEN_LIB_VARIABLE int opterr = 1; /* if error message should be printed */
48233294SstasROKEN_LIB_VARIABLE int optind = 1; /* index into parent argv vector */
49233294SstasROKEN_LIB_VARIABLE int optopt;   /* character checked for validity */
50233294Sstasint	optreset;                /* reset getopt */
51233294SstasROKEN_LIB_VARIABLE char	*optarg; /* argument associated with option */
5255682Smarkm
5355682Smarkm#define	BADCH	(int)'?'
5455682Smarkm#define	BADARG	(int)':'
5555682Smarkm#define	EMSG	""
5655682Smarkm
57233294SstasROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
58233294Sstasgetopt(int nargc, char * const *nargv, const char *ostr)
5955682Smarkm{
6055682Smarkm	static char *place = EMSG;		/* option letter processing */
6155682Smarkm	char *oli;			/* option letter list index */
6255682Smarkm	char *p;
6355682Smarkm
6455682Smarkm	if (optreset || !*place) {		/* update scanning pointer */
6555682Smarkm		optreset = 0;
6655682Smarkm		if (optind >= nargc || *(place = nargv[optind]) != '-') {
6755682Smarkm			place = EMSG;
6855682Smarkm			return(-1);
6955682Smarkm		}
7055682Smarkm		if (place[1] && *++place == '-') {	/* found "--" */
7155682Smarkm			++optind;
7255682Smarkm			place = EMSG;
7355682Smarkm			return(-1);
7455682Smarkm		}
7555682Smarkm	}					/* option letter okay? */
7655682Smarkm	if ((optopt = (int)*place++) == (int)':' ||
7755682Smarkm	    !(oli = strchr(ostr, optopt))) {
7855682Smarkm		/*
7955682Smarkm		 * if the user didn't specify '-' as an option,
8055682Smarkm		 * assume it means -1 (EOF).
8155682Smarkm		 */
8255682Smarkm		if (optopt == (int)'-')
8355682Smarkm			return(-1);
8455682Smarkm		if (!*place)
8555682Smarkm			++optind;
8655682Smarkm		if (opterr && *ostr != ':') {
8755682Smarkm			if (!(p = strrchr(*nargv, '/')))
8855682Smarkm				p = *nargv;
8955682Smarkm			else
9055682Smarkm				++p;
9155682Smarkm			fprintf(stderr, "%s: illegal option -- %c\n",
9255682Smarkm			    p, optopt);
9355682Smarkm		}
9455682Smarkm		return(BADCH);
9555682Smarkm	}
9655682Smarkm	if (*++oli != ':') {			/* don't need argument */
9755682Smarkm		optarg = NULL;
9855682Smarkm		if (!*place)
9955682Smarkm			++optind;
10055682Smarkm	}
10155682Smarkm	else {					/* need an argument */
10255682Smarkm		if (*place)			/* no white space */
10355682Smarkm			optarg = place;
10455682Smarkm		else if (nargc <= ++optind) {	/* no arg */
10555682Smarkm			place = EMSG;
10655682Smarkm			if (!(p = strrchr(*nargv, '/')))
10755682Smarkm				p = *nargv;
10855682Smarkm			else
10955682Smarkm				++p;
11055682Smarkm			if (*ostr == ':')
11155682Smarkm				return(BADARG);
11255682Smarkm			if (opterr)
11355682Smarkm				fprintf(stderr,
11455682Smarkm				    "%s: option requires an argument -- %c\n",
11555682Smarkm				    p, optopt);
11655682Smarkm			return(BADCH);
11755682Smarkm		}
11855682Smarkm	 	else				/* white space */
11955682Smarkm			optarg = nargv[optind];
12055682Smarkm		place = EMSG;
12155682Smarkm		++optind;
12255682Smarkm	}
12355682Smarkm	return(optopt);				/* dump back option letter */
12455682Smarkm}
125