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);
When a terminal file is opened, it normally causes the process to wait until a connection is established. In practice, users’ 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 controlling 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 getsid(2) ).
A terminal associated with one of these files ordinarily operates in full-duplex mode. Characters may be typed at any time, even while output is occurring, and are only lost when the character input buffers of the system become completely full, which is rare (for example, if the number of characters in the line discipline buffer exceeds {MAX_CANON} and IMAXBEL (see below) is not set), or when the user has accumulated {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.
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.
The operating system will not normally send SIGTSTP, SIGTTIN, or SIGTTOU. signals to a process that is a member of an orphaned process group.
These are process groups which do not have a member with a parent in another process group that is in the same session and therefore shares the same controlling terminal. When a member’s orphaned process group attempts to access its controlling terminal, errors will be returned. since there is no process to continue it if it should stop.
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 member of an orphaned process group, the read will fail with errno set to EIO , and no signal will be 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 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 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 terminal 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.
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.
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.
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 intercharacter 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.
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 \ 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.
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 deallocated by the controlling process.
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:
0 VINTR ETX 1 VQUIT FS 2 VERASE DEL 3 VKILL NAK 4 VEOF EOT 5 VEOL NUL 6 VEOL2 NUL 7 VSWTCH NUL 8 VSTART DC1 9 VSTOP DC3 10 VSUSP SUB 11 VDSUSP EM 12 VREPRINT DC2 13 VDISCARD SI 14 VWERASE ETB 15 VLNEXT SYN 16-19 reserved
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 ASCII NULL character (´\0´), or if PARMRK is set, as ´\377´, ´\0´, ´\0´.
If IGNPAR is set, a byte with framing or parity errors (other than break) is ignored.
If PARMRK is set, and IGNPAR is not set, a byte with a framing or parity error (other than break) is given to the application as the three-character sequence: ´\377´, ´\0´, X, where X is the data of the byte received in error. To avoid ambiguity in this case, if ISTRIP is not set, a valid character of ´\377´ is given to the application as ´\377´, ´\377´. If neither IGNPAR nor PARMRK is set, a framing or parity error (other than break) is given to the application as a single ASCII NULL character (´\0´).
If INPCK is set, input parity checking is enabled. If INPCK is not set, input parity checking is disabled. This allows output parity generation without input parity errors. Note that whether input parity checking is enabled or disabled is independent of whether parity detection is enabled or disabled. If parity detection is enabled but input parity checking is disabled, the hardware to which the terminal is connected will recognize the parity bit, but the terminal special file will not check whether this is set correctly or not.
If ISTRIP is set, valid input characters are first stripped to seven bits, otherwise all eight bits are processed.
If INLCR is set, a received NL character is translated into a CR character. If IGNCR is set, a received CR character is ignored (not read). Otherwise, if ICRNL is set, a received CR character is translated into a NL character.
If IUCLC is set, a received upper case, alphabetic character is translated into the corresponding lower case character.
If IXON is set, start/stop output control is enabled. A received STOP character suspends output and a received START character restarts output. The STOP and START characters will not be read, but will merely perform flow control functions. If IXANY is set, any input character restarts output that has been suspended.
If IXOFF is set, the system transmits a STOP character when the input queue is nearly full, and a START character when enough input has been read so that the input queue is nearly empty again.
If IMAXBEL is set, the ASCII BEL character is echoed if the input stream overflows. Further input is not stored, but any input already present in the input stream is not disturbed. If IMAXBEL is not set, no BEL character is echoed, and all input present in the input queue is discarded if the input stream overflows.
OPOST Post-process output. OLCUC Map lower case to upper on output. ONLCR Map NL to CR-NL on output. OCRNL Map CR to NL on output. ONOCR No CR output at column 0. ONLRET NL performs CR function. OFILL Use fill characters for delay. OFDEL Fill is DEL, else NULL. NLDLY Select newline delays: NL0 NL1 CRDLY Select carriage-return delays: CR0 CR1 CR2 CR3 TABDLY Select horizontal tab delays: TAB0 or tab expansion: TAB1 TAB2 TAB3 Expand tabs to spaces. XTABS Expand tabs to spaces. BSDLY Select backspace delays: BS0 BS1 VTDLY Select vertical tab delays: VT0 VT1 FFDLY Select form feed delays: FF0 FF1
If OPOST is set, output characters are post-processed as indicated by the remaining flags; otherwise, characters are transmitted without change.
If
OLCUC is set, a lower case alphabetic character is transmitted as the corresponding
upper case character. This function is often used in conjunction with IUCLC.
If ONLCR is set, the NL character is transmitted as the CR-NL character pair. If OCRNL is set, the CR character is transmitted as the NL character. If ONOCR is set, no CR character is transmitted when at column 0 (first position). If ONRET is set, the NL character is assumed to do the carriage-return function; the column pointer is set to 0 and the delays specified for CR are used. Otherwise, the NL character is assumed to do just the line-feed function; the column pointer remains unchanged. The column pointer is also set to 0 if the CR character is actually transmitted.
The delay bits specify how long transmission stops to allow for mechanical or other movement when certain characters are sent to the terminal. In all cases, a value of 0 indicates no delay. If OFILL is set, fill characters are transmitted for delay instead of a timed delay. This is useful for high baud rate terminals that need only a minimal delay. If OFDEL is set, the fill character is DEL ; otherwise it is NULL.
If a form-feed or vertical-tab delay is specified, it lasts for about 2 seconds.
Newline delay lasts about 0.10 seconds. If ONLRET is set, the carriage-return delays are used instead of the newline delays. If OFILL is set, two fill characters are transmitted.
Carriage-return delay type 1 is dependent on the current column position, type 2 is about 0.10 seconds, and type 3 is about 0.15 seconds. If OFILL is set, delay type 1 transmits two fill characters, and type 2 transmits four fill characters.
Horizontal-tab delay type 1 is dependent on the current column position. Type 2 is about 0.10 seconds. Type 3 specifies that tabs are to be expanded into spaces. If OFILL is set, two fill characters are transmitted for any delay.
Backspace delay lasts about 0.05 seconds. If OFILL is set, one fill character is transmitted.
The actual delays depend on line speed and system
load.
CBAUD Baud rate: B0 Hang up B50 50 baud B75 75 baud B110 110 baud B134 134 baud B150 150 baud B200 200 baud B300 300 baud B600 600 baud B1200 1200 baud B1800 1800 baud B2400 2400 baud B4800 4800 baud B9600 9600 baud B19200 19200 baud EXTA External A B38400 38400 baud EXTB External B B57600 57600 baud B76800 76800 baud B115200 115200 baud B153600 153600 baud B230400 230400 baud B307200 307200 baud B460800 460800 baud CSIZE Character size: CS5 5 bits CS6 6 bits CS7 7 bits CS8 8 bits CSTOPB Send two stop bits, else one CREAD Enable receiver PARENB Parity enable PARODD Odd parity, else even HUPCL Hang up on last close CLOCAL Local line, else dial-up CIBAUD Input baud rate, if different from output rate PAREXT Extended parity for mark and space parity CRTSXOFF Enable inbound hardware flow control CRTSCTS Enable outbound hardware flow control CBAUDEXT Bit to indicate output speed > B38400 CIBAUDEXT Bit to indicate input speed > B38400
The CBAUD bits together with the CBAUDEXT bit specify the output baud rate. To retrieve the output speed from the termios structure pointed to by termios_p see the following code segment.
unsigned long ospeed; if (termios_p->c_cflag & CBAUDEXT) ospeed = (termios_p->c_cflag & CBAUD) + CBAUD + 1; else ospeed = termios_p->c_cflag & CBAUD;
To store the output speed in the termios structure pointed to by termios_p see the following code segment.
unsigned long ospeed; if (ospeed > CBAUD) { termios_p->c_cflag |= CBAUDEXT; ospeed -= (CBAUD + 1); } else termios_p->c_cflag &= ~CBAUDEXT; termios_p->c_cflag = (termios_p->c_cflag & ~CBAUD) | (ospeed & CBAUD);
The zero baud rate, B0, is used to hang up the connection. If B0 is specified, the data-terminal-ready signal is not asserted. Normally, this disconnects the line.
If the CIBAUDEXT or CIBAUD bits are not zero, they specify the input baud rate, with the CBAUDEXT and CBAUD bits specifying the output baud rate; otherwise, the output and input baud rates are both specified by the CBAUDEXT and CBAUD bits. The values for the CIBAUD bits are the same as the values for the CBAUD bits, shifted left IBSHIFT bits. For any particular hardware, impossible speed changes are ignored. To retrieve the input speed in the termios structure pointed to by termios_p see the following code segment.
unsigned long ispeed; if (termios_p->c_cflag & CIBAUDEXT) ispeed = ((termios_p->c_cflag & CIBAUD) >> IBSHIFT) + (CIBAUD >> IBSHIFT) + 1; else ispeed = (termios_p->c_cflag & CIBAUD) >> IBSHIFT;
To store the input speed in the termios structure pointed to by termios_p see the following code segment.
unsigned long ispeed; if (ispeed == 0) { ispeed = termios_p->c_cflag & CBAUD; if (termios_p->c_cflag & CBAUDEXT) ispeed += (CBAUD + 1); } if ((ispeed << IBSHIFT) > CIBAUD) { termios_p->c_cflag |= CIBAUDEXT; ispeed -= ((CIBAUD >> IBSHIFT) + 1); } else termios_p->c_cflag &= ~CIBAUDEXT; termios_p->c_cflag = (termios_p->c_cflag & ~CIBAUD) | ((ispeed << IBSHIFT) & CIBAUD);
The CSIZE bits specify the character size in bits for both transmission and reception. This size does not include the parity bit, if any. If CSTOPB is set, two stop bits are used; otherwise, one stop bit is used. For example, at 110 baud, two stops bits are required.
If PARENB is set, parity generation and detection is enabled, and a parity bit is added to each character. If parity is enabled, the PARODD flag specifies odd parity if set; otherwise, even parity is used.
If CREAD is set, the receiver is enabled. Otherwise, no characters are received.
If HUPCL is set, the line is disconnected when the last process with the line open closes it or terminates. That is, the data-terminal-ready signal is not asserted.
If CLOCAL is set, the line is assumed to be a local, direct connection with no modem control; otherwise, modem control is assumed.
If CRTSCTS is set, inbound hardware flow control is enabled.
If CRTSCTS is set, outbound hardware flow control is enabled.
The four possible combinations for the state of CRTSCTS and CRTSXOFF bits and their interactions are described below.
ISIG Enable signals. ICANON Canonical input (erase and kill processing). XCASE Canonical upper/lower presentation. ECHO Enable echo. ECHOE Echo erase character as BS-SP-BS. ECHOK Echo NL after kill character. ECHONL Echo NL. NOFLSH Disable flush after interrupt or quit. TOSTOP Send SIGTTOU for background output. ECHOCTL Echo control characters as ^char, delete as ^?. ECHOPRT Echo erase character as character erased. ECHOKE BS-SP-BS erase entire line on line kill. FLUSHO Output is being flushed. PENDIN Retype pending input at next read or input character. IEXTEN Enable extended (implementation-defined) functions.
If ISIG is set, each input character is checked against the special control characters INTR, QUIT, SWTCH, SUSP, STATUS , and DSUSP . If an input character matches one of these control characters, the function associated with that character is performed. If ISIG is not set, no checking is done. Thus, these special input functions are possible only if ISIG is set.
If ICANON is set, canonical processing is enabled. This enables the erase and kill edit functions, and the assembly of input characters into lines delimited by NL, EOF, EOL , and EOL . If ICANON is not set, read requests are satisfied directly from the input queue. A read is not satisfied until at least MIN characters have been received or the timeout value TIME has expired between characters. This allows fast bursts of input to be read efficiently while still allowing single character input. The time value represents tenths of seconds.
If XCASE
is set, and if ICANON is set, an upper case letter is accepted on input
by preceding it with a \ character, and is output preceded by a \ character.
In this mode, the following escape sequences are generated on output and
accepted on input:
- for:
- use:
- ga
- \´
- |
- \!
- ~
- \^
- {
- \(
- }
- \)
- \
- \\
For example, A is input as \a, \n as \\n, and \N as \\\n.
If ECHO is set, characters are echoed as received.
When ICANON is set, the following echo functions are possible.
If ECHOCTL and IEXTEN are set, all control characters (characters with codes between 0 and 37 octal) other than ASCII TAB, ASCII NL , the START character, and the STOP character, ASCII CR , and ASCII BS are echoed as ^X, where X is the character given by adding 100 octal to the code of the control character (so that the character with octal code 1 is echoed as ^A), and the ASCII DEL character, with code 177 octal, is echoed as ^?.
If NOFLSH is set, the normal flush of the input and output queues associated with the INTR, QUIT , and SUSP characters is not done. This bit should be set when restarting system calls that read from or write to a terminal (see sigaction(2) ).
If TOSTOP and IEXTEN are set, the signal SIGTTOU is sent to a process that tries to write to its controlling terminal if it is not in the foreground process group for that terminal. This signal normally stops the process. Otherwise, the output generated by that process is output to the current output stream. Processes that are blocking or ignoring SIGTTOU signals are excepted and allowed to produce output, if any.
If FLUSHO and IEXTEN are set, data written to the terminal is discarded. This bit is set when the FLUSH character is typed. A program can cancel the effect of typing the FLUSH character by clearing FLUSHO.
If PENDIN and IEXTEN are set, any input that has not yet been read is reprinted when the next character arrives as input. PENDIN is then automatically cleared.
If IEXTEN is set, the following implementation-defined functions are enabled: special characters (WERASE, REPRINT, DISCARD, and LNEXT) and local flags (TOSTOP, ECHOCTL, ECHOPRT, ECHOKE, FLUSHO, and PENDIN).
The MIN and TIME values
are described above under Non-canonical mode input processing. The initial
value of MIN is 1, and the initial value of TIME is 0.
unsigned short ws_row; /* rows, in characters */ unsigned short ws_col; /* columns, in characters */ unsigned short ws_xpixel; /* horizontal size, in pixels */ unsigned short ws_ypixel; /* vertical size, in pixels */
unsigned short c_iflag; /* input modes */ unsigned short c_oflag; /* output modes */ unsigned short c_cflag; /* control modes */ unsigned short c_lflag; /* local modes */ char c_line; /* line discipline */ unsigned char c_cc[NCC]; /* control chars */
The special control characters are defined by the array c_cc. The symbolic
name NCC is the size of the control-character array and is also defined
by <termio.h>. The relative positions, subscript names, and typical default
values for each function are as follows:
0 VINTR EXT 1 VQUIT FS 2 VERASE DEL 3 VKILL NAK 4 VEOF EOT 5 VEOL NUL 6 VEOL2 NUL 7 reserved
The MIN values is stored in the VMIN element of the c_cc array; the TIME value is stored in the VTIME element of the c_cc array. The VMIN element is the same element as the VEOF element; the VTIME element is the same element as the VEOL element.
The calls that use the termio structure only affect the flags and control characters that can be stored in the termio structure; all other flags and control characters are unaffected.
TIOCM_LE line enable TIOCM_DTR data terminal ready TIOCM_RTS request to send TIOCM_ST secondary transmit TIOCM_SR secondary receive TIOCM_CTS clear to send TIOCM_CAR carrier detect TIOCM_RNG ring TIOCM_DSR data set ready
TIOCM_CD is a synonym for TIOCM_CAR, and TIOCM_RI is a synonym for TIOCM_RNG. Not all of these are necessarily supported by any particular device; check the manual page for the device in question.
If this property is undefined, the following termios modes are in effect. The initial input control value is BRKINT, ICRNL, IXON, IMAXBEL. The initial output control value is OPOST, ONLCR, TAB3. The initial hardware control value is B9600, CS8, CREAD. The initial line-discipline control value is ISIG, ICANON, IEXTEN, ECHO, ECHOK, ECHOE, ECHOKE, ECHOCTL.