Terminals 1321
Note, however, that these events on their own are insufficient to change the actual
dimensions of the displayed window, which are controlled by software outside the
kernel (such as a window manager or a terminal emulator program).
Although not standardized in SUSv3, most UNIX implementations provide
access to the terminal window size using the ioctl() operations described in this section.
62.10 Terminal Identification
In Section 34.4, we described the ctermid() function, which returns the name of the
controlling terminal for a process (usually /dev/tty on UNIX systems). The func-
tions described in this section are also useful for identifying a terminal.
The isatty() function enables us to determine whether a file descriptor, fd, is
associated with a terminal (as opposed to some other file type).
The isatty() function is useful in editors and other screen-handling programs that
need to determine whether their standard input and output are directed to a terminal.
Given a file descriptor, the ttyname() function returns the name of the associ-
ated terminal device.
To find the name of the terminal, ttyname() uses the opendir() and readdir() func-
tions described in Section 18.8 to walk through the directories holding terminal
device names, looking at each directory entry until it finds one whose device ID (the
st_rdev field of the stat structure) matches that of the device referred to by the file
descriptor fd. Terminal device entries normally reside in two directories: /dev and
/dev/pts. The /dev directory contains entries for virtual consoles (e.g., /dev/tty1)
and BSD pseudoterminals. The /dev/pts directory contains entries for (System V-
style) pseudoterminal slave devices. (We describe pseudoterminals in Chapter 64.)
A reentrant version of ttyname() exists in the form of ttyname_r().
The tty(1) command, which displays the name of the terminal referred to
by its standard input, is the command-line analog of ttyname().
#include <unistd.h>
int isatty(int fd);
Returns true (1) if fd is associated with a terminal, otherwise false (0)
#include <unistd.h>
char *ttyname(int fd);
Returns pointer to (statically allocated) string containing
terminal name, or NULL on error