Chapter 18. Low-Level Terminal Interface

This chapter describes functions that are specific to terminal devices. You can use these functions to do things like turn off input echoing; set serial line characteristics such as line speed and flow control; and change which characters are used for end-of-file, command-line editing, sending signals, and similar control functions.

Most of the functions in this chapter operate on file descriptors. Chapter 14, for more information about what a file descriptor is and how to open a file descriptor for a terminal device.

Identifying Terminals

The functions described in this chapter only work on files that correspond to terminal devices. You can find out whether a file descriptor is associated with a terminal by using the isatty function.

Prototypes for the functions in this section are declared in the header file unistd.h.

int function>isatty/function> (int filedes) This function returns 1 if filedes is a file descriptor associated with an open terminal device, and 0 otherwise.

If a file descriptor is associated with a terminal, you can get its associated file name using the ttyname function. See also the ctermid function, described in the section called “Identifying the Controlling Terminal”.

char * function>ttyname/function> (int filedes) If the file descriptor filedes is associated with a terminal device, the ttyname function returns a pointer to a statically-allocated, null-terminated string containing the file name of the terminal file. The value is a null pointer if the file descriptor isn't associated with a terminal, or the file name cannot be determined.

int function>ttyname_r/function> (int filedes, char *buf, size_t len) The ttyname_r function is similar to the ttyname function except that it places its result into the user-specified buffer starting at buf with length len.

The normal return value from ttyname_r is 0. Otherwise an error number is returned to indicate the error. The following errno error conditions are defined for this function:

EBADF

The filedes argument is not a valid file descriptor.

ENOTTY

The filedes is not associated with a terminal.

ERANGE

The buffer length len is too small to store the string to be returned.