Make fflush rely on fpurge.
authorEric Blake <ebb9@byu.net>
Tue, 17 Apr 2007 03:38:08 +0000 (03:38 +0000)
committerEric Blake <ebb9@byu.net>
Tue, 17 Apr 2007 03:38:08 +0000 (03:38 +0000)
* lib/fflush.c (rpl_fflush): Rely on fpurge module, rather than
open coding all variants.
* modules/fflush (Depends-on): Add fpurge and unistd.
* modules/fflush-tests (Depends-on): Unistd is no longer extra.
* m4/fflush.m4 (gl_REPLACE_FFLUSH): Simplify.

ChangeLog
lib/fflush.c
m4/fflush.m4
modules/fflush
modules/fflush-tests

index 70198231d632661331595ed7bcec0f98411272b4..f4455c3c026adb14edc134df8c885b8bf2b48ee9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2007-04-16  Eric Blake  <ebb9@byu.net>
 
+       Make fflush rely on fpurge.
+       * lib/fflush.c (rpl_fflush): Rely on fpurge module, rather than
+       open coding all variants.
+       * modules/fflush (Depends-on): Add fpurge and unistd.
+       * modules/fflush-tests (Depends-on): Unistd is no longer extra.
+       * m4/fflush.m4 (gl_REPLACE_FFLUSH): Simplify.
+
        Fix --with-tests compilation on cygwin.
        * modules/argmatch-tests (Makefile.am): List gnulib library first
        in LDADD.
index 5271e17dfa58a42f5e9154bc011ec896d77562ee..8c902f80d84485d9d188e992267700412b442202 100755 (executable)
 #include <errno.h>
 #include <stdio.h>
 
-#if HAVE_STDIO_EXT_H
-# include <stdio_ext.h>
-#endif
-
-#if HAVE_FPURGE && ! HAVE_DECL_FPURGE
-int fpurge (FILE *);
-#endif
+#include "fpurge.h"
 
 #undef fflush
 
@@ -36,53 +30,31 @@ int fpurge (FILE *);
 int
 rpl_fflush (FILE *stream)
 {
-  int e; /* 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.  */
   result = fflush (stream);
-  if (! stream || result == 0 || (e = errno) != EBADF)
+  if (! stream || result == 0 || errno != EBADF)
     return result;
 
-  /* POSIX does not specify behavior for non-seekable streams.  */
-  if (fseeko (stream, 0, SEEK_CUR) != 0)
+  /* POSIX does not specify fflush behavior for non-seekable input
+     streams.  */
+  pos = ftello (stream);
+  if (pos == -1)
     {
-      errno = e;
+      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
+     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;
-#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 = e;
-      }
-    else
-      result = 0;
-  }
-#endif /* ! HAVE___FPURGE */
-
+  if (result == 0 && lseek (fileno (stream), pos, SEEK_SET) == -1)
+    return EOF;
   return result;
 }
index 161680f2cea0f217fe812bf992f9d3c40ff54d95..d4ebeccb6cfaca84c38b0f34b893e7908fc1c3a1 100755 (executable)
@@ -1,4 +1,4 @@
-#serial 1
+#serial 2
 
 # Copyright (C) 2007 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
index ccb58b08894e703b4907580798cdefa473b2004c..ff79fa6112e6bb5454e636bd504c999b5096b472 100755 (executable)
@@ -6,7 +6,9 @@ lib/fflush.c
 m4/fflush.m4
 
 Depends-on:
+fpurge
 stdio
+unistd
 
 configure.ac:
 gl_FUNC_FFLUSH
index 0f0e5670effb499bb31ff8120f43f6c1addae9df..c0979dd30e662faa41db9efc145984b7b0c2e550 100755 (executable)
@@ -2,7 +2,6 @@ Files:
 tests/test-fflush.c
 
 Depends-on:
-unistd
 
 configure.ac: