summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
diff options
context:
space:
mode:
authorjtc <jtc@NetBSD.org>1993-09-14 22:37:24 +0000
committerjtc <jtc@NetBSD.org>1993-09-14 22:37:24 +0000
commit2eb08c94c412bd2e87fe30a1164f7f9e58a7585a (patch)
treeb18693626e2dd8e9a53baadbf1e980b09e8eef2f /lib/libc/stdlib
parentff02097496a8c5b4ad516a2b13a6eb6e6f019936 (diff)
POSIX.2 has changed getopt to return -1 instead of EOF (to decouple getopt()
from standard i/o). This change is more pedantic than functional as EOF is defined to be -1.
Diffstat (limited to 'lib/libc/stdlib')
-rw-r--r--lib/libc/stdlib/getopt.335
-rw-r--r--lib/libc/stdlib/getopt.c10
2 files changed, 29 insertions, 16 deletions
diff --git a/lib/libc/stdlib/getopt.3 b/lib/libc/stdlib/getopt.3
index c8e80e1d54c..2c3186718a2 100644
--- a/lib/libc/stdlib/getopt.3
+++ b/lib/libc/stdlib/getopt.3
@@ -30,7 +30,7 @@
.\" SUCH DAMAGE.
.\"
.\" from: @(#)getopt.3 6.16 (Berkeley) 4/19/91
-.\" $Id: getopt.3,v 1.2 1993/08/01 07:44:26 mycroft Exp $
+.\" $Id: getopt.3,v 1.3 1993/09/14 22:37:24 jtc Exp $
.\"
.Dd April 19, 1991
.Dt GETOPT 3
@@ -94,8 +94,7 @@ evaluation.
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
@@ -103,13 +102,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 return \-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
@@ -131,7 +128,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;
@@ -150,12 +147,29 @@ while ((ch = getopt(argc, argv, "bf:")) != EOF)
argc -= optind;
argv += optind;
.Ed
+.Sh STANDARDS
+The
+.Fn getopt
+function conforms to
+.St -p1003.2-92 .
.Sh HISTORY
The
.Fn getopt
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
Option arguments are allowed to begin with
.Dq Li \- ;
this is reasonable but
@@ -178,8 +192,7 @@ It is provided for backward compatibility
.Em only .
By default, a single dash causes
.Fn getopt
-to return
-.Dv EOF .
+to returns \-1.
This is, we believe, compatible with System V.
.Pp
It is also possible to handle digits as option letters.
@@ -196,7 +209,7 @@ The following code fragment works fairly well.
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 5b5a1d19901..8eb2d3d96eb 100644
--- a/lib/libc/stdlib/getopt.c
+++ b/lib/libc/stdlib/getopt.c
@@ -33,7 +33,7 @@
#if defined(LIBC_SCCS) && !defined(lint)
/*static char *sccsid = "from: @(#)getopt.c 4.13 (Berkeley) 2/23/91";*/
-static char *rcsid = "$Id: getopt.c,v 1.3 1993/08/26 00:47:58 jtc Exp $";
+static char *rcsid = "$Id: getopt.c,v 1.4 1993/09/14 22:37:26 jtc Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stdio.h>
@@ -64,22 +64,22 @@ getopt(nargc, nargv, ostr)
if (!*place) { /* update scanning pointer */
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 = index(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) {