NEWS.stable: log cherry-pick [e446f25]->[c092018] relocatable-shell: Update suggested...
[gnulib.git] / lib / close-stream.c
index 72d0d68..87921d4 100644 (file)
@@ -1,12 +1,11 @@
 /* Close a stream, with nicer error checking than fclose's.
 
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2006 Free
-   Software Foundation, Inc.
+   Copyright (C) 1998-2002, 2004, 2006-2014 Free Software Foundation, Inc.
 
-   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
@@ -14,8 +13,7 @@
    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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 
@@ -24,7 +22,7 @@
 #include <errno.h>
 #include <stdbool.h>
 
-#include "__fpending.h"
+#include "fpending.h"
 
 #if USE_UNLOCKED_IO
 # include "unlocked-io.h"
    otherwise.  A failure might set errno to 0 if the error number
    cannot be determined.
 
+   A failure with errno set to EPIPE may or may not indicate an error
+   situation worth signaling to the user.  See the documentation of the
+   close_stdout_set_ignore_EPIPE function for details.
+
    If a program writes *anything* to STREAM, that program should close
    STREAM and make sure that it succeeds before exiting.  Otherwise,
    suppose that you go to the extreme of checking the return status
 int
 close_stream (FILE *stream)
 {
-  bool some_pending = (__fpending (stream) != 0);
-  bool prev_fail = (ferror (stream) != 0);
-  bool fclose_fail = (fclose (stream) != 0);
+  const bool some_pending = (__fpending (stream) != 0);
+  const bool prev_fail = (ferror (stream) != 0);
+  const bool fclose_fail = (fclose (stream) != 0);
 
   /* Return an error indication if there was a previous failure or if
      fclose failed, with one exception: ignore an fclose failure if
      there was no previous error, no data remains to be flushed, and
      fclose failed with EBADF.  That can happen when a program like cp
-     is invoked like this `cp a b >&-' (i.e., with standard output
+     is invoked like this 'cp a b >&-' (i.e., with standard output
      closed) and doesn't generate any output (hence no previous error
      and nothing to be flushed).  */
 
   if (prev_fail || (fclose_fail && (some_pending || errno != EBADF)))
     {
       if (! fclose_fail)
-       errno = 0;
+        errno = 0;
       return EOF;
     }