Return -1 instead of 0 upon reaching EOF. This is somewhat ill-advised
because it means getdelim() returns -1 for both error and EOF, and never returns 0. However, this is what the original GNU implementation does, and POSIX inherited the bug. Reported by: marcus@
This commit is contained in:
parent
ecac0338c1
commit
6685ac34d9
@ -120,7 +120,6 @@ getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
|
||||
goto error;
|
||||
}
|
||||
|
||||
linelen = 0;
|
||||
if (*linecapp == 0)
|
||||
*linep = NULL;
|
||||
|
||||
@ -128,9 +127,12 @@ getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
|
||||
/* If fp is at EOF already, we just need space for the NUL. */
|
||||
if (__sferror(fp) || expandtofit(linep, 1, linecapp))
|
||||
goto error;
|
||||
goto done;
|
||||
FUNLOCKFILE(fp);
|
||||
(*linep)[0] = '\0';
|
||||
return (-1);
|
||||
}
|
||||
|
||||
linelen = 0;
|
||||
while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) {
|
||||
if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r))
|
||||
goto error;
|
||||
|
@ -24,7 +24,7 @@
|
||||
.\"
|
||||
.\" $FreeBSD$
|
||||
.\"
|
||||
.Dd February 28, 2009
|
||||
.Dd March 29, 2009
|
||||
.Dt GETLINE 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
@ -79,7 +79,7 @@ and
|
||||
functions return the number of characters written, excluding the
|
||||
terminating
|
||||
.Dv NUL .
|
||||
The value \-1 is returned if an error occurs.
|
||||
The value \-1 is returned if an error occurs, or if end-of-file is reached.
|
||||
.Sh EXAMPLES
|
||||
The following code fragment reads lines from a file and
|
||||
writes them to standard output.
|
||||
|
@ -100,7 +100,7 @@ main(int argc, char *argv[])
|
||||
assert(line[0] == '\0' && line[1] == '\0');
|
||||
/* Third line: EOF */
|
||||
line[0] = 'X';
|
||||
assert(getline(&line, &linecap, fp) == 0);
|
||||
assert(getline(&line, &linecap, fp) == -1);
|
||||
assert(line[0] == '\0');
|
||||
free(line);
|
||||
assert(feof(fp));
|
||||
@ -139,7 +139,7 @@ main(int argc, char *argv[])
|
||||
free(line);
|
||||
line = NULL;
|
||||
linecap = 0;
|
||||
assert(getline(&line, &linecap, fp) == 0);
|
||||
assert(getline(&line, &linecap, fp) == -1);
|
||||
assert(line[0] == '\0');
|
||||
assert(linecap > 0);
|
||||
assert(errno == 0);
|
||||
|
Loading…
x
Reference in New Issue
Block a user