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