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

Amos Shapira amoss at huji.ac.il
Tue Aug 7 00:43:48 AEST 1990


Hello,

gg10 at prism.gatech.EDU (GALLOWAY) writes:

>So, how do they do it on a generic unix box?

Simple, don't know how much compatible it is, but there should be a routine
in the standard C library called "isatty(fd)" which will return 1 if fd
(a file descriptor) is a terminal and 0 otherwise.

If you don't have isatty(), it's way of operation is simple, it just does
an ioctl that only a terminal will accept without an error, like
"ioctl(fd, TIOGETP, &junk)" and if it returns a value less than a 0 then
fd is not a tty, otherwise it is. Not that it does a "GET*" operation, so
it doesn't brake anything.

Just for the sake of completeness, to convert from a file pointer (FILE *)
to a file descriptor (int), use fileno(fp).

Hope this helps,
Amos Shapira.
amoss at batata.huji.ac.il



More information about the Comp.lang.c mailing list