[OT]-ish: Can someone explain this?

Dirk Koopman djk at tobit.co.uk
Sat Jul 2 17:32:38 BST 2011

Here is a bit of C code that is part of something much bigger.

#include <stdio.h>
#include <unistd.h>

#define GIT_VERSION "1.23-dev"
#define GIT_COUNT "73"
#define GIT_ID "daa3ab8"

#define VERSION "1.23"

	printf("\nThingy version %s (%s-%s-%s) (built %s, %s)\n\n", VERSION, 
//	fflush(stdout);

If one compiles it (cc vtest.c -o vtest) and runs it at a shell prompt, 
one gets:

jim at mike2:~/thingy$ vtest

Thingy version 1.23 (1.23-dev-73-daa3ab8) (built 16:21:49, Jun 30 2011)

jim at mike2:~/thingy$

All is well and seemingly as expected. However now do this:

jim at mike2:~/thingy$ vtest | grep version
jim at mike2:~/thingy$

Stdout appears not to have been have been flushed. Which, after reading 
the man page for _exit is fair enough as it says: "The function _exit() 
terminates the calling process "immediately".  Any open file descriptors 
belonging to the process are closed". This is, in fact, what is wanted 
in the larger program. This behaviour has only come to light, after 
several years mind, because some (other) poor fool wanted to grab the 
version no for some shell script or other, instead of just checking what 
a user had installed via a shell prompt.

The fix is simply to fflush(stdout), before _exit().

But how come I get output at a shell prompt, and not down a pipe (or a 
redirection either)? What special magic is occurring here?


More information about the london.pm mailing list