diff --git a/lib/libc/stdlib/getopt.3 b/lib/libc/stdlib/getopt.3 index c8e80e1d54ce..2c3186718a2a 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 . +.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 5b5a1d19901b..8eb2d3d96eb6 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 @@ -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) {