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

Brad Appleton brad at SSD.CSD.HARRIS.COM
Tue Aug 7 01:32:20 AEST 1990


In article <2072 at bimacs.BITNET> kfir at bimacs.biu.ac.il.UUCP (Yuval Kfir) writes:

(stuff deleted)

>I seem to remember a function called isatty(stdin). I am not sure it is
>standard. I hope this helps.                ^^^^^
>

Thats almost correct, the usage should be:

	isatty(STDIN);

where you have the following #defines

	#define STDIN  0
	#define STDOUT 1
	#define STDERR 2

You need to give it the file-descriptor number, not the file pointer!

Some compilers have the above #defines in their standard header files,
some dont! Also, On some Unix implementations (you will have to check yours),
calling isatty() can set errno to a non-zero value if isatty() returns FALSE.
This is due to the fact that some implementations of isatty() use ioctl()
to make a terminal-specific setting, if ioctl() fails, the file-descriptor
is NOT connected to a terminal.

Anyway, if you are on UNIX, isatty() is the way to go. If you are on another
system - look through your C-Compiler manual! Hope this helps!!

Just for the record - Peter Da Silva helped me figure out why errno
was being set!
______________________ "And miles to go before I sleep." ______________________
 Brad Appleton        brad at travis.ssd.csd.harris.com   Harris Computer Systems
                          ...!uunet!hcx1!brad          Fort Lauderdale, FL USA
~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~



More information about the Comp.lang.c mailing list