xref: /onnv-gate/usr/src/cmd/ssh/libopenbsd-compat/common/getopt.c (revision 9845:0d705da26956)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 1987, 1993, 1994
30Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
60Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
70Sstevel@tonic-gate  * are met:
80Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
90Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
100Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
110Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
120Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
130Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
140Sstevel@tonic-gate  *    must display the following acknowledgement:
150Sstevel@tonic-gate  *	This product includes software developed by the University of
160Sstevel@tonic-gate  *	California, Berkeley and its contributors.
170Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
180Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
190Sstevel@tonic-gate  *    without specific prior written permission.
200Sstevel@tonic-gate  *
210Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
220Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
230Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
250Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
270Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
280Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
300Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
310Sstevel@tonic-gate  * SUCH DAMAGE.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "includes.h"
350Sstevel@tonic-gate #if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
380Sstevel@tonic-gate static char *rcsid = "$OpenBSD: getopt.c,v 1.2 1996/08/19 08:33:32 tholo Exp $";
390Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate int	BSDopterr = 1,		/* if error message should be printed */
460Sstevel@tonic-gate 	BSDoptind = 1,		/* index into parent argv vector */
470Sstevel@tonic-gate 	BSDoptopt,		/* character checked for validity */
480Sstevel@tonic-gate 	BSDoptreset;		/* reset getopt */
490Sstevel@tonic-gate char	*BSDoptarg;		/* argument associated with option */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #define	BADCH	(int)'?'
520Sstevel@tonic-gate #define	BADARG	(int)':'
530Sstevel@tonic-gate #define	EMSG	""
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * getopt --
570Sstevel@tonic-gate  *	Parse argc/argv argument vector.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate int
BSDgetopt(nargc,nargv,ostr)600Sstevel@tonic-gate BSDgetopt(nargc, nargv, ostr)
610Sstevel@tonic-gate 	int nargc;
620Sstevel@tonic-gate 	char * const *nargv;
630Sstevel@tonic-gate 	const char *ostr;
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	extern char *__progname;
660Sstevel@tonic-gate 	static char *place = EMSG;		/* option letter processing */
670Sstevel@tonic-gate 	char *oli;				/* option letter list index */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if (BSDoptreset || !*place) {		/* update scanning pointer */
700Sstevel@tonic-gate 		BSDoptreset = 0;
710Sstevel@tonic-gate 		if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
720Sstevel@tonic-gate 			place = EMSG;
730Sstevel@tonic-gate 			return (-1);
740Sstevel@tonic-gate 		}
750Sstevel@tonic-gate 		if (place[1] && *++place == '-') {	/* found "--" */
760Sstevel@tonic-gate 			++BSDoptind;
770Sstevel@tonic-gate 			place = EMSG;
780Sstevel@tonic-gate 			return (-1);
790Sstevel@tonic-gate 		}
800Sstevel@tonic-gate 	}					/* option letter okay? */
810Sstevel@tonic-gate 	if ((BSDoptopt = (int)*place++) == (int)':' ||
820Sstevel@tonic-gate 	    !(oli = strchr(ostr, BSDoptopt))) {
830Sstevel@tonic-gate 		/*
840Sstevel@tonic-gate 		 * if the user didn't specify '-' as an option,
850Sstevel@tonic-gate 		 * assume it means -1.
860Sstevel@tonic-gate 		 */
870Sstevel@tonic-gate 		if (BSDoptopt == (int)'-')
880Sstevel@tonic-gate 			return (-1);
890Sstevel@tonic-gate 		if (!*place)
900Sstevel@tonic-gate 			++BSDoptind;
910Sstevel@tonic-gate 		if (BSDopterr && *ostr != ':')
92*9845SJan.Pechanec@Sun.COM 			(void) fprintf(stderr,
93*9845SJan.Pechanec@Sun.COM 			    gettext("%s: illegal option -- %c\n"), __progname,
94*9845SJan.Pechanec@Sun.COM 			    BSDoptopt);
950Sstevel@tonic-gate 		return (BADCH);
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 	if (*++oli != ':') {			/* don't need argument */
980Sstevel@tonic-gate 		BSDoptarg = NULL;
990Sstevel@tonic-gate 		if (!*place)
1000Sstevel@tonic-gate 			++BSDoptind;
101*9845SJan.Pechanec@Sun.COM 	} else {				/* need an argument */
1020Sstevel@tonic-gate 		if (*place)			/* no white space */
1030Sstevel@tonic-gate 			BSDoptarg = place;
1040Sstevel@tonic-gate 		else if (nargc <= ++BSDoptind) {	/* no arg */
1050Sstevel@tonic-gate 			place = EMSG;
1060Sstevel@tonic-gate 			if (*ostr == ':')
1070Sstevel@tonic-gate 				return (BADARG);
1080Sstevel@tonic-gate 			if (BSDopterr)
109*9845SJan.Pechanec@Sun.COM 				(void) fprintf(stderr,
1100Sstevel@tonic-gate 				    "%s: option requires an argument -- %c\n",
1110Sstevel@tonic-gate 				    __progname, BSDoptopt);
1120Sstevel@tonic-gate 			return (BADCH);
113*9845SJan.Pechanec@Sun.COM 		/* white space */
114*9845SJan.Pechanec@Sun.COM 		} else
1150Sstevel@tonic-gate 			BSDoptarg = nargv[BSDoptind];
116*9845SJan.Pechanec@Sun.COM 
1170Sstevel@tonic-gate 		place = EMSG;
1180Sstevel@tonic-gate 		++BSDoptind;
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 	return (BSDoptopt);			/* dump back option letter */
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #endif /* !defined(HAVE_GETOPT) || !defined(HAVE_OPTRESET) */
124