From mboxrd@z Thu Jan 1 00:00:00 1970 From: hjl@lucon.org (H.J. Lu) To: schwab@suse.de (Andreas Schwab) Cc: libc-hacker@sourceware.cygnus.com (GNU C Library) Subject: Re: [Gurusamy Sarathy ] ferror() after fread() on a FILE* opened for write Date: Wed, 21 Jul 1999 08:08:00 -0000 Message-id: <19990721150806.B4B9D57BA@ocean.lucon.org> References: X-SW-Source: 1999-07/msg00072.html This patch should pass the test. The problem is _IO_file_xsgetn and _IO_XXX_file_xsputn may call read/write directly. -- H.J. Lu (hjl@gnu.org) ---- Wed Jul 21 08:03:47 1999 H.J. Lu * libio/fileops.c (_IO_new_file_underflow): Set the error flag if fp is not opened for read. * libio/oldfileops.c (_IO_old_file_underflow): Likewise. * libio/fileops.c (_IO_new_file_xsputn): Set the error flag and return 0 if f is not opened for write. * libio/oldfileops.c (_IO_old_file_xsputn): Likewise. * libio/fileops.c (_IO_file_xsgetn): Set the error flag and return 0 if f is not opened for read. Index: libio/fileops.c =================================================================== RCS file: /work/cvs/gnu/glibc-2.1/libio/fileops.c,v retrieving revision 1.10 diff -u -p -r1.10 fileops.c --- libio/fileops.c 1999/05/01 22:41:30 1.10 +++ libio/fileops.c 1999/07/21 14:58:22 @@ -349,6 +349,7 @@ _IO_new_file_underflow (fp) if (fp->_flags & _IO_NO_READS) { __set_errno (EBADF); + fp->_flags |= _IO_ERR_SEEN; return EOF; } if (fp->_IO_read_ptr < fp->_IO_read_end) @@ -745,6 +746,13 @@ _IO_new_file_xsputn (f, data, n) int must_flush = 0; _IO_size_t count; + if (f->_flags & _IO_NO_WRITES) + { + __set_errno (EBADF); + f->_flags |= _IO_ERR_SEEN; + return 0; + } + if (n <= 0) return 0; /* This is an optimized implementation. @@ -833,6 +841,13 @@ _IO_file_xsgetn (fp, data, n) register _IO_size_t want, have; register _IO_ssize_t count; register char *s = data; + + if (fp->_flags & _IO_NO_READS) + { + __set_errno (EBADF); + fp->_flags |= _IO_ERR_SEEN; + return 0; + } want = n; Index: libio/oldfileops.c =================================================================== RCS file: /work/cvs/gnu/glibc-2.1/libio/oldfileops.c,v retrieving revision 1.2 diff -u -p -r1.2 oldfileops.c --- libio/oldfileops.c 1999/05/01 22:41:30 1.2 +++ libio/oldfileops.c 1999/07/21 14:59:15 @@ -313,6 +313,7 @@ _IO_old_file_underflow (fp) if (fp->_flags & _IO_NO_READS) { __set_errno (EBADF); + fp->_flags |= _IO_ERR_SEEN; return EOF; } if (fp->_IO_read_ptr < fp->_IO_read_end) @@ -668,6 +669,13 @@ _IO_old_file_xsputn (f, data, n) int must_flush = 0; _IO_size_t count; + if (f->_flags & _IO_NO_WRITES) + { + __set_errno (EBADF); + f->_flags |= _IO_ERR_SEEN; + return 0; + } + if (n <= 0) return 0; /* This is an optimized implementation.