NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ATTRIBUTES |STANDARDS |HISTORY |NOTES |BUGS |SEE ALSO |COLOPHON | |
termios(3) Library Functions Manualtermios(3)termios, tcgetattr, tcsetattr, tcsendbreak, tcdrain, tcflush, tcflow, cfmakeraw, cfgetospeed, cfgetispeed, cfsetispeed, cfsetospeed, cfsetspeed - get and set terminal attributes, line control, get and set baud rate
Standard C library (libc,-lc)
#include <termios.h>#include <unistd.h>int tcgetattr(intfd, struct termios *termios_p);int tcsetattr(intfd, intoptional_actions,const struct termios *termios_p);int tcsendbreak(intfd, intduration);int tcdrain(intfd);int tcflush(intfd, intqueue_selector);int tcflow(intfd, intaction);void cfmakeraw(struct termios *termios_p);speed_t cfgetispeed(const struct termios *termios_p);speed_t cfgetospeed(const struct termios *termios_p);int cfsetispeed(struct termios *termios_p, speed_tspeed);int cfsetospeed(struct termios *termios_p, speed_tspeed);int cfsetspeed(struct termios *termios_p, speed_tspeed); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):cfsetspeed(),cfmakeraw(): Since glibc 2.19: _DEFAULT_SOURCE glibc 2.19 and earlier: _BSD_SOURCE
The termios functions describe a general terminal interface that is provided to control asynchronous communications ports.The termios structure Many of the functions described here have atermios_p argument that is a pointer to atermios structure. This structure contains at least 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]; /* special characters */ The values that may be assigned to these fields are described below. In the case of the first four bit-mask fields, the definitions of some of the associated flags that may be set are exposed only if a specific feature test macro (seefeature_test_macros(7)) is defined, as noted in brackets ("[]"). In the descriptions below, "not in POSIX" means that the value is not specified in POSIX.1-2001, and "XSI" means that the value is specified in POSIX.1-2001 as part of the XSI extension.c_iflag flag constants:IGNBRKIgnore BREAK condition on input.BRKINTIfIGNBRKis set, a BREAK is ignored. If it is not set butBRKINTis set, then a BREAK causes the input and output queues to be flushed, and if the terminal is the controlling terminal of a foreground process group, it will cause aSIGINTto be sent to this foreground process group. When neitherIGNBRKnorBRKINTare set, a BREAK reads as a null byte ('\0'), except whenPARMRKis set, in which case it reads as the sequence \377 \0 \0.IGNPARIgnore framing errors and parity errors.PARMRKIf this bit is set, input bytes with parity or framing errors are marked when passed to the program. This bit is meaningful only whenINPCKis set andIGNPARis not set. The way erroneous bytes are marked is with two preceding bytes, \377 and \0. Thus, the program actually reads three bytes for one erroneous byte received from the terminal. If a valid byte has the value \377, andISTRIP(see below) is not set, the program might confuse it with the prefix that marks a parity error. Therefore, a valid byte \377 is passed to the program as two bytes, \377 \377, in this case. If neitherIGNPARnorPARMRKis set, read a character with a parity error or framing error as \0.INPCKEnable input parity checking.ISTRIPStrip off eighth bit.INLCRTranslate NL to CR on input.IGNCRIgnore carriage return on input.ICRNLTranslate carriage return to newline on input (unlessIGNCR is set).IUCLC(not in POSIX) Map uppercase characters to lowercase on input.IXONEnable XON/XOFF flow control on output.IXANY(XSI) Typing any character will restart stopped output. (The default is to allow just the START character to restart output.)IXOFFEnable XON/XOFF flow control on input.IMAXBEL (not in POSIX) Ring bell when input queue is full. Linux does not implement this bit, and acts as if it is always set.IUTF8(since Linux 2.6.4) (not in POSIX) Input is UTF8; this allows character-erase to be correctly performed in cooked mode.c_oflag flag constants:OPOSTEnable implementation-defined output processing.OLCUC(not in POSIX) Map lowercase characters to uppercase on output.ONLCR(XSI) Map NL to CR-NL on output.OCRNLMap CR to NL on output.ONOCRDon't output CR at column 0.ONLRETThe NL character is assumed to do the carriage-return function; the kernel's idea of the current column is set to 0 after both NL and CR.OFILLSend fill characters for a delay, rather than using a timed delay.OFDELFill character is ASCII DEL (0177). If unset, fill character is ASCII NUL ('\0'). (Not implemented on Linux.)NLDLYNewline delay mask. Values areNL0andNL1. [requires_BSD_SOURCEor_SVID_SOURCEor_XOPEN_SOURCE]CRDLYCarriage return delay mask. Values areCR0,CR1,CR2, orCR3. [requires_BSD_SOURCEor_SVID_SOURCEor_XOPEN_SOURCE]TABDLYHorizontal tab delay mask. Values areTAB0,TAB1,TAB2,TAB3(orXTABS, but see theBUGSsection). A value of TAB3, that is, XTABS, expands tabs to spaces (with tab stops every eight columns). [requires_BSD_SOURCEor_SVID_SOURCEor_XOPEN_SOURCE]BSDLYBackspace delay mask. Values areBS0orBS1. (Has never been implemented.) [requires_BSD_SOURCEor_SVID_SOURCE or_XOPEN_SOURCE]VTDLYVertical tab delay mask. Values areVT0orVT1.FFDLYForm feed delay mask. Values areFF0orFF1. [requires_BSD_SOURCEor_SVID_SOURCEor_XOPEN_SOURCE]c_cflag flag constants:CBAUD(not in POSIX) Baud speed mask (4+1 bits). [requires_BSD_SOURCEor_SVID_SOURCE]CBAUDEX (not in POSIX) Extra baud speed mask (1 bit), included inCBAUD. [requires_BSD_SOURCEor_SVID_SOURCE] (POSIX says that the baud speed is stored in thetermios structure without specifying where precisely, and providescfgetispeed() andcfsetispeed() for getting at it. Some systems use bits selected byCBAUDinc_cflag, other systems use separate fields, for example,sg_ispeed andsg_ospeed.)CSIZECharacter size mask. Values areCS5,CS6,CS7, orCS8.CSTOPBSet two stop bits, rather than one.CREADEnable receiver.PARENBEnable parity generation on output and parity checking for input.PARODDIf set, then parity for input and output is odd; otherwise even parity is used.HUPCLLower modem control lines after last process closes the device (hang up).CLOCALIgnore modem control lines.LOBLK(not in POSIX) Block output from a noncurrent shell layer. For use byshl(shell layers). (Not implemented on Linux.)CIBAUD(not in POSIX) Mask for input speeds. The values for theCIBAUDbits are the same as the values for theCBAUDbits, shifted leftIBSHIFTbits. [requires_BSD_SOURCEor_SVID_SOURCE] (Not implemented in glibc, supported on Linux viaTCGET* andTCSET* ioctls; seeioctl_tty(2))CMSPAR(not in POSIX) Use "stick" (mark/space) parity (supported on certain serial devices): ifPARODDis set, the parity bit is always 1; ifPARODDis not set, then the parity bit is always 0. [requires_BSD_SOURCEor_SVID_SOURCE]CRTSCTS (not in POSIX) Enable RTS/CTS (hardware) flow control. [requires_BSD_SOURCEor_SVID_SOURCE]c_lflag flag constants:ISIGWhen any of the characters INTR, QUIT, SUSP, or DSUSP are received, generate the corresponding signal.ICANONEnable canonical mode (described below).XCASE(not in POSIX; not supported under Linux) IfICANONis also set, terminal is uppercase only. Input is converted to lowercase, except for characters preceded by \. On output, uppercase characters are preceded by \ and lowercase characters are converted to uppercase. [requires_BSD_SOURCEor_SVID_SOURCEor_XOPEN_SOURCE]ECHOEcho input characters.ECHOEIfICANONis also set, the ERASE character erases the preceding input character, and WERASE erases the preceding word.ECHOKIfICANONis also set, the KILL character erases the current line.ECHONLIfICANONis also set, echo the NL character even if ECHO is not set.ECHOCTL (not in POSIX) IfECHOis also set, terminal special characters other than TAB, NL, START, and STOP are echoed as^X, where X is the character with ASCII code 0x40 greater than the special character. For example, character 0x08 (BS) is echoed as^H. [requires_BSD_SOURCEor_SVID_SOURCE]ECHOPRT (not in POSIX) IfICANONandECHOare also set, characters are printed as they are being erased. [requires_BSD_SOURCEor_SVID_SOURCE]ECHOKE(not in POSIX) IfICANONis also set, KILL is echoed by erasing each character on the line, as specified byECHOE andECHOPRT. [requires_BSD_SOURCEor_SVID_SOURCE]DEFECHO (not in POSIX) Echo only when a process is reading. (Not implemented on Linux.)FLUSHO(not in POSIX; not supported under Linux) Output is being flushed. This flag is toggled by typing the DISCARD character. [requires_BSD_SOURCEor_SVID_SOURCE]NOFLSHDisable flushing the input and output queues when generating signals for the INT, QUIT, and SUSP characters.TOSTOPSend theSIGTTOUsignal to the process group of a background process which tries to write to its controlling terminal.PENDIN(not in POSIX; not supported under Linux) All characters in the input queue are reprinted when the next character is read. (bash(1) handles typeahead this way.) [requires_BSD_SOURCEor_SVID_SOURCE]IEXTENEnable implementation-defined input processing. This flag, as well asICANONmust be enabled for the special characters EOL2, LNEXT, REPRINT, WERASE to be interpreted, and for theIUCLCflag to be effective. Thec_cc array defines the terminal special characters. The symbolic indices (initial values) and meaning are:VDISCARD (not in POSIX; not supported under Linux; 017, SI, Ctrl-O) Toggle: start/stop discarding pending output. Recognized whenIEXTENis set, and then not passed as input.VDSUSP(not in POSIX; not supported under Linux; 031, EM, Ctrl-Y) Delayed suspend character (DSUSP): sendSIGTSTPsignal when the character is read by the user program. Recognized whenIEXTENandISIGare set, and the system supports job control, and then not passed as input.VEOF(004, EOT, Ctrl-D) End-of-file character (EOF). More precisely: this character causes the pending tty buffer to be sent to the waiting user program without waiting for end-of-line. If it is the first character of the line, theread(2) in the user program returns 0, which signifies end- of-file. Recognized whenICANONis set, and then not passed as input.VEOL(0, NUL) Additional end-of-line character (EOL). Recognized whenICANONis set.VEOL2(not in POSIX; 0, NUL) Yet another end-of-line character (EOL2). Recognized whenICANONis set.VERASE(0177, DEL, rubout, or 010, BS, Ctrl-H, or also #) Erase character (ERASE). This erases the previous not-yet-erased character, but does not erase past EOF or beginning-of- line. Recognized whenICANONis set, and then not passed as input.VINTR(003, ETX, Ctrl-C, or also 0177, DEL, rubout) Interrupt character (INTR). Send aSIGINTsignal. Recognized whenISIGis set, and then not passed as input.VKILL(025, NAK, Ctrl-U, or Ctrl-X, or also @) Kill character (KILL). This erases the input since the last EOF or beginning-of-line. Recognized whenICANONis set, and then not passed as input.VLNEXT(not in POSIX; 026, SYN, Ctrl-V) Literal next (LNEXT). Quotes the next input character, depriving it of a possible special meaning. Recognized whenIEXTENis set, and then not passed as input.VMINMinimum number of characters for noncanonical read (MIN).VQUIT(034, FS, Ctrl-\) Quit character (QUIT). SendSIGQUIT signal. Recognized whenISIGis set, and then not passed as input.VREPRINT (not in POSIX; 022, DC2, Ctrl-R) Reprint unread characters (REPRINT). Recognized whenICANONandIEXTENare set, and then not passed as input.VSTART(021, DC1, Ctrl-Q) Start character (START). Restarts output stopped by the Stop character. Recognized whenIXON is set, and then not passed as input.VSTATUS (not in POSIX; not supported under Linux; status request: 024, DC4, Ctrl-T). Status character (STATUS). Display status information at terminal, including state of foreground process and amount of CPU time it has consumed. Also sends aSIGINFOsignal (not supported on Linux) to the foreground process group.VSTOP(023, DC3, Ctrl-S) Stop character (STOP). Stop output until Start character typed. Recognized whenIXONis set, and then not passed as input.VSUSP(032, SUB, Ctrl-Z) Suspend character (SUSP). SendSIGTSTP signal. Recognized whenISIGis set, and then not passed as input.VSWTCH(not in POSIX; not supported under Linux; 0, NUL) Switch character (SWTCH). Used in System V to switch shells inshell layers, a predecessor to shell job control.VTIMETimeout in deciseconds for noncanonical read (TIME).VWERASE (not in POSIX; 027, ETB, Ctrl-W) Word erase (WERASE). Recognized whenICANONandIEXTENare set, and then not passed as input. An individual terminal special character can be disabled by setting the value of the correspondingc_cc element to_POSIX_VDISABLE. The above symbolic subscript values are all different, except thatVTIME,VMINmay have the same value asVEOL,VEOF, respectively. In noncanonical mode the special character meaning is replaced by the timeout meaning. For an explanation ofVMINandVTIME, see the description of noncanonical mode below.Retrieving and changing terminal settingstcgetattr() gets the parameters associated with the object referred byfd and stores them in thetermios structure referenced bytermios_p. This function may be invoked from a background process; however, the terminal attributes may be subsequently changed by a foreground process.tcsetattr() sets the parameters associated with the terminal (unless support is required from the underlying hardware that is not available) from thetermios structure referred to bytermios_p.optional_actions specifies when the changes take effect:TCSANOW the change occurs immediately.TCSADRAIN the change occurs after all output written tofd has been transmitted. This option should be used when changing parameters that affect output.TCSAFLUSH the change occurs after all output written to the object referred byfd has been transmitted, and all input that has been received but not read will be discarded before the change is made.Canonical and noncanonical mode The setting of theICANONcanon flag inc_lflag determines whether the terminal is operating in canonical mode (ICANONset) or noncanonical mode (ICANONunset). By default,ICANONis set. In canonical mode: • Input is made available line by line. An input line is available when one of the line delimiters is typed (NL, EOL, EOL2; or EOF at the start of line). Except in the case of EOF, the line delimiter is included in the buffer returned byread(2). • Line editing is enabled (ERASE, KILL; and if theIEXTENflag is set: WERASE, REPRINT, LNEXT). Aread(2) returns at most one line of input; if theread(2) requested fewer bytes than are available in the current line of input, then only as many bytes as requested are read, and the remaining characters will be available for a futureread(2). • The maximum line length is 4096 chars (including the terminating newline character); lines longer than 4096 chars are truncated. After 4095 characters, input processing (e.g.,ISIGandECHO*processing) continues, but any input data after 4095 characters up to (but not including) any terminating newline is discarded. This ensures that the terminal can always receive more input until at least one line can be read. In noncanonical mode input is available immediately (without the user having to type a line-delimiter character), no input processing is performed, and line editing is disabled. The read buffer will only accept 4095 chars; this provides the necessary space for a newline char if the input mode is switched to canonical. The settings of MIN (c_cc[VMIN]) and TIME (c_cc[VTIME]) determine the circumstances in which aread(2) completes; there are four distinct cases: MIN == 0, TIME == 0 (polling read) If data is available,read(2) returns immediately, with the lesser of the number of bytes available, or the number of bytes requested. If no data is available,read(2) returns 0. MIN > 0, TIME == 0 (blocking read)read(2) blocks until MIN bytes are available, and returns up to the number of bytes requested. MIN == 0, TIME > 0 (read with timeout) TIME specifies the limit for a timer in tenths of a second. The timer is started whenread(2) is called.read(2) returns either when at least one byte of data is available, or when the timer expires. If the timer expires without any input becoming available,read(2) returns 0. If data is already available at the time of the call toread(2), the call behaves as though the data was received immediately after the call. MIN > 0, TIME > 0 (read with interbyte timeout) TIME specifies the limit for a timer in tenths of a second. Once an initial byte of input becomes available, the timer is restarted after each further byte is received.read(2) returns when any of the following conditions is met: • MIN bytes have been received. • The interbyte timer expires. • The number of bytes requested byread(2) has been received. (POSIX does not specify this termination condition, and on some other implementationsread(2) does not return in this case.) Because the timer is started only after the initial byte becomes available, at least one byte will be read. If data is already available at the time of the call toread(2), the call behaves as though the data was received immediately after the call. POSIX does not specify whether the setting of theO_NONBLOCKfile status flag takes precedence over the MIN and TIME settings. IfO_NONBLOCKis set, aread(2) in noncanonical mode may return immediately, regardless of the setting of MIN or TIME. Furthermore, if no data is available, POSIX permits aread(2) in noncanonical mode to return either 0, or -1 witherrno set toEAGAIN.Raw modecfmakeraw() sets the terminal to something like the "raw" mode of the old Version 7 terminal driver: input is available character by character, echoing is disabled, and all special processing of terminal input and output characters is disabled. The terminal attributes are set as follows: termios_p->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); termios_p->c_oflag &= ~OPOST; termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); termios_p->c_cflag &= ~(CSIZE | PARENB); termios_p->c_cflag |= CS8;Line controltcsendbreak() transmits a continuous stream of zero-valued bits for a specific duration, if the terminal is using asynchronous serial data transmission. Ifduration is zero, it transmits zero- valued bits for at least 0.25 seconds, and not more than 0.5 seconds. Ifduration is not zero, it sends zero-valued bits for some implementation-defined length of time. If the terminal is not using asynchronous serial data transmission,tcsendbreak() returns without taking any action.tcdrain() waits until all output written to the object referred to byfd has been transmitted.tcflush() discards data written to the object referred to byfd but not transmitted, or data received but not read, depending on the value ofqueue_selector:TCIFLUSH flushes data received but not read.TCOFLUSH flushes data written but not transmitted.TCIOFLUSH flushes both data received but not read, and data written but not transmitted.tcflow() suspends transmission or reception of data on the object referred to byfd, depending on the value ofaction:TCOOFFsuspends output.TCOONrestarts suspended output.TCIOFFtransmits a STOP character, which stops the terminal device from transmitting data to the system.TCIONtransmits a START character, which starts the terminal device transmitting data to the system. The default on open of a terminal file is that neither its input nor its output is suspended.Line speed The baud rate functions are provided for getting and setting the values of the input and output baud rates in thetermios structure. The new values do not take effect untiltcsetattr() is successfully called. Setting the speed toB0instructs the modem to "hang up". The actual bit rate corresponding toB38400may be altered withsetserial(8). The input and output baud rates are stored in thetermios structure.cfgetospeed() returns the output baud rate stored in thetermios structure pointed to bytermios_p.cfsetospeed() sets the output baud rate stored in thetermios structure pointed to bytermios_p tospeed, which must be one of these constants:B0B50B75B110B134B150B200B300B600B1200B1800B2400B4800B9600B19200B38400B57600B115200B230400B460800B500000B576000B921600B1000000B1152000B1500000B2000000 These constants are additionally supported on the SPARC architecture:B76800B153600B307200B614400 These constants are additionally supported on non-SPARC architectures:B2500000B3000000B3500000B4000000 Due to differences between architectures, portable applications should check if a particularBnnn constant is defined prior to using it. The zero baud rate,B0, is used to terminate the connection. IfB0is specified, the modem control lines shall no longer be asserted. Normally, this will disconnect the line.CBAUDEXis a mask for the speeds beyond those defined in POSIX.1 (57600 and above). Thus,B57600&CBAUDEXis nonzero. Setting the baud rate to a value other than those defined byBnnn constants is possible via theTCSETS2ioctl; seeioctl_tty(2).cfgetispeed() returns the input baud rate stored in thetermios structure.cfsetispeed() sets the input baud rate stored in thetermios structure tospeed, which must be specified as one of theBnnn constants listed above forcfsetospeed(). If the input baud rate is set to the literal constant0(not the symbolic constantB0), the input baud rate will be equal to the output baud rate.cfsetspeed() is a 4.4BSD extension. It takes the same arguments ascfsetispeed(), and sets both input and output speed.cfgetispeed() returns the input baud rate stored in thetermios structure.cfgetospeed() returns the output baud rate stored in thetermios structure. All other functions return:0on success.-1on failure and seterrno to indicate the error. Note thattcsetattr() returns success ifany of the requested changes could be successfully carried out. Therefore, when making multiple changes it may be necessary to follow this call with a further call totcgetattr() to check that all changes have been performed successfully.
For an explanation of the terms used in this section, seeattributes(7). ┌──────────────────────────────────────┬───────────────┬─────────┐ │Interface│Attribute│Value│ ├──────────────────────────────────────┼───────────────┼─────────┤ │tcgetattr(),tcsetattr(),tcdrain(), │ Thread safety │ MT-Safe │ │tcflush(),tcflow(),tcsendbreak(), │ │ │ │cfmakeraw(),cfgetispeed(), │ │ │ │cfgetospeed(),cfsetispeed(), │ │ │ │cfsetospeed(),cfsetspeed() │ │ │ └──────────────────────────────────────┴───────────────┴─────────┘
tcgetattr()tcsetattr()tcsendbreak()tcdrain()tcflush()tcflow()cfgetispeed()cfgetospeed()cfsetispeed()cfsetospeed() POSIX.1-2008.cfmakeraw()cfsetspeed() BSD.
tcgetattr()tcsetattr()tcsendbreak()tcdrain()tcflush()tcflow()cfgetispeed()cfgetospeed()cfsetispeed()cfsetospeed() POSIX.1-2001.cfmakeraw()cfsetspeed() BSD.
UNIX V7 and several later systems have a list of baud rates where after the valuesB0throughB9600one finds the two constantsEXTA,EXTB("External A" and "External B"). Many systems extend the list with much higher baud rates. The effect of a nonzeroduration withtcsendbreak() varies. SunOS specifies a break ofduration * N seconds, whereN is at least 0.25, and not more than 0.5. Linux, AIX, DU, Tru64 send a break ofduration milliseconds. FreeBSD and NetBSD and HP-UX and MacOS ignore the value ofduration. Under Solaris and UnixWare,tcsendbreak() with nonzeroduration behaves liketcdrain().On the Alpha architecture before Linux 4.16 (and glibc before glibc 2.28), theXTABSvalue was different fromTAB3and it was ignored by theN_TTYline discipline code of the terminal driver as a result (because as it wasn't part of theTABDLYmask).
reset(1),setterm(1),stty(1),tput(1),tset(1),tty(1),ioctl_console(2),ioctl_tty(2),cc_t(3type),speed_t(3type),tcflag_t(3type),setserial(8)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17termios(3)Pages that refer to this page:_exit(2), FIONREAD(2const), ioctl_console(2), ioctl_tty(2), setpgid(2), TCSETS(2const), TCXONC(2const), cc_t(3type), curs_inopts(3x), getpass(3), stdin(3), tty(4), attributes(7), credentials(7), pty(7), signal-safety(7), termio(7), agetty(8)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |