From f0313cd30887e39dce414d48851082b81b7336de Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Fri, 30 Jan 1998 16:57:37 +0000 Subject: [PATCH] Return an error only if there is a new error. --- libio/iofgets.c | 7 +++++++ libio/iogets.c | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/libio/iofgets.c b/libio/iofgets.c index 7a1044f8018..24f8333708d 100644 --- a/libio/iofgets.c +++ b/libio/iofgets.c @@ -33,11 +33,17 @@ _IO_fgets (buf, n, fp) { _IO_size_t count; char *result; + int old_error; CHECK_FILE (fp, NULL); if (n <= 0) return NULL; _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp); _IO_flockfile (fp); + /* This is very tricky since a file descriptor may be in the + non-blocking mode. The error flag doesn't mean much in this + case. We return an error only when there is a new error. */ + old_error = fp->_IO_file_flags & _IO_ERR_SEEN; + fp->_IO_file_flags &= ~_IO_ERR_SEEN; count = _IO_getline (fp, buf, n - 1, '\n', 1); if (count == 0 || (fp->_IO_file_flags & _IO_ERR_SEEN)) result = NULL; @@ -46,6 +52,7 @@ _IO_fgets (buf, n, fp) buf[count] = '\0'; result = buf; } + fp->_IO_file_flags |= old_error; _IO_cleanup_region_end (1); return result; } diff --git a/libio/iogets.c b/libio/iogets.c index 0e87504107c..5bf3f234cbe 100644 --- a/libio/iogets.c +++ b/libio/iogets.c @@ -46,6 +46,11 @@ _IO_gets (buf) count = 0; else { + /* This is very tricky since a file descriptor may be in the + non-blocking mode. The error flag doesn't mean much in this + case. We return an error only when there is a new error. */ + int old_error = _IO_stdin->_IO_file_flags & _IO_ERR_SEEN; + _IO_stdin->_IO_file_flags &= ~_IO_ERR_SEEN; buf[0] = (char)ch; count = _IO_getline (_IO_stdin, buf + 1, INT_MAX, '\n', 0) + 1; if (_IO_stdin->_IO_file_flags & _IO_ERR_SEEN) @@ -53,6 +58,8 @@ _IO_gets (buf) retval = NULL; goto unlock_return; } + else + _IO_stdin->_IO_file_flags |= old_error; } buf[count] = 0; retval = buf; -- 2.47.2