6.4 Terminals: isatty()
The Linux/Unix standard input, standard output, standard error model discourages the special treatment of input and output devices. Programs generally should not need to know, or care, whether their output is a terminal, a file, a pipe, a network connection, a physical device, or whatever.
However, there are times when a program really does need to know what kind of a file a file descriptor is associated with. The stat() family of calls often provides enough information: regular file, directory, device, and so on. Sometimes, though, even that is not enough, and for interactive programs in particular, you may need to know if a file descriptor represents a tty.
A tty (short for Teletype, one of the early manufacturers of computer terminals) is any device that represents a terminal—that is, something that a human would use to interact with the computer. This may be either a hardware device, such as the keyboard and monitor of a personal computer, an old-fashioned video display terminal connected to a computer by a serial line or modem, or a software pseudoterminal, such as is used for windowing systems and network logins.
The discrimination can be made with isatty():
#include <unistd.h> POSIX int isatty(int desc);
This function returns 1 if the file descriptor desc represents a terminal and 0 otherwise. According to POSIX, isatty() may set errno to indicate an error; thus you should set errno to 0 before calling isatty() and then check its value if the return is 0. The POSIX standard also points out that isatty() returning 1 doesn’t mean there’s a human at the other end of the file descriptor!
One place where isatty() comes into use is in ls, in which the default is to print file names in columns if the standard output is a terminal and to print them one per line if not. Another is GNU grep’s --color option, which colorizes the matching text in the output if writing to a terminal.
There are a number of system calls and library functions for dealing specifically with terminals. We don’t cover them in this book for reasons of space.
