Re: [PATCH] Don't fflush(stdout) when it's not helpful
- From
Jeff King <peff@peff.net>
- Date
- Jun 29, 2007, 06:38 UTC
- Message-ID
- <20070629063819.GA23138@coredump.intra.peff.net>
- In-Reply-To
- <20070629034838.GF29279@thunk.org>
On Thu, Jun 28, 2007 at 11:48:38PM -0400, Theodore Tso wrote:
Show 20 quoted lines
> +void maybe_flush_or_die(FILE *f, const char *desc)
> +{
> + static int stdout_is_file = -1;
> + struct stat st;
> + char *cp;
> +
> + if (f == stdout) {
> + if (stdout_is_file < 0) {
> + cp = getenv("GIT_FLUSH");
> + if (cp)
> + stdout_is_file = (atoi(cp) == 0);
> + else if ((fstat(fileno(stdout), &st) == 0) &&
> + S_ISREG(st.st_mode))
> + stdout_is_file = 1;
> + else
> + stdout_is_file = 0;
> + }
> + if (stdout_is_file)
> + return;
> + }Looks much better to me, but I have one minor nit: stdout_is_file is a poor name, since it can mean either that stdout is a file, or that flushing was explicitly turned off. Naming it something like stdout_wants_flush would make much more sense. Though it's not a huge deal since the function is fairly short, I think it makes things a little easier to read (I had to double-check the negation on atoi(cp) == 0 before I convinced myself the code was correct).
-Peff