public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Karel Zak <kzak@redhat.com>
To: kerolasa@gmail.com
Cc: util-linux <util-linux@vger.kernel.org>
Subject: Re: [pull] check stream status
Date: Wed, 4 Apr 2012 13:09:17 +0200	[thread overview]
Message-ID: <20120404110917.GN1084@x2.net.home> (raw)
In-Reply-To: <CAG27Bk2cNH-x6D=H4C6+NiHJDp4-_dWuU9BJuubTWFOcr-Bu9Q@mail.gmail.com>

On Thu, Mar 29, 2012 at 10:28:59PM +0200, Sami Kerola wrote:
> Reference: http://www.irill.org/events/ghm-gnu-hackers-meeting/videos/jim-meyering-goodbye-world-the-perils-of-relying-on-output-streams-in-c

 thanks :-)

>  include/fileutils.h |    2 ++
>  lib/fileutils.c     |   37 ++++++++++++++++++++++++++++++++++++-

 I don't like the idea that we need to modify all Makefiles and always
 compile fileutils.c.

 It would be better to add close_{stream,stdout} functions to a
 separate include/closestream.h file as static inline functions.

> +int close_stream(FILE * stream)
> +{
> +	const int some_pending = (__fpending(stream) != 0);
> +	const int prev_fail = (ferror(stream) != 0);
> +	const int fclose_fail = (fclose(stream) != 0);
> +	if (prev_fail || (fclose_fail && (some_pending || errno != EBADF))) {
> +		if (!fclose_fail)
> +			errno = 0;
> +		return EOF;
> +	}
> +	return 0;
> +}
> +
> +/* Use atexit(); */
> +void close_stdout(void)
> +{
> +	if (close_stream(stdout) != 0 && !(errno == EPIPE)) {
> +		char const *write_error = _("write error");
> +		error(0, errno, "%s", write_error);

 Is error() really portable? We already have alternatives for err.h
 stuff, maybe it would be better to use:

 if (errno)
    warn(_("write error"));
 else
    warnx(_("write error"));

 or add alternative for error() to c.h.

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

       reply	other threads:[~2012-04-04 11:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAG27Bk2cNH-x6D=H4C6+NiHJDp4-_dWuU9BJuubTWFOcr-Bu9Q@mail.gmail.com>
2012-04-04 11:09 ` Karel Zak [this message]
2012-04-04 18:34   ` [pull] check stream status Sami Kerola
2012-04-11 10:56     ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120404110917.GN1084@x2.net.home \
    --to=kzak@redhat.com \
    --cc=kerolasa@gmail.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox