From: Bruno Haible Date: Thu, 26 Apr 2007 09:25:05 +0000 (+0000) Subject: On BSD implementations, when we call lseek(), we must also update or disable X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0a786d259145a78dc117cce97b128a787f804871;p=pspp On BSD implementations, when we call lseek(), we must also update or disable the stream's file descriptor position cache. --- diff --git a/ChangeLog b/ChangeLog index ddc5038d25..ad0547e188 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2007-04-26 Bruno Haible + + * tests/test-fflush.c (main): Also check the ftell result after + fflush and fseek/fseeko. + * lib/fflush.c (rpl_fflush): For BSD implementations, update the + file descriptor position cache in the stream. + * lib/fseeko.c (rpl_fseeko): Likewise. + 2007-04-26 Bruno Haible * modules/fflush-tests (Depends-on): Add fseeko. diff --git a/lib/fflush.c b/lib/fflush.c index 7481e66919..aae6ac67a3 100755 --- a/lib/fflush.c +++ b/lib/fflush.c @@ -58,7 +58,14 @@ rpl_fflush (FILE *stream) semantics of fpurge are now appropriate to clear the buffer. To avoid losing data, the lseek is also necessary. */ result = fpurge (stream); - if (result == 0 && lseek (fileno (stream), pos, SEEK_SET) == -1) + if (result != 0) + return result; + pos = lseek (fileno (stream), pos, SEEK_SET); + if (pos == -1) return EOF; - return result; +#if defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */ + stream->_offset = pos; + stream->_flags |= __SOFF; +#endif + return 0; } diff --git a/lib/fseeko.c b/lib/fseeko.c index de44802b6f..e7a7f74952 100644 --- a/lib/fseeko.c +++ b/lib/fseeko.c @@ -66,7 +66,24 @@ rpl_fseeko (FILE *fp, off_t offset, int whence) #else #error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib." #endif - return (lseek (fileno (fp), offset, whence) == (off_t)(-1) ? -1 : 0); + { + off_t pos = lseek (fileno (fp), offset, whence); + if (pos == -1) + { +#if defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */ + fp->_flags &= ~__SOFF; +#endif + return -1; + } + else + { +#if defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */ + fp->_offset = pos; + fp->_flags |= __SOFF; +#endif + return 0; + } + } else return fseeko (fp, offset, whence); } diff --git a/tests/test-fflush.c b/tests/test-fflush.c index 79fa051263..88789f30ac 100755 --- a/tests/test-fflush.c +++ b/tests/test-fflush.c @@ -74,6 +74,13 @@ main (int argc, char *argv[]) unlink ("test-fflush.txt"); return 1; } + if (ftell (f) != 5) + { + fputs ("ftell result is wrong after fseek.\n", stderr); + fclose (f); + unlink ("test-fflush.txt"); + return 1; + } /* Check that file reading resumes at correct location. */ if (fgetc (f) != '6') { @@ -106,6 +113,13 @@ main (int argc, char *argv[]) unlink ("test-fflush.txt"); return 1; } + if (ftell (f) != 6) + { + fputs ("ftell result is wrong after fseek.\n", stderr); + fclose (f); + unlink ("test-fflush.txt"); + return 1; + } /* Check that file reading resumes at correct location. */ if (fgetc (f) != '7') {