Re: [pull] check stream status

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
 http://karelzak.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux