From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/1022 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: printf POSIX compliance Date: Fri, 8 Jun 2012 13:00:56 -0400 Message-ID: <20120608170056.GU163@brightrain.aerifal.cx> References: <20120608144423.GN163@brightrain.aerifal.cx> <20120608145519.GP163@brightrain.aerifal.cx> <20120608150618.GB17860@port70.net> <4FD22C6C.5040704@barfooze.de> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1339175134 16771 80.91.229.3 (8 Jun 2012 17:05:34 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 8 Jun 2012 17:05:34 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-1023-gllmg-musl=m.gmane.org@lists.openwall.com Fri Jun 08 19:05:33 2012 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1Sd2cu-0004na-R2 for gllmg-musl@plane.gmane.org; Fri, 08 Jun 2012 19:05:32 +0200 Original-Received: (qmail 26011 invoked by uid 550); 8 Jun 2012 17:05:32 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 26000 invoked from network); 8 Jun 2012 17:05:32 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:1022 Archived-At: On Fri, Jun 08, 2012 at 05:46:10PM +0100, Reuben Thomas wrote: > On 8 June 2012 17:46, John Spencer wrote: > > > > this is bogus, according to Rich: > > "all files are closed when a process terminates normally/calls exit. > >  if you want to report write failures, just fflush(stdout) before exit and > > check the return value" > > Jim Meyering has an analysis of the problem here: > > http://www.gnu.org/ghm/2011/paris/#sec-2-1 He makes it a lot more difficult than it has to be. My preferred way of handling the situation is never to close stdout at all, only to flush it with fflush(stdout), then check ferror(stdout). This will report any new or past write errors that weren't already cleared by clearerr; the only errors it cannot report are error returns from the underlying close(1) operation. Some people want to detect such errors, in which case the following expression will reliably determine if any error occurred: ferror(stdout) || fclose(stdout) With that said, I question the value of checking for errors in close(). The only historical case where they have any consequence are with broken NFS setups (NFS is broken beyond usability for countless reasons, but this is getting off-topic...), where even the lack of an error from close() cannot ensure consistency. There's certainly not a need to do any of this with atexit functions or other ugly hacks. Whatever error check you do, it belongs at the point of normal program termination (or if you're handling multiple output files, perhaps the point where you finish handling stdout). Rich