termio(7I) termio(7I)
NAME
termio - general terminal interface
SYNOPSIS
#include <termio.h>
ioctl(int fildes, int request, struct termio *arg);
ioctl(int fildes, int request, int arg);
#include <termios.h>
ioctl(int fildes, int request, struct termios *arg);
This release supports a general interface for asynchronous communications ports that is hardware-independent. The user interface to this
functionality is using function calls (the preferred interface) described in termios(3C) or ioctl commands described in this section. This
section also discusses the common features of the terminal subsystem which are relevant with both user interfaces.
When a terminal file is opened, it normally causes the process to wait until a connection is established. In practice, user programs seldom
open terminal files; they are opened by the system and become a user's standard input, output, and error files. The first terminal file
opened by the session leader that is not already associated with a session becomes the controlling terminal for that session. The control-
ling terminal plays a special role in handling quit and interrupt signals, as discussed below. The controlling terminal is inherited by a
child process during a fork(2). A process can break this association by changing its session using setsid() (see setsid(2)).
A terminal associated with one of these files ordinarily operates in full-duplex mode. Characters may be typed at any time, even while out-
put is occurring, and are only lost when the character input buffers of the system become completely full, which is rare. For example, the
number of characters in the line discipline buffer may exceed {MAX_CANON} and IMAXBEL (see below) is not set, or the user may accumulate
{ MAX_INPUT} number of input characters that have not yet been read by some program. When the input limit is reached, all the characters
saved in the buffer up to that point are thrown away without notice.
Session Management (Job Control)
A control terminal will distinguish one of the process groups in the session associated with it to be the foreground process group. All
other process groups in the session are designated as background process groups. This foreground process group plays a special role in han-
dling signal-generating input characters, as discussed below. By default, when a controlling terminal is allocated, the controlling
process's process group is assigned as foreground process group.
Background process groups in the controlling process's session are subject to a job control line discipline when they attempt to access
their controlling terminal. Process groups can be sent signals that will cause them to stop, unless they have made other arrangements. An
exception is made for members of orphaned process groups.
An orphaned process group is one where the process group (see getpgid(2)) has no members with a parent in a different process group but
sharing the same controlling terminal. When a member of an orphaned process group attempts to access its controlling terminal, EIO is
returned because there would be no way to restart the process if it were stopped on one of these signals.
If a member of a background process group attempts to read its controlling terminal, its process group will be sent a SIGTTIN signal,
which will normally cause the members of that process group to stop. If, however, the process is ignoring or holding SIGTTIN, or is a mem-
ber of an orphaned process group, the read will fail with errno set to EIO, and no signal is sent.
If a member of a background process group attempts to write its controlling terminal and the TOSTOP bit is set in the c_lflag field, its
process group is sent a SIGTTOU signal, which will normally cause the members of that process group to stop. If, however, the process is
ignoring or holding SIGTTOU, the write will succeed. If the process is not ignoring or holding SIGTTOU and is a member of an orphaned
process group, the write will fail with errno set to EIO, and no signal will be sent.
If TOSTOP is set and a member of a background process group attempts to ioctl its controlling terminal, and that ioctl will modify termi-
nal parameters (for example, TCSETA, TCSETAW, TCSETAF, or TIOCSPGRP), its process group will be sent a SIGTTOU signal, which will normally
cause the members of that process group to stop. If, however, the process is ignoring or holding SIGTTOU, the ioctl will succeed. If the
process is not ignoring or holding SIGTTOU and is a member of an orphaned process group, the write will fail with errno set to EIO, and no
signal will be sent.
Canonical Mode Input Processing
Normally, terminal input is processed in units of lines. A line is delimited by a newline (ASCII LF) character, an end-of-file (ASCII EOT)
character, or an end-of-line character. This means that a program attempting to read will block until an entire line has been typed. Also,
no matter how many characters are requested in the read call, at most one line will be returned. It is not necessary, however, to read a
whole line at once; any number of characters may be requested in a read, even one, without losing information.
During input, erase and kill processing is normally done. The ERASE character (by default, the character DEL) erases the last character
typed. The WERASE character (the character Control-w) erases the last "word" typed in the current input line (but not any preceding spaces
or tabs). A "word" is defined as a sequence of non-blank characters, with tabs counted as blanks. Neither ERASE nor WERASE will erase
beyond the beginning of the line. The KILL character (by default, the character NAK) kills (deletes) the entire input line, and optionally
outputs a newline character. All these characters operate on a key stroke basis, independent of any backspacing or tabbing that may have
been done. The REPRINT character (the character Control-r) prints a newline followed by all characters that have not been read. Reprinting
also occurs automatically if characters that would normally be erased from the screen are fouled by program output. The characters are
reprinted as if they were being echoed; consequencely, if ECHO is not set, they are not printed.
The ERASE and KILL characters may be entered literally by preceding them with the escape character. In this case, the escape character is
not read. The erase and kill characters may be changed.
Non-canonical Mode Input Processing
In non-canonical mode input processing, input characters are not assembled into lines, and erase and kill processing does not occur. The
MIN and TIME values are used to determine how to process the characters received.
MIN represents the minimum number of characters that should be received when the read is satisfied (that is, when the characters are
returned to the user). TIME is a timer of 0.10-second granularity that is used to timeout bursty and short-term data transmissions. The
four possible values for MIN and TIME and their interactions are described below.
Case A: MIN > 0, TIME > 0 In this case, TIME serves as an intercharacter timer and is activated after the first character is
received. Since it is an intercharacter timer, it is reset after a character is received. The interaction
between MIN and TIME is as follows: as soon as one character is received, the intercharacter timer is
started. If MIN characters are received before the intercharacter timer expires (note that the timer is
reset upon receipt of each character), the read is satisfied. If the timer expires before MIN characters
are received, the characters received to that point are returned to the user. Note that if TIME expires,
at least one character will be returned because the timer would not have been enabled unless a character
was received. In this case (MIN > 0, TIME > 0), the read sleeps until the MIN and TIME mechanisms are acti-
vated by the receipt of the first character. If the number of characters read is less than the number of
characters available, the timer is not reactivated and the subsequent read is satisfied immediately.
Case B: MIN > 0, TIME = 0 In this case, since the value of TIME is zero, the timer plays no role and only MIN is significant. A
pending read is not satisfied until MIN characters are received (the pending read sleeps until MIN charac-
ters are received). A program that uses this case to read record based terminal I/O may block indefinitely
in the read operation.
Case C: MIN = 0, TIME > 0 In this case, since MIN = 0, TIME no longer represents an intercharacter timer: it now serves as a read
timer that is activated as soon as a read is done. A read is satisfied as soon as a single character is
received or the read timer expires. Note that, in this case, if the timer expires, no character is
returned. If the timer does not expire, the only way the read can be satisfied is if a character is
received. In this case, the read will not block indefinitely waiting for a character; if no character is
received within TIME *.10 seconds after the read is initiated, the read returns with zero characters.
Case D: MIN = 0, TIME = 0 In this case, return is immediate. The minimum of either the number of characters requested or the number
of characters currently available is returned without waiting for more characters to be input.
Comparing Different Cases of MIN, TIME Interaction
Some points to note about MIN and TIME :
o In the following explanations, note that the interactions of MIN and TIME are not symmetric. For example, when MIN > 0 and TIME =
0, TIME has no effect. However, in the opposite case, where MIN = 0 and TIME > 0, both MIN and TIME play a role in that MIN is
satisfied with the receipt of a single character.
o Also note that in case A (MIN > 0, TIME > 0), TIME represents an intercharacter timer, whereas in case C ( MIN = 0, TIME > 0), TIME
represents a read timer.
These two points highlight the dual purpose of the MIN/TIME feature. Cases A and B, where MIN > 0, exist to handle burst mode activity
(for example, file transfer programs), where a program would like to process at least MIN characters at a time. In case A, the intercharac-
ter timer is activated by a user as a safety measure; in case B, the timer is turned off.
Cases C and D exist to handle single character, timed transfers. These cases are readily adaptable to screen-based applications that need
to know if a character is present in the input queue before refreshing the screen. In case C, the read is timed, whereas in case D, it is
not.
Another important note is that MIN is always just a minimum. It does not denote a record length. For example, if a program does a read of
20 bytes, MIN is 10, and 25 characters are present, then 20 characters will be returned to the user.
Writing Characters
When one or more characters are written, they are transmitted to the terminal as soon as previously written characters have finished typ-
ing. Input characters are echoed as they are typed if echoing has been enabled. If a process produces characters more rapidly than they
can be typed, it will be suspended when its output queue exceeds some limit. When the queue is drained down to some threshold, the program
is resumed.
Special Characters
Certain characters have special functions on input. These functions and their default character values are summarized as follows:
INTR (Control-c or ASCII ETX) generates a SIGINT signal. SIGINT is sent to all foreground processes associated with the control-
ling terminal. Normally, each such process is forced to terminate, but arrangements may be made either to ignore the signal
or to receive a trap to an agreed upon location. (See signal.h(3HEAD)).
QUIT (Control-| or ASCII FS) generates a SIGQUIT signal. Its treatment is identical to the interrupt signal except that, unless
a receiving process has made other arrangements, it will not only be terminated but a core image file (called core) will
be created in the current working directory.
ERASE (DEL) erases the preceding character. It does not erase beyond the start of a line, as delimited by a NL, EOF, EOL, or EOL2
character.
WERASE (Control-w or ASCII ETX) erases the preceding "word". It does not erase beyond the start of a line, as delimited by a NL,
EOF, EOL, or EOL2 character.
KILL (Control-u or ASCII NAK) deletes the entire line, as delimited by a NL, EOF, EOL, or EOL2 character.
REPRINT (Control-r or ASCII DC2) reprints all characters, preceded by a newline, that have not been read.
EOF (Control-d or ASCII EOT) may be used to generate an end-of-file from a terminal. When received, all the characters waiting
to be read are immediately passed to the program, without waiting for a newline, and the EOF is discarded. Thus, if no
characters are waiting (that is, the EOF occurred at the beginning of a line) zero characters are passed back, which is the
standard end-of-file indication. Unless escaped, the EOF character is not echoed. Because EOT is the default EOF character,
this prevents terminals that respond to EOT from hanging up.
NL (ASCII LF) is the normal line delimiter. It cannot be changed or escaped.
EOL (ASCII NULL) is an additional line delimiter, like NL . It is not normally used.
EOL2 is another additional line delimiter.
SWTCH (Control-z or ASCII EM) Header file symbols related to this special character are present for compatibility purposes only
and the kernel takes no special action on matching SWTCH (except to discard the character).
SUSP (Control-z or ASCII SUB) generates a SIGTSTP signal. SIGTSTP stops all processes in the foreground process group for that
terminal.
DSUSP (Control-y or ASCII EM). It generates a SIGTSTP signal as SUSP does, but the signal is sent when a process in the fore-
ground process group attempts to read the DSUSP character, rather than when it is typed.
STOP (Control-s or ASCII DC3) can be used to suspend output temporarily. It is useful with CRT terminals to prevent output from
disappearing before it can be read.
While output is suspended, STOP characters are ignored and not read.
START (Control-q or ASCII DC1) is used to resume output. Output has been suspended by a STOP character. While output is not sus-
pended, START characters are ignored and not read.
DISCARD (Control-o or ASCII SI) causes subsequent output to be discarded. Output is discarded until another DISCARD character is
typed, more input arrives, or the condition is cleared by a program.
LNEXT (Control-v or ASCII SYN) causes the special meaning of the next character to be ignored. This works for all the special
characters mentioned above. It allows characters to be input that would otherwise be interpreted by the system (for example
KILL, QUIT). The character values for INTR, QUIT, ERASE, WERASE, KILL, REPRINT, EOF, EOL, EOL2, SWTCH, SUSP, DSUSP, STOP,
START, DISCARD, and LNEXT may be changed to suit individual tastes. If the value of a special control character is
_POSIX_VDISABLE(0), the function of that special control character is disabled. The ERASE, KILL, and EOF characters may
be escaped by a preceding backslash () character, in which case no special function is done. Any of the special characters
may be preceded by the LNEXT character, in which case no special function is done.
Modem Disconnect
When a modem disconnect is detected, a SIGHUP signal is sent to the terminal's controlling process.
Unless other arrangements have been made, these signals cause the process to terminate. If SIGHUP is ignored or caught, any subsequent
read returns with an end-of-file indication until the terminal is closed.
If the controlling process is not in the foreground process group of the terminal, a SIGTSTP is sent to the terminal's foreground process
group. Unless other arrangements have been made, these signals cause the processes to stop.
Processes in background process groups that attempt to access the controlling terminal after modem disconnect while the terminal is still
allocated to the session will receive appropriate SIGTTOU and SIGTTIN signals. Unless other arrangements have been made, this signal
causes the processes to stop.
The controlling terminal will remain in this state until it is reinitialized with a successful open by the controlling process, or deallo-
cated by the controlling process.
Terminal Parameters
The parameters that control the behavior of devices and modules providing the termios interface are specified by the termios structure
defined by termios.h. Several ioctl(2) system calls that fetch or change these parameters use this structure that contains the following
members:
tcflag_t c_iflag; /* input modes */
tcflag_t c_oflag; /* output modes */
tcflag_t c_cflag; /* control modes */
tcflag_t c_lflag; /* local modes */
cc_t c_cc[NCCS]; /* control chars */
The special control characters are defined by the array c_cc. The symbolic name NCCS is the size of the Control-character array and is also
defined by <termios.h>. The relative positions, subscript names, and typical default values for each function are as follows:
+--------------------+--------------------+-----------------------+
|Relative Position | Subscript Name | Typical Default Value |
+--------------------+--------------------+-----------------------+
|0 |VINTR | ETX |
+--------------------+--------------------+-----------------------+
|1 |VQUIT | FS |
+--------------------+--------------------+-----------------------+
|2 |VERASE | DEL |
+--------------------+--------------------+-----------------------+
|3 |VKILL | NAK |
+--------------------+--------------------+-----------------------+
|4 |VEOF | EOT |
+--------------------+--------------------+-----------------------+
|5 |VEOL | NUL |
+--------------------+--------------------+-----------------------+
|6 |VEOL2 | NUL |
+--------------------+--------------------+-----------------------+
|7 |VWSTCH | NUL |
+--------------------+--------------------+-----------------------+
|8 |VSTART | NUL |
+--------------------+--------------------+-----------------------+
|9 |VSTOP | DC3 |
+--------------------+--------------------+-----------------------+
|10 |VSUSP | SUB |
+--------------------+--------------------+-----------------------+
|11 |VDSUSP | EM |
+--------------------+--------------------+-----------------------+
|12 |VREPRINT | DC2 |
+--------------------+--------------------+-----------------------+
|13 |VDISCARD | SI |
+--------------------+--------------------+-----------------------+
|14 |VWERASE | ETB |
+--------------------+--------------------+-----------------------+
|15 |VLNEXT | SYN |
+--------------------+--------------------+-----------------------+
|16-19 |Reserved | |
+--------------------+--------------------+-----------------------+
Input Modes
The c_iflag field describes the basic terminal input control:
IGNBRK Ignore break condition.
BRKINT Signal interrupt on break.
IGNPAR Ignore characters with parity errors.
PARMRK Mark parity errors.
INPCK Enable input parity check.
ISTRIP Strip character.
INLCR Map NL to CR on input.
IGNCR Ignore CR.
ICRNL Map CR to NL on input.
IUCLC Map upper-case to lower-case on input.
IXON Enable start/stop output control.
IXANY Enable any character to restart output.
IXOFF Enable start/stop input control.
IMAXBEL Echo BEL on input line too long.
If IGNBRK is set, a break condition (a character framing error with data all zeros) detected on input is ignored, that is, not put on the
input queue and therefore not read by any process. If IGNBRK is not set and BRKINT is set, the break condition shall flush the input and
output queues and if the terminal is the controlling terminal of a foreground process group, the break condition generates a single SIGINT
signal to that foreground process group. If neither IGNBRK nor BRKINT is set, a break condition is read as a single '