summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
diff options
context:
space:
mode:
authorjtc <jtc@NetBSD.org>1994-05-06 18:18:33 +0000
committerjtc <jtc@NetBSD.org>1994-05-06 18:18:33 +0000
commita5284b9db48ff1e578eee29970845be83e0f540b (patch)
treea31566ab5fcc58b94817d8dcee73463b39390228 /lib/libc/stdlib
parent34ae5e111cdb9c6ed290538ba473ad2f167f8f2b (diff)
merge back in our changes (mostly POSIX.2 pedanticism)
Diffstat (limited to 'lib/libc/stdlib')
-rw-r--r--lib/libc/stdlib/getopt.330
-rw-r--r--lib/libc/stdlib/getopt.c11
2 files changed, 25 insertions, 16 deletions
diff --git a/lib/libc/stdlib/getopt.3 b/lib/libc/stdlib/getopt.3
index 95ff6e6875c..f843881afd8 100644
--- a/lib/libc/stdlib/getopt.3
+++ b/lib/libc/stdlib/getopt.3
@@ -38,7 +38,7 @@
.Nm getopt
.Nd get option character from command line argument list
.Sh SYNOPSIS
-.Fd #include <stdlib.h>
+.Fd #include <unistd.h>
.Vt extern char *optarg;
.Vt extern int optind;
.Vt extern int optopt;
@@ -120,8 +120,7 @@ must be reinitialized.
The
.Fn getopt
function
-returns an
-.Dv EOF
+returns \-1
when the argument list is exhausted, or a non-recognized
option is encountered.
The interpretation of options in the argument list may be cancelled
@@ -129,13 +128,11 @@ by the option
.Ql --
(double dash) which causes
.Fn getopt
-to signal the end of argument processing and return an
-.Dv EOF .
+to signal the end of argument processing and returns \-1.
When all options have been processed (i.e., up to the first non-option
argument),
.Fn getopt
-returns
-.Dv EOF .
+returns \-1.
.Sh DIAGNOSTICS
If the
.Fn getopt
@@ -177,7 +174,7 @@ extern int optind;
int bflag, ch, fd;
bflag = 0;
-while ((ch = getopt(argc, argv, "bf:")) != EOF)
+while ((ch = getopt(argc, argv, "bf:")) != -1)
switch(ch) {
case 'b':
bflag = 1;
@@ -202,6 +199,18 @@ The
function appeared
.Bx 4.3 .
.Sh BUGS
+The
+.Fn getopt
+function was once specified to return
+.Dv EOF
+instead of \-1.
+This was changed by
+.St -p1003.2-92
+to decouple
+.Fn getopt
+from
+.Pa <stdio.h> .
+.Pp
A single dash
.Dq Li -
may be specified as an character in
@@ -219,8 +228,7 @@ It is provided for backward compatibility
.Em only .
By default, a single dash causes
.Fn getopt
-to return
-.Dv EOF .
+to return \-1.
This is, we believe, compatible with System V.
.Pp
It is also possible to handle digits as option letters.
@@ -237,7 +245,7 @@ The following code fragment works in most cases.
int length;
char *p;
-while ((c = getopt(argc, argv, "0123456789")) != EOF)
+while ((c = getopt(argc, argv, "0123456789")) != -1)
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
diff --git a/lib/libc/stdlib/getopt.c b/lib/libc/stdlib/getopt.c
index 994d757533e..561f4623a7f 100644
--- a/lib/libc/stdlib/getopt.c
+++ b/lib/libc/stdlib/getopt.c
@@ -32,7 +32,8 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)getopt.c 8.2 (Berkeley) 4/2/94";
+/* static char sccsid[] = "from: @(#)getopt.c 8.2 (Berkeley) 4/2/94"; */
+static char *rcsid = "$Id: getopt.c,v 1.7 1994/05/06 18:18:35 jtc Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stdio.h>
@@ -67,22 +68,22 @@ getopt(nargc, nargv, ostr)
optreset = 0;
if (optind >= nargc || *(place = nargv[optind]) != '-') {
place = EMSG;
- return (EOF);
+ return (-1);
}
if (place[1] && *++place == '-') { /* found "--" */
++optind;
place = EMSG;
- return (EOF);
+ 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 EOF.
+ * assume it means -1.
*/
if (optopt == (int)'-')
- return (EOF);
+ return (-1);
if (!*place)
++optind;
if (opterr && *ostr != ':')