TCSETATTR(3POSIX)
π TCSETATTR β ok bestie, we're setting terminal params π
so this is tcsetattr(3POSIX), straight out of the POSIX Programmer's Manual. and before we even get into it, a lil heads up from the PROLOG:
this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface might not even be implemented on Linux at all. π
β¨ NAME β¨
tcsetattr β set the parameters associated with the terminal. that's it, that's the vibe. π
πΈ SYNOPSIS πΈ
#include <termios.h>
int tcsetattr(int fildes, int optional_actions,
const struct termios *termios_p);
π¦ DESCRIPTION π¦
okay so tcsetattr() sets the parameters associated with the terminal referred to by the open file descriptor fildes (and yeah, it's gotta be an open file descriptor associated with a terminal, no faking it π
), pulling the settings from the termios structure that termios_p points at. here's how it goes depending on what you pass:
- if
optional_actionsisTCSANOW, the change happens immediately. no waiting, main character energy, periodt. β¨ - if
optional_actionsisTCSADRAIN, the change happens after all output written tofildesis transmitted. this is the one you want when you're changing parameters that affect output, bestie. take the note, it's free. - if
optional_actionsisTCSAFLUSH, the change happens after all output written tofildesis transmitted, and all input so far received but not read gets discarded before the change is made. bye bye unread input π«Ά
now, baud rate tea β:
if the output baud rate stored in the termios structure that termios_p points to is the zero baud rate, B0, then the modem control lines shall no longer be asserted. normally, this shall disconnect the line. so like⦠know what you're doing lol.
if the input baud rate stored in that same termios structure is 0, then the input baud rate given to the hardware is the same as the output baud rate stored in the termios structure. cute, right? they match. π
π«Ά the "we tried our best" rules π«Ά
tcsetattr() shall return successfully if it was able to perform any of the requested actions β even if some of the requested actions could not be performed. it's giving partial credit, and honestly? valid.
it shall set all the attributes that the implementation supports as requested, and leave all the attributes not supported by the implementation unchanged.
if no part of the request can be honored, it shall return -1 and set errno to [EINVAL]. π
if the input and output baud rates differ and are a combination that is not supported, then neither baud rate shall be changed. all or nothing, no half measures π .
a subsequent call to tcgetattr() shall return the actual state of the terminal device β reflecting both the changes made and the changes not made in the previous tcsetattr() call. so like, always check. and heads up: tcsetattr() shall not change the values found in the termios structure under any circumstances. nope. not even a little.
β οΈ the effect oftcsetattr()is undefined if the value of thetermiosstructure pointed to bytermios_pwas not derived from the result of a call totcgetattr()onfildes. an application should modify only fields and flags defined by this volume of POSIX.1β2017 between the call totcgetattr()andtcsetattr(), leaving all other fields and flags unmodified. don't freestyle it bestie, this is not the moment for creativity.
π who's allowed to change the terminal π
no actions defined by this volume of POSIX.1β2017 β other than a call to tcsetattr(), a close of the last file descriptor in the system associated with this terminal device, or an open of the first file descriptor in the system associated with this terminal device (using the O_TTY_INIT flag if it's non-zero and the device is not a pseudo-terminal) β shall cause any of the terminal attributes defined by this volume of POSIX.1β2017 to change. so like, the terminal attributes are lowkey locked down except for those specific moments. iconic gatekeeping honestly π₯
π background process group drama π
if tcsetattr() is called from a process which is a member of a background process group on a fildes associated with its controlling terminal:
- if the calling thread is blocking
SIGTTOUsignals or the process is ignoringSIGTTOUsignals, the operation completes normally and no signal is sent. slay, smooth sailing. π¦ - otherwise, a
SIGTTOUsignal shall be sent to the process group. π the terminal said "not while you're in the background, hun."
β RETURN VALUE β
upon successful completion, 0 shall be returned. otherwise, -1 shall be returned and errno set to indicate the error. simple as that π
π₯ ERRORS π₯
the tcsetattr() function shall fail if:
EBADFβ thefildesargument is not a valid file descriptor.EINTRβ a signal interruptedtcsetattr().EINVALβ theoptional_actionsargument is not a supported value, or an attempt was made to change an attribute represented in thetermiosstructure to an unsupported value.EIOβ the process group of the writing process is orphaned, the calling thread is not blockingSIGTTOU, and the process is not ignoringSIGTTOU.ENOTTYβ the file associated withfildesis not a terminal. bestie. that's not a terminal. π«
the following sections are informative.
π EXAMPLES π
None. that's the whole section. it's giving nothing and honestly we respect the honesty. π
π APPLICATION USAGE π
if you're trying to change baud rates, applications should call tcsetattr() and then call tcgetattr() in order to figure out what baud rates were actually selected. because sometimes the hardware has opinions and you gotta ask it what it actually did. π
in general, there are two reasons an application would want to change the parameters associated with a terminal device:
- the device already has working parameter settings but the application needs a different behavior, like non-canonical mode instead of canonical mode. the application sets (or clears) only a few flags or
c_cc[]values. typically, the terminal device in this case is either the controlling terminal for the process or a pseudo-terminal. light touch, minimal edits, we love her. β¨ - the device is a modem or similar piece of equipment connected by a serial line, and it was not open before the application opened it. in this case, the application needs to initialize all of the parameter settings "from scratch". however, since the
termiosstructure may include both standard and non-standard parameters, the application cannot just initialize the whole structure in an arbitrary way (e.g., usingmemset()) β because that may cause some of the non-standard parameters to be set incorrectly, resulting in non-conforming behavior of the terminal device. conversely, the application cannot just set the standard parameters and assume the non-standard parameters will already have suitable values, because the device might previously have been used with non-conforming parameter settings (and some implementations retain the settings from one use to the next π). the solution? open the terminal device using theO_TTY_INITflag to initialize the terminal device to have conforming parameter settings, obtain those settings usingtcgetattr(), and then set all of the standard parameters to the desired settings. it's a whole ritual and you gotta do it in order, no skipping steps π
π§ RATIONALE π§
the tcsetattr() function can be interrupted in the following situations:
- it is interrupted while waiting for output to drain.
- it is called from a process in a background process group and
SIGTTOUis caught.
also go read the RATIONALE section in tcgetattr(), there's more lore there π
π FUTURE DIRECTIONS π
using an input baud rate of 0 to set the input rate equal to the output rate may not necessarily be supported in a future version of this volume of POSIX.1β2017. so likeβ¦ enjoy it while it lasts bestie, it's on the way out π₯²
πΈ SEE ALSO πΈ
cfgetispeed(), tcgetattr()
the Base Definitions volume of POSIX.1β2017, Chapter 11, General Terminal Interface, <termios.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! brought to you by IEEE/The Open Group, 2017 β mwah, kiss kiss, go set some terminal attributes responsibly πβ¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms