From: Bruno Haible Date: Fri, 29 Sep 2006 13:48:32 +0000 (+0000) Subject: Handle file descriptors which were not open from the beginning. X-Git-Tag: 0.16.x-branchpoint~148 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=39478c56fcd799f14ebfb18755844d89fd071ace;p=thirdparty%2Fgettext.git Handle file descriptors which were not open from the beginning. --- diff --git a/gnulib-local/ChangeLog b/gnulib-local/ChangeLog index 4e69105a0..5acc86bee 100644 --- a/gnulib-local/ChangeLog +++ b/gnulib-local/ChangeLog @@ -1,3 +1,9 @@ +2006-09-29 Bruno Haible + + * lib/closeout.c (close_stdout_status): Remove function. + (close_stdout): Inline it. Call fwriteerror_no_ebadf instead of + fwriteerror. Also close stderr. + 2006-09-14 Bruno Haible * lib/addext.c: Include unconditionally. diff --git a/gnulib-local/lib/closeout.c b/gnulib-local/lib/closeout.c index 4a6c70737..7dc9ec269 100644 --- a/gnulib-local/lib/closeout.c +++ b/gnulib-local/lib/closeout.c @@ -1,4 +1,4 @@ -/* closeout.c - close standard output +/* closeout.c - close standard output and standard error Copyright (C) 1998-2006 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify @@ -23,10 +23,6 @@ #include #include -#if 0 -#include "unlocked-io.h" -#include "__fpending.h" -#endif #include "error.h" #include "fwriteerror.h" #include "exit.h" @@ -51,22 +47,36 @@ that writes to stdout -- just let the internal stream state record the failure. That's what the ferror test is checking below. + If the stdout file descriptor was initially closed (such as when executing + a program through "program 1>&-"), it is a failure if and only if some + output was made to stdout. + + Likewise for standard error. + It's important to detect such failures and exit nonzero because many tools (most notably `make' and other build-management systems) depend on being able to detect failure in other tools via their exit status. */ -static void -close_stdout_status (int status) -{ - if (fwriteerror (stdout)) - error (status, errno, "%s", _("write error")); -} - -/* Close standard output, exiting with status EXIT_FAILURE on failure. */ +/* Close standard output and standard error, exiting with status EXIT_FAILURE + on failure. */ void close_stdout (void) { - close_stdout_status (EXIT_FAILURE); + /* Close standard output. */ + if (fwriteerror_no_ebadf (stdout)) + error (EXIT_FAILURE, errno, "%s", _("write error")); + + /* Close standard error. This is simpler than fwriteerror_no_ebadf, because + upon failure we don't need an errno - all we can do at this point is to + set an exit status. */ + errno = 0; + if (ferror (stderr) || fflush (stderr)) + { + fclose (stderr); + exit (EXIT_FAILURE); + } + if (fclose (stderr) && errno != EBADF) + exit (EXIT_FAILURE); } /* Note: When exit (...) calls the atexit-registered