Blame missing/getopt.c

Packit 209cc3
/*
Packit 209cc3
 * Copyright (c) 1987, 1993, 1994
Packit 209cc3
 *	The Regents of the University of California.  All rights reserved.
Packit 209cc3
 *
Packit 209cc3
 * Redistribution and use in source and binary forms, with or without
Packit 209cc3
 * modification, are permitted provided that the following conditions
Packit 209cc3
 * are met:
Packit 209cc3
 * 1. Redistributions of source code must retain the above copyright
Packit 209cc3
 *    notice, this list of conditions and the following disclaimer.
Packit 209cc3
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 209cc3
 *    notice, this list of conditions and the following disclaimer in the
Packit 209cc3
 *    documentation and/or other materials provided with the distribution.
Packit 209cc3
 * 3. All advertising materials mentioning features or use of this software
Packit 209cc3
 *    must display the following acknowledgement:
Packit 209cc3
 *	This product includes software developed by the University of
Packit 209cc3
 *	California, Berkeley and its contributors.
Packit 209cc3
 * 4. Neither the name of the University nor the names of its contributors
Packit 209cc3
 *    may be used to endorse or promote products derived from this software
Packit 209cc3
 *    without specific prior written permission.
Packit 209cc3
 *
Packit 209cc3
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 209cc3
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 209cc3
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 209cc3
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 209cc3
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 209cc3
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 209cc3
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 209cc3
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 209cc3
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 209cc3
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 209cc3
 * SUCH DAMAGE.
Packit 209cc3
 */
Packit 209cc3
Packit 209cc3
#if defined(LIBC_SCCS) && !defined(lint)
Packit 209cc3
static char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 4/27/95";
Packit 209cc3
#endif /* LIBC_SCCS and not lint */
Packit 209cc3
Packit 209cc3
#include <stdio.h>
Packit 209cc3
#include <stdlib.h>
Packit 209cc3
#include <string.h>
Packit 209cc3
Packit 209cc3
#include "getopt.h"
Packit 209cc3
Packit 209cc3
int	opterr = 1,		/* if error message should be printed */
Packit 209cc3
	optind = 1,		/* index into parent argv vector */
Packit 209cc3
	optopt,			/* character checked for validity */
Packit 209cc3
	optreset;		/* reset getopt */
Packit 209cc3
char	*optarg;		/* argument associated with option */
Packit 209cc3
Packit 209cc3
#define	BADCH	(int)'?'
Packit 209cc3
#define	BADARG	(int)':'
Packit 209cc3
#define	EMSG	""
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * getopt --
Packit 209cc3
 *	Parse argc/argv argument vector.
Packit 209cc3
 */
Packit 209cc3
int
Packit 209cc3
getopt(int nargc, char * const *nargv, const char *ostr)
Packit 209cc3
{
Packit 209cc3
	char *cp;
Packit 209cc3
	static char *__progname;
Packit 209cc3
	static char *place = EMSG;		/* option letter processing */
Packit 209cc3
	char *oli;				/* option letter list index */
Packit 209cc3
Packit 209cc3
	if (__progname == NULL) {
Packit 209cc3
		if ((cp = strrchr(nargv[0], '/')) != NULL)
Packit 209cc3
			__progname = cp + 1;
Packit 209cc3
		else
Packit 209cc3
			__progname = nargv[0];
Packit 209cc3
	}
Packit 209cc3
	if (optreset || !*place) {		/* update scanning pointer */
Packit 209cc3
		optreset = 0;
Packit 209cc3
		if (optind >= nargc || *(place = nargv[optind]) != '-') {
Packit 209cc3
			place = EMSG;
Packit 209cc3
			return (-1);
Packit 209cc3
		}
Packit 209cc3
		if (place[1] && *++place == '-') {	/* found "--" */
Packit 209cc3
			++optind;
Packit 209cc3
			place = EMSG;
Packit 209cc3
			return (-1);
Packit 209cc3
		}
Packit 209cc3
	}					/* option letter okay? */
Packit 209cc3
	if ((optopt = (int)*place++) == (int)':' ||
Packit 209cc3
	    !(oli = strchr(ostr, optopt))) {
Packit 209cc3
		/*
Packit 209cc3
		 * if the user didn't specify '-' as an option,
Packit 209cc3
		 * assume it means -1.
Packit 209cc3
		 */
Packit 209cc3
		if (optopt == (int)'-')
Packit 209cc3
			return (-1);
Packit 209cc3
		if (!*place)
Packit 209cc3
			++optind;
Packit 209cc3
		if (opterr && *ostr != ':')
Packit 209cc3
			(void)fprintf(stderr,
Packit 209cc3
			    "%s: illegal option -- %c\n", __progname, optopt);
Packit 209cc3
		return (BADCH);
Packit 209cc3
	}
Packit 209cc3
	if (*++oli != ':') {			/* don't need argument */
Packit 209cc3
		optarg = NULL;
Packit 209cc3
		if (!*place)
Packit 209cc3
			++optind;
Packit 209cc3
	}
Packit 209cc3
	else {					/* need an argument */
Packit 209cc3
		if (*place)			/* no white space */
Packit 209cc3
			optarg = place;
Packit 209cc3
		else if (nargc <= ++optind) {	/* no arg */
Packit 209cc3
			place = EMSG;
Packit 209cc3
			if (*ostr == ':')
Packit 209cc3
				return (BADARG);
Packit 209cc3
			if (opterr)
Packit 209cc3
				(void)fprintf(stderr,
Packit 209cc3
				    "%s: option requires an argument -- %c\n",
Packit 209cc3
				    __progname, optopt);
Packit 209cc3
			return (BADCH);
Packit 209cc3
		}
Packit 209cc3
	 	else				/* white space */
Packit 209cc3
			optarg = nargv[optind];
Packit 209cc3
		place = EMSG;
Packit 209cc3
		++optind;
Packit 209cc3
	}
Packit 209cc3
	return (optopt);			/* dump back option letter */
Packit 209cc3
}