How do you tell if stdin/stdout is a terminal?

Peter da Silva peter at ficc.ferranti.com
Tue Aug 7 06:08:44 AEST 1990


This is touched on in the latest FAQ for comp.unix.questions, under "how
do I tell if my program is running in the background". It is not a C
problem, but a UNIX problem. However, here are the answers.

In article <12210 at hydra.gatech.EDU> gg10 at prism.gatech.EDU (GALLOWAY) writes:
> If I have a program named "foo" which can accept standard input if no file
> name exists on the command line, and the user just types "foo", I want to
> display usage.

	if(isatty(fileno(stdin))) {
		usage();
		exit(0);
	}

> It would also be nice, if the output of "foo" was no ASCII to display a
> message like "foo: output must not be terminal".

	if(isatty(fileno(stdout))) {
		user_error();
		usage();
		exit(2);
	}
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
<peter at ficc.ferranti.com>



More information about the Comp.lang.c mailing list