summaryrefslogtreecommitdiff
path: root/devel/bmake/files/getopt.c
diff options
context:
space:
mode:
Diffstat (limited to 'devel/bmake/files/getopt.c')
-rw-r--r--devel/bmake/files/getopt.c89
1 files changed, 49 insertions, 40 deletions
diff --git a/devel/bmake/files/getopt.c b/devel/bmake/files/getopt.c
index eb1f0e65fe7..8945275bb8c 100644
--- a/devel/bmake/files/getopt.c
+++ b/devel/bmake/files/getopt.c
@@ -1,3 +1,5 @@
+/* $NetBSD: getopt.c,v 1.1.1.4 2020/05/24 05:35:52 nia Exp $ */
+
/*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -10,11 +12,7 @@
* 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. 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
+ * 3. 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,11 +33,7 @@
# include "config.h"
#endif
#if !defined(HAVE_GETOPT) || defined(WANT_GETOPT_LONG) || defined(BROKEN_GETOPT)
-
-#if defined(LIBC_SCCS) && !defined(lint)
-/* static char sccsid[] = "from: @(#)getopt.c 8.2 (Berkeley) 4/2/94"; */
-static char *rcsid = "$Id: getopt.c,v 1.1.1.3 2009/09/18 20:55:25 joerg Exp $";
-#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
#include <stdio.h>
#include <stdlib.h>
@@ -61,13 +55,10 @@ char *optarg; /* argument associated with option */
* Parse argc/argv argument vector.
*/
int
-getopt(nargc, nargv, ostr)
- int nargc;
- char * const *nargv;
- const char *ostr;
+getopt(int nargc, char * const nargv[], const char *ostr)
{
extern char *__progname;
- static char *place = EMSG; /* option letter processing */
+ static const char *place = EMSG; /* option letter processing */
char *oli; /* option letter list index */
#ifndef BSD4_4
@@ -79,43 +70,63 @@ getopt(nargc, nargv, ostr)
}
#endif
- if (optreset || !*place) { /* update scanning pointer */
+ if (optreset || *place == 0) { /* update scanning pointer */
optreset = 0;
- if (optind >= nargc || *(place = nargv[optind]) != '-') {
+ place = nargv[optind];
+ if (optind >= nargc || *place++ != '-') {
+ /* Argument is absent or is not an option */
place = EMSG;
return (-1);
}
- if (place[1] && *++place == '-' /* found "--" */
- && !place[1]) { /* and not "--foo" */
+ optopt = *place++;
+ if (optopt == '-' && *place == 0) {
+ /* "--" => end of options */
++optind;
place = EMSG;
return (-1);
}
- } /* 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)
+ 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)
++optind;
if (opterr && *ostr != ':')
(void)fprintf(stderr,
- "%s: illegal option -- %c\n", __progname, optopt);
+ "%s: unknown option -- %c\n", __progname, optopt);
return (BADCH);
}
- if (*++oli != ':') { /* don't need argument */
+
+ /* Does this option need an argument? */
+ if (oli[1] != ':') {
+ /* don't need argument */
optarg = NULL;
- if (!*place)
+ if (*place == 0)
++optind;
- }
- else { /* need an argument */
- if (*place) /* no white space */
- optarg = place;
- else if (nargc <= ++optind) { /* no arg */
+ } else {
+ /* Option-argument is either the rest of this argument or the
+ entire next argument. */
+ if (*place)
+ optarg = __UNCONST(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 */
place = EMSG;
if (*ostr == ':')
return (BADARG);
@@ -125,12 +136,10 @@ getopt(nargc, nargv, ostr)
__progname, optopt);
return (BADCH);
}
- else /* white space */
- optarg = nargv[optind];
place = EMSG;
++optind;
}
- return (optopt); /* dump back option letter */
+ return (optopt); /* return option letter */
}
#endif
#ifdef MAIN