Blame port/getopt.c

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