open, fopen: close fd leak in last patch
[gnulib.git] / lib / fwriteerror.c
index 364db86..3ea3ecc 100644 (file)
@@ -1,11 +1,11 @@
 /* Detect write error on a stream.
-   Copyright (C) 2003 Free Software Foundation, Inc.
+   Copyright (C) 2003-2006 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
-   This program is free software; you can redistribute it and/or modify
+   This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
+#include <config.h>
 
 /* Specification.  */
 #include "fwriteerror.h"
 
 #include <errno.h>
+#include <stdbool.h>
 
-int
-fwriteerror (FILE *fp)
+static int
+do_fwriteerror (FILE *fp, bool ignore_ebadf)
 {
+  /* State to allow multiple calls to fwriteerror (stdout).  */
+  static bool stdout_closed = false;
+
+  if (fp == stdout)
+    {
+      if (stdout_closed)
+       return 0;
+
+      /* If we are closing stdout, don't attempt to do it later again.  */
+      stdout_closed = true;
+    }
+
   /* 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))
+       goto close_preserving_errno; /* 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
         stream's contents is garbage anyway.  */
       if (fputc ('\0', fp) == EOF)
-       return -1; /* errno is set here */
+       goto close_preserving_errno; /* errno is set here */
       if (fflush (fp))
-       return -1; /* errno is set here */
+       goto close_preserving_errno; /* errno is set here */
       /* Give up on errno.  */
       errno = 0;
-      return -1;
+      goto close_preserving_errno;
+    }
+
+  if (ignore_ebadf)
+    {
+      /* We need an explicit fflush to tell whether some output was already
+        done on FP.  */
+      if (fflush (fp))
+       goto close_preserving_errno; /* errno is set here */
+      if (fclose (fp) && errno != EBADF)
+       return -1; /* errno is set here */
+    }
+  else
+    {
+      if (fclose (fp))
+       return -1; /* errno is set here */
     }
 
   return 0;
+
+ close_preserving_errno:
+  /* There's an error.  Nevertheless call fclose(fp), for consistency
+     with the other cases.  */
+  {
+    int saved_errno = errno;
+    fclose (fp);
+    errno = saved_errno;
+    return -1;
+  }
+}
+
+int
+fwriteerror (FILE *fp)
+{
+  return do_fwriteerror (fp, false);
+}
+
+int
+fwriteerror_no_ebadf (FILE *fp)
+{
+  return do_fwriteerror (fp, true);
 }
 
 
@@ -109,10 +155,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);
        }
     }