X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Ffwriteerror.c;h=b3d6d2ae48546ac93fccdc5ba4913222a7d8f716;hb=d90792110b23736fb629c7f3ee150d917e8e9e3a;hp=f010674ba43a5d95a0fab48eb2efeb6ef45d094b;hpb=f3000fe73349f65bb7cc433fb5dccec2bce8d3b8;p=gnulib.git diff --git a/lib/fwriteerror.c b/lib/fwriteerror.c index f010674ba..b3d6d2ae4 100644 --- a/lib/fwriteerror.c +++ b/lib/fwriteerror.c @@ -16,9 +16,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifdef HAVE_CONFIG_H -# include -#endif +#include /* Specification. */ #include "fwriteerror.h" @@ -26,14 +24,20 @@ #include #include -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 && stdout_closed) - return 0; + 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, @@ -58,25 +62,47 @@ fwriteerror (FILE *fp) goto close_preserving_errno; /* errno is set here */ /* Give up on errno. */ errno = 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; - } + goto close_preserving_errno; } - /* 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 */ + 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); }