bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] fclose: don't fail on non-seekable input stream


From: Eric Blake
Subject: [PATCH] fclose: don't fail on non-seekable input stream
Date: Sat, 30 Apr 2011 17:37:51 -0600

On mingw, for a project that uses both sockets and streams,
then fclose(stdin) when getting input from a terminal or pipe
was spuriously failing.

* modules/fclose (Depends-on): Add freading, fflush, fseeko.
* lib/fclose.c (rpl_fclose): Skip fflush for non-seekable input,
since fflush is allowed to fail in that case.

Signed-off-by: Eric Blake <address@hidden>
---

I've tested this on mingw, and it cleared up the problems I was seeing.

This also has the (nice) side-effect that fclose(NULL) now fails
without trying to flush all possible streams; however, calling
fclose(NULL) is already unspecified behavior so it isn't really a
bug fix so much as a QoI improvement.

 ChangeLog      |    7 +++++++
 lib/fclose.c   |    6 +++++-
 modules/fclose |    3 +++
 3 files changed, 15 insertions(+), 1 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index eb233f6..3b5331c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2011-04-30  Eric Blake  <address@hidden>
+
+       fclose: don't fail on non-seekable input stream
+       * modules/fclose (Depends-on): Add freading, fflush, fseeko.
+       * lib/fclose.c (rpl_fclose): Skip fflush for non-seekable input,
+       since fflush is allowed to fail in that case.
+
 2011-04-30  Bruno Haible  <address@hidden>

        dup3: cleanup
diff --git a/lib/fclose.c b/lib/fclose.c
index 1d7e85b..bce409c 100644
--- a/lib/fclose.c
+++ b/lib/fclose.c
@@ -22,6 +22,8 @@
 #include <errno.h>
 #include <unistd.h>

+#include "freading.h"
+
 /* Override fclose() to call the overridden close().  */

 int
@@ -30,7 +32,9 @@ rpl_fclose (FILE *fp)
 {
   int saved_errno = 0;

-  if (fflush (fp))
+  /* We only need to flush the file if it is not reading or if it is
+     seekable.  */
+  if ((!freading (fp) || fseeko (fp, 0, SEEK_CUR) == 0) && fflush (fp))
     saved_errno = errno;

   if (close (fileno (fp)) < 0 && saved_errno == 0)
diff --git a/modules/fclose b/modules/fclose
index 4f6f786..d8727c3 100644
--- a/modules/fclose
+++ b/modules/fclose
@@ -8,6 +8,9 @@ m4/fclose.m4
 Depends-on:
 stdio
 close
+fflush
+freading
+fseeko

 configure.ac:
 gl_FUNC_FCLOSE
-- 
1.7.4.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]