Windows initialization fixes, tiny change from Adam Strzelecki <ono@java.pl>.
[gnulib.git] / lib / fflush.c
index be97769..22d7d02 100644 (file)
@@ -1,5 +1,5 @@
 /* fflush.c -- allow flushing input streams
-   Copyright (C) 2007 Free Software Foundation, Inc.
+   Copyright (C) 2007-2008 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include "freading.h"
 #include "fpurge.h"
 
+#include "stdio-impl.h"
+
 #undef fflush
 
+static inline void
+clear_ungetc_buffer (FILE *fp)
+{
+#if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+  if (HASUB (fp))
+    {
+      fp_->_p += fp_->_r;
+      fp_->_r = 0;
+    }
+#endif
+}
+
+#if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+
+static inline int
+disable_seek_optimization (FILE *fp)
+{
+  int saved_flags = fp_->_flags & (__SOPT | __SNPT);
+  fp_->_flags = (fp_->_flags & ~__SOPT) | __SNPT;
+  return saved_flags;
+}
+
+static inline void
+restore_seek_optimization (FILE *fp, int saved_flags)
+{
+  fp_->_flags = (fp_->_flags & ~(__SOPT | __SNPT)) | saved_flags;
+}
+
+#endif
+
+static inline void
+update_fpos_cache (FILE *fp, off_t pos)
+{
+#if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+  fp_->_offset = pos;
+  fp_->_flags |= __SOFF;
+#endif
+}
+
 /* Flush all pending data on STREAM according to POSIX rules.  Both
    output and seekable input streams are supported.  */
 int
@@ -59,6 +100,29 @@ rpl_fflush (FILE *stream)
   if (stream == NULL || ! freading (stream))
     return fflush (stream);
 
+  /* Clear the ungetc buffer.
+
+     This is needed before fetching the file-position indicator, because
+     1) The file position indicator is incremented by fgetc() and decremented
+        by ungetc():
+        <http://www.opengroup.org/susv3/functions/fgetc.html>
+          "... the fgetc() function shall ... advance the associated file
+           position indicator for the stream ..."
+        <http://www.opengroup.org/susv3/functions/ungetc.html>
+          "The file-position indicator is decremented by each successful
+           call to ungetc()..."
+     2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
+          "The value of the file-position indicator for the stream after
+           reading or discarding all pushed-back bytes shall be the same
+           as it was before the bytes were pushed back."
+     3) Here we are discarding all pushed-back bytes.
+
+     Unfortunately it is impossible to implement this on platforms with
+     _IOERR, because an ungetc() on this platform prepends the pushed-back
+     bytes to the buffer without an indication of the limit between the
+     pushed-back bytes and the read-ahead bytes.  */
+  clear_ungetc_buffer (stream);
+
   /* POSIX does not specify fflush behavior for non-seekable input
      streams.  Some implementations purge unread data, some return
      EBADF, some do nothing.  */
@@ -76,18 +140,17 @@ rpl_fflush (FILE *stream)
   if (result != 0)
     return result;
 
-#if defined __sferror && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
+#if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
 
   {
     /* Disable seek optimization for the next fseeko call.  This tells the
        following fseeko call to seek to the desired position directly, rather
        than to seek to a block-aligned boundary.  */
-    int saved_flags = stream->_flags & (__SOPT | __SNPT);
-    stream->_flags = (stream->_flags & ~__SOPT) | __SNPT;
+    int saved_flags = disable_seek_optimization (stream);
 
     result = fseeko (stream, pos, SEEK_SET);
 
-    stream->_flags = (stream->_flags & ~(__SOPT | __SNPT)) | saved_flags;
+    restore_seek_optimization (stream, saved_flags);
   }
   return result;
 
@@ -98,10 +161,7 @@ rpl_fflush (FILE *stream)
     return EOF;
   /* After a successful lseek, update the file descriptor's position cache
      in the stream.  */
-# if defined __sferror           /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
-  stream->_offset = pos;
-  stream->_flags |= __SOFF;
-# endif
+  update_fpos_cache (stream, pos);
 
   return 0;