Loading...
stdlib/FreeBSD/getopt.c Libc-1725.40.4 Libc-320
--- Libc/Libc-1725.40.4/stdlib/FreeBSD/getopt.c
+++ Libc/Libc-320/stdlib/FreeBSD/getopt.c
@@ -1,8 +1,4 @@
-/*	$NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $	*/
-
-/*-
- * SPDX-License-Identifier: BSD-3-Clause
- *
+/*
  * Copyright (c) 1987, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
  *
@@ -14,7 +10,11 @@
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
@@ -35,13 +35,12 @@
 static char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 4/27/95";
 #endif /* LIBC_SCCS and not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
+__FBSDID("$FreeBSD: src/lib/libc/stdlib/getopt.c,v 1.6 2002/03/29 22:43:42 markm Exp $");
 
 #include "namespace.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 #include "un-namespace.h"
 
 #include "libc_private.h"
@@ -54,99 +53,70 @@
 
 #define	BADCH	(int)'?'
 #define	BADARG	(int)':'
-static char EMSG[] = "";
-
-#if __DARWIN_UNIX03
-#define PROGNAME nargv[0]
-#else
-#define PROGNAME _getprogname()
-#endif
+#define	EMSG	""
 
 /*
  * getopt --
  *	Parse argc/argv argument vector.
  */
 int
-getopt(int nargc, char * const nargv[], const char *ostr)
+getopt(nargc, nargv, ostr)
+	int nargc;
+	char * const *nargv;
+	const char *ostr;
 {
 	static char *place = EMSG;		/* option letter processing */
 	char *oli;				/* option letter list index */
 
-	if (optreset || *place == 0) {		/* update scanning pointer */
+	if (optreset || !*place) {		/* update scanning pointer */
 		optreset = 0;
-		place = nargv[optind];
-		if (optind >= nargc || *place++ != '-') {
-			/* Argument is absent or is not an option */
+		if (optind >= nargc || *(place = nargv[optind]) != '-') {
 			place = EMSG;
 			return (-1);
 		}
-		optopt = *place++;
-		if (optopt == '-' && *place == 0) {
-			/* "--" => end of options */
+		if (place[1] && *++place == '-') {	/* found "--" */
 			++optind;
 			place = EMSG;
 			return (-1);
 		}
-		if (optopt == 0) {
-			/* Solitary '-', treat as a '-' option
-			   if the program (eg su) is looking for it. */
-			place = EMSG;
-			if (strchr(ostr, '-') == NULL)
-				return (-1);
-			optopt = '-';
-		}
-	} else
-		optopt = *place++;
-
-	/* See if option letter is one the caller wanted... */
-	if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) {
-		if (*place == 0)
+	}					/* option letter okay? */
+	if ((optopt = (int)*place++) == (int)':' ||
+	    !(oli = strchr(ostr, optopt))) {
+		/*
+		 * if the user didn't specify '-' as an option,
+		 * assume it means -1.
+		 */
+		if (optopt == (int)'-')
+			return (-1);
+		if (!*place)
 			++optind;
-		if (opterr && *ostr != ':')
-			(void)fprintf(stderr,
-			    "%s: illegal option -- %c\n",
-			    PROGNAME, optopt);
+		if (opterr && *ostr != ':' && optopt != BADCH)
+			(void)fprintf(stderr, "%s: illegal option -- %c\n",
+			    _getprogname(), optopt);
 		return (BADCH);
 	}
-
-	/* Does this option need an argument? */
-	if (oli[1] != ':') {
-		/* don't need argument */
+	if (*++oli != ':') {			/* don't need argument */
 		optarg = NULL;
-		if (*place == 0)
+		if (!*place)
 			++optind;
-	} else {
-		/* Option-argument is either the rest of this argument or the
-		   entire next argument. */
-		if (*place)
+	}
+	else {					/* need an argument */
+		if (*place)			/* no white space */
 			optarg = place;
-		else if (oli[2] == ':')
-			/*
-			 * GNU Extension, for optional arguments if the rest of
-			 * the argument is empty, we return NULL
-			 */
-			optarg = NULL;
-		else if (nargc > ++optind)
-			optarg = nargv[optind];
-		else {
-			/* option-argument absent */
-#if __DARWIN_UNIX03
-			/* Yes, the standard will put optind past the last
-			   argument */
-			++optind;
-			optarg = NULL;
-#endif /* __DARWIN_UNIX03 */
+		else if (nargc <= ++optind) {	/* no arg */
 			place = EMSG;
 			if (*ostr == ':')
 				return (BADARG);
 			if (opterr)
 				(void)fprintf(stderr,
 				    "%s: option requires an argument -- %c\n",
-				    PROGNAME, optopt);
+				    _getprogname(), optopt);
 			return (BADCH);
 		}
+	 	else				/* white space */
+			optarg = nargv[optind];
 		place = EMSG;
 		++optind;
 	}
-	return (optopt);			/* return option letter */
+	return (optopt);			/* dump back option letter */
 }