Blame port/getopt.c

Packit 994f1a
/* $Id: getopt.c,v 1.2.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * Copyright (c) 1987, 1993, 1994
Packit 994f1a
 *	The Regents of the University of California.  All rights reserved.
Packit 994f1a
 *
Packit 994f1a
 * Redistribution and use in source and binary forms, with or without
Packit 994f1a
 * modification, are permitted provided that the following conditions
Packit 994f1a
 * are met:
Packit 994f1a
 * 1. Redistributions of source code must retain the above copyright
Packit 994f1a
 *    notice, this list of conditions and the following disclaimer.
Packit 994f1a
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 994f1a
 *    notice, this list of conditions and the following disclaimer in the
Packit 994f1a
 *    documentation and/or other materials provided with the distribution.
Packit 994f1a
 * 3. Neither the name of the University nor the names of its contributors
Packit 994f1a
 *    may be used to endorse or promote products derived from this software
Packit 994f1a
 *    without specific prior written permission.
Packit 994f1a
 *
Packit 994f1a
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 994f1a
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 994f1a
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 994f1a
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 994f1a
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 994f1a
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 994f1a
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 994f1a
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 994f1a
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 994f1a
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 994f1a
 * SUCH DAMAGE.
Packit 994f1a
 */
Packit 994f1a
Packit 994f1a
#if 0
Packit 994f1a
static char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 4/27/95";
Packit 994f1a
__RCSID("$NetBSD: getopt.c,v 1.26 2003/08/07 16:43:40 agc Exp $");
Packit 994f1a
#endif
Packit 994f1a
Packit 994f1a
#include <stdio.h>
Packit 994f1a
#include <string.h>
Packit 994f1a
Packit 994f1a
int	opterr = 1,		/* if error message should be printed */
Packit 994f1a
	optind = 1,		/* index into parent argv vector */
Packit 994f1a
	optopt,			/* character checked for validity */
Packit 994f1a
	optreset;		/* reset getopt */
Packit 994f1a
char	*optarg;		/* argument associated with option */
Packit 994f1a
Packit 994f1a
#define	BADCH	(int)'?'
Packit 994f1a
#define	BADARG	(int)':'
Packit 994f1a
#define	EMSG	""
Packit 994f1a
Packit 994f1a
/*
Packit 994f1a
 * getopt --
Packit 994f1a
 *	Parse argc/argv argument vector.
Packit 994f1a
 */
Packit 994f1a
int
Packit 994f1a
getopt(int argc, char * const argv[], const char *optstring)
Packit 994f1a
{
Packit 994f1a
	static char *place = EMSG;		/* option letter processing */
Packit 994f1a
	char *oli;				/* option letter list index */
Packit 994f1a
Packit 994f1a
	if (optreset || *place == 0) {		/* update scanning pointer */
Packit 994f1a
		optreset = 0;
Packit 994f1a
		place = argv[optind];
Packit 994f1a
		if (optind >= argc || *place++ != '-') {
Packit 994f1a
			/* Argument is absent or is not an option */
Packit 994f1a
			place = EMSG;
Packit 994f1a
			return (-1);
Packit 994f1a
		}
Packit 994f1a
		optopt = *place++;
Packit 994f1a
		if (optopt == '-' && *place == 0) {
Packit 994f1a
			/* "--" => end of options */
Packit 994f1a
			++optind;
Packit 994f1a
			place = EMSG;
Packit 994f1a
			return (-1);
Packit 994f1a
		}
Packit 994f1a
		if (optopt == 0) {
Packit 994f1a
			/* Solitary '-', treat as a '-' option
Packit 994f1a
			   if the program (eg su) is looking for it. */
Packit 994f1a
			place = EMSG;
Packit 994f1a
			if (strchr(optstring, '-') == NULL)
Packit 994f1a
				return -1;
Packit 994f1a
			optopt = '-';
Packit 994f1a
		}
Packit 994f1a
	} else
Packit 994f1a
		optopt = *place++;
Packit 994f1a
Packit 994f1a
	/* See if option letter is one the caller wanted... */
Packit 994f1a
	if (optopt == ':' || (oli = strchr(optstring, optopt)) == NULL) {
Packit 994f1a
		if (*place == 0)
Packit 994f1a
			++optind;
Packit 994f1a
		if (opterr && *optstring != ':')
Packit 994f1a
			(void)fprintf(stderr,
Packit 994f1a
                                      "unknown option -- %c\n", optopt);
Packit 994f1a
		return (BADCH);
Packit 994f1a
	}
Packit 994f1a
Packit 994f1a
	/* Does this option need an argument? */
Packit 994f1a
	if (oli[1] != ':') {
Packit 994f1a
		/* don't need argument */
Packit 994f1a
		optarg = NULL;
Packit 994f1a
		if (*place == 0)
Packit 994f1a
			++optind;
Packit 994f1a
	} else {
Packit 994f1a
		/* Option-argument is either the rest of this argument or the
Packit 994f1a
		   entire next argument. */
Packit 994f1a
		if (*place)
Packit 994f1a
			optarg = place;
Packit 994f1a
		else if (argc > ++optind)
Packit 994f1a
			optarg = argv[optind];
Packit 994f1a
		else {
Packit 994f1a
			/* option-argument absent */
Packit 994f1a
			place = EMSG;
Packit 994f1a
			if (*optstring == ':')
Packit 994f1a
				return (BADARG);
Packit 994f1a
			if (opterr)
Packit 994f1a
				(void)fprintf(stderr,
Packit 994f1a
                                        "option requires an argument -- %c\n",
Packit 994f1a
                                        optopt);
Packit 994f1a
			return (BADCH);
Packit 994f1a
		}
Packit 994f1a
		place = EMSG;
Packit 994f1a
		++optind;
Packit 994f1a
	}
Packit 994f1a
	return (optopt);			/* return option letter */
Packit 994f1a
}
Packit 994f1a
/*
Packit 994f1a
 * Local Variables:
Packit 994f1a
 * mode: c
Packit 994f1a
 * c-basic-offset: 8
Packit 994f1a
 * fill-column: 78
Packit 994f1a
 * End:
Packit 994f1a
 */