diff lib/fwriteerror.c @ 5581:c8676e66b5da

The fwriteerror() function now needs to fclose() the stream, otherwise it cannot detect write failure reliably on NFS.
author Bruno Haible <bruno@clisp.org>
date Mon, 17 Jan 2005 12:56:47 +0000 (2005-01-17)
parents 86c722c44f87
children a48fb0e98c8c
line wrap: on
line diff
--- a/lib/fwriteerror.c
+++ b/lib/fwriteerror.c
@@ -1,5 +1,5 @@
 /* Detect write error on a stream.
-   Copyright (C) 2003 Free Software Foundation, Inc.
+   Copyright (C) 2003-2005 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify
@@ -24,24 +24,30 @@
 #include "fwriteerror.h"
 
 #include <errno.h>
+#include <stdbool.h>
 
 int
 fwriteerror (FILE *fp)
 {
+  /* State to allow multiple calls to fwriteerror (stdout).  */
+  static bool stdout_closed = false;
+
+  if (fp == stdout && stdout_closed)
+    return 0;
+
   /* Need to
      1. test the error indicator of the stream,
-     2. flush the buffers (what fclose() would do), testing for error again.
-     We can equally well swap these steps; this leads to smaller code.  */
+     2. flush the buffers both in userland and in the kernel, through fclose,
+        testing for error again.  */
 
   /* Clear errno, so that on non-POSIX systems the caller doesn't see a
      wrong value of errno when we return -1.  */
   errno = 0;
 
-  if (fflush (fp))
-    return -1; /* errno is set here */
-
   if (ferror (fp))
     {
+      if (fflush (fp))
+	return -1; /* errno is set here */
       /* The stream had an error earlier, but its errno was lost.  If the
 	 error was not temporary, we can get the same errno by writing and
 	 flushing one more byte.  We can do so because at this point the
@@ -55,6 +61,13 @@
       return -1;
     }
 
+  /* If we are closing stdout, don't attempt to do it later again.  */
+  if (fp == stdout)
+    stdout_closed = true;
+
+  if (fclose (fp))
+    return -1; /* errno is set here */
+
   return 0;
 }
 
@@ -109,10 +122,6 @@
 	  else
 	    fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n",
 		     i, j);
-
-	  if (fclose (stream))
-	    fprintf (stderr, "Test %u:%u: fclose failed, errno = %d\n",
-		     i, j, errno);
 	}
     }