On BSD implementations, when we call lseek(), we must also update or disable
authorBruno Haible <bruno@clisp.org>
Thu, 26 Apr 2007 09:25:05 +0000 (09:25 +0000)
committerBruno Haible <bruno@clisp.org>
Thu, 26 Apr 2007 09:25:05 +0000 (09:25 +0000)
the stream's file descriptor position cache.

ChangeLog
lib/fflush.c
lib/fseeko.c
tests/test-fflush.c

index ddc5038..ad0547e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2007-04-26  Bruno Haible  <bruno@clisp.org>
 
+       * 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  <bruno@clisp.org>
+
        * modules/fflush-tests (Depends-on): Add fseeko.
 
 2007-04-25  Charles Wilson  <libtool@cwilson.fastmail.fm>
index 7481e66..aae6ac6 100755 (executable)
@@ -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;
 }
index de44802..e7a7f74 100644 (file)
@@ -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);
 }
index 79fa051..88789f3 100755 (executable)
@@ -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')
     {