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.
This commit is contained in:
jtc 1993-09-14 22:37:24 +00:00
parent ff02097496
commit 2eb08c94c4
2 changed files with 29 additions and 16 deletions

View File

@ -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':

View File

@ -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) {