Blame samples/socks5-proxy/getopt.c

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