* lib/utimens.c (gl_futimens): Rename from futimens,
[gnulib.git] / lib / fflush.c
index c4c575f..459a710 100755 (executable)
 /* Written by Eric Blake. */
 
 #include <config.h>
-#include <errno.h>
+
+/* Specification.  */
 #include <stdio.h>
 
-#if HAVE_STDIO_EXT_H
-# include <stdio_ext.h>
-#endif
+#include <errno.h>
+#include <unistd.h>
 
-#if HAVE_FPURGE && ! HAVE_DECL_FPURGE
-int fpurge (FILE *);
-#endif
+#include "freading.h"
+#include "fpurge.h"
 
 #undef fflush
 
@@ -36,61 +35,57 @@ int fpurge (FILE *);
 int
 rpl_fflush (FILE *stream)
 {
-  int e1; /* Leave errno unchanged on success.  */
-  int e2; /* Capture errno of first fflush if nothing else succeeds.  */
   int result;
+  off_t pos;
 
-  /* Try flushing the stream.  C89 guarantees behavior of output
-     streams, so we only need to worry if failure might have been on
-     an input stream.  When stream is NULL, POSIX only requires
-     flushing of output streams.  */
-  e1 = errno;
-  result = fflush (stream);
-  if (! stream || result == 0 || errno != EBADF)
-    return result;
+  /* When stream is NULL, POSIX and C99 only require flushing of "output
+     streams and update streams in which the most recent operation was not
+     input", and all implementations do this.
+
+     When stream is "an output stream or an update stream in which the most
+     recent operation was not input", POSIX and C99 requires that fflush
+     writes out any buffered data, and all implementations do this.
 
-  /* POSIX does not specify behavior for non-seekable streams.  */
-  e2 = errno;
-  if (fseeko (stream, 0, SEEK_CUR) != 0)
+     When stream is, however, an input stream or an update stream in
+     which the most recent operation was input, C99 specifies nothing,
+     and POSIX only specifies behavior if the stream is seekable.
+     mingw, in particular, drops the input buffer, leaving the file
+     descriptor positioned at the end of the input buffer. I.e. ftell
+     (stream) is lost.  We don't want to call the implementation's
+     fflush in this case.
+
+     We test ! freading (stream) here, rather than fwriting (stream), because
+     what we need to know is whether the stream holds a "read buffer", and on
+     mingw this is indicated by _IOREAD, regardless of _IOWRT.  */
+  if (stream == NULL || ! freading (stream))
+    return fflush (stream);
+
+  /* POSIX does not specify fflush behavior for non-seekable input
+     streams.  Some implementations purge unread data, some return
+     EBADF, some do nothing.  */
+  pos = ftello (stream);
+  if (pos == -1)
     {
-      errno = e2;
+      errno = EBADF;
       return EOF;
     }
 
   /* To get here, we must be flushing a seekable input stream, so the
-     semantics of fpurge are now appropriate.  */
-#if HAVE_FPURGE
-  errno = e1;
+     semantics of fpurge are now appropriate to clear the buffer.  To
+     avoid losing data, the lseek is also necessary.  */
   result = fpurge (stream);
-#elif HAVE___FPURGE
-  /* __fpurge has no return value, and on Solaris, we can't even trust
-     errno.  So assume it succeeds.  */
-  __fpurge (stream);
-  result = 0;
-  errno = e1;
-#else /* ! HAVE___FPURGE */
-
-  /* No single replacement; do it manually.  */
-  {
-    off_t position = ftello (stream);
-    if (position == -1)
-      {
-       result = EOF; /* Should not happen; we know stream is seekable.  */
-      }
-    /* Set position of stream; hopefully the stdio routines don't
-       overoptimize by not setting the underlying file position.  */
-    else if (fseeko (stream, position, SEEK_SET) != 0)
-      {
-       result = EOF;
-       errno = e2;
-      }
-    else
-      {
-       result = 0;
-       errno = e1;
-      }
-  }
-#endif /* ! HAVE___FPURGE */
-
-  return result;
+  if (result != 0)
+    return result;
+
+  pos = lseek (fileno (stream), pos, SEEK_SET);
+  if (pos == -1)
+    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
+
+  return 0;
 }