Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Fri, 8 Jun 2012 13:00:56 -0400
From: Rich Felker <>
Subject: Re: printf POSIX compliance

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:

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).


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.