The fwriteerror() function now needs to fclose() the stream,
authorBruno Haible <bruno@clisp.org>
Mon, 17 Jan 2005 12:56:47 +0000 (12:56 +0000)
committerBruno Haible <bruno@clisp.org>
Mon, 17 Jan 2005 12:56:47 +0000 (12:56 +0000)
otherwise it cannot detect write failure reliably on NFS.

lib/ChangeLog
lib/fwriteerror.c
lib/fwriteerror.h

index 6b4623ecaaa3db4d79a88d89f4e17b3fb3df4f98..3fe9f20874461695ab9fcba462b2b4f394b9fc08 100644 (file)
@@ -1,3 +1,10 @@
+2005-01-06  Bruno Haible  <bruno@clisp.org>
+
+       * fwriteerror.h (fwriteerror): Change specification to include fclose.
+       * fwriteerror.c: Include <stdbool.h>.
+       (fwriteerror): At the end, close the file stream. Record whether
+       stdout was already closed.
+
 2004-05-27  Bruno Haible  <bruno@clisp.org>
 
        * execute.c (environ): Declare if needed.
index 364db8609372dd8087bd216a32414d8b5abd1329..97fbed261770dcb3258525d72c84d3b2b610a653 100644 (file)
@@ -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
 #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 @@ fwriteerror (FILE *fp)
       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 @@ main ()
          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);
        }
     }
 
index 82cf5f18e3552008b63984d696585b723bef26a1..570023778875a4942ddac8461fedcb688e1ee474 100644 (file)
@@ -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
@@ -22,7 +22,7 @@
      (a) Test the return value of every fwrite() or fprintf() call, and react
          immediately.
      (b) Just before fclose(), test the error indicator in the stream and
-         the return value of the final fflush() or fclose() call.
+         the return value of the final fclose() call.
 
    The benefit of (a) is that non file related errors (such that ENOMEM during
    fprintf) and temporary error conditions can be diagnosed accurately.
 
 #include <stdio.h>
 
-/* Write out the not yet written buffered contents of the stream FP, and then
-   test whether some error occurred on the stream FP.  FP must be a stream
-   opened for writing.
-   Return 0 if no error occurred.  In this case it can be assumed that
-   fclose (fp) will succeed.
+/* Write out the not yet written buffered contents of the stream FP, close
+   the stream FP, and test whether some error occurred on the stream FP.
+   FP must be a stream opened for writing.
+   Return 0 if no error occurred and fclose (fp) succeeded.
    Return -1 and set errno if there was an error.  The errno value will be 0
    if the cause of the error cannot be determined.
- */
+   For any given stream FP other than stdout, fwriteerror (FP) may only be
+   called once.  */
 extern int fwriteerror (FILE *fp);