TCSETATTR(3)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

๐Ÿ’… TCSETATTR(3) โ€” termios but make it make sense ๐Ÿ’…

ok so bestie, welcome to TCSETATTR(3), from the Library Functions Manual โœจ this one lives in the Standard C Library (libc, -lc) and it's literally the whole squad of functions that let you poke at the termios structure โ€” the thing that holds all your terminal's vibes ๐ŸŽ€


๐ŸŒธ NAME ๐ŸŒธ

cfgetispeed, cfsetispeed, cfgetospeed, cfsetospeed, cfsetspeed, cfmakeraw, cfmakesane, tcgetattr, tcsetattr โ€” manipulating the termios structure ๐Ÿซถ


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C Library (libc, -lc) ๐Ÿ’œ


โœจ SYNOPSIS โœจ

ok stay with me, here's the whole cast:

#include <termios.h>

speed_t
cfgetispeed(const struct termios *t);

int
cfsetispeed(struct termios *t, speed_t speed);

speed_t
cfgetospeed(const struct termios *t);

int
cfsetospeed(struct termios *t, speed_t speed);

int
cfsetspeed(struct termios *t, speed_t speed);

void
cfmakeraw(struct termios *t);

void
cfmakesane(struct termios *t);

int
tcgetattr(int fd, struct termios *t);

int
tcsetattr(int fd, int action, const struct termios *t);

like yes, that's nine functions, she's got range ๐Ÿ‘‘


๐Ÿ’– DESCRIPTION ๐Ÿ’–

so the iconique four โ€” cfmakeraw(), cfmakesane(), tcgetattr() and tcsetattr() โ€” are the ones you get for getting and setting the termios structure. main character energy, honestly.

then cfgetispeed(), cfsetispeed(), cfgetospeed(), cfsetospeed() and cfsetspeed() handle getting and setting the baud rate values inside that same termios structure ๐ŸŒˆ

โš ๏ธ okay important: the effects on the terminal described below do not become effective, and not all errors get detected, until you actually call tcsetattr(). so like, don't expect the world to change before then, bestie.

and some baud rate values you stick in the termios structure and pass to tcsetattr() have special meanings ๐Ÿ‘€ โ€” those get discussed down in the tcsetattr() section, iykyk.


๐Ÿฆ‹ GETTING AND SETTING THE BAUD RATE ๐Ÿฆ‹

your input and output baud rates live in the termios structure. the unsigned integer speed_t is typedef'd in the include file <termios.h> โœจ the value of the integer corresponds directly to the baud rate it represents โ€” but also, these symbolic values are defined:

#define B0      0
#define B50     50
#define B75     75
#define B110    110
#define B134    134
#define B150    150
#define B200    200
#define B300    300
#define B600    600
#define B1200   1200
#define B1800   1800
#define B2400   2400
#define B4800   4800
#define B9600   9600
#define B19200  19200
#define B38400  38400
#ifndef _POSIX_SOURCE
#define EXTA    19200
#define EXTB    38400
#endif  /*_POSIX_SOURCE */

ok now the individual girlies:

  • cfgetispeed() โ€” returns the input baud rate in the termios structure referenced by t ๐Ÿ’…
  • cfsetispeed() โ€” sets the input baud rate in the termios structure referenced by t to speed โœจ
  • cfgetospeed() โ€” returns the output baud rate in the termios structure referenced by t ๐ŸŽ€
  • cfsetospeed() โ€” sets the output baud rate in the termios structure referenced by t to speed ๐ŸŒธ
  • cfsetspeed() โ€” sets both the input and output baud rate in the termios structure referenced by t to speed. overachiever, we love her ๐Ÿซถ

and the returns: upon successful completion, cfsetispeed(), cfsetospeed(), and cfsetspeed() return a value of 0 โญ otherwise, they return -1 and the global variable errno is set to indicate the error ๐Ÿ’€


๐Ÿ‘‘ GETTING AND SETTING THE TERMIOS STATE ๐Ÿ‘‘

this section is all about the functions that control the general terminal interface ๐Ÿ–ฅ๏ธ

๐Ÿšจ unless otherwise noted for a specific command, these functions are restricted from use by background processes. attempts to perform these operations shall cause the process group to be sent a SIGTTOU signal. however โ€” if the calling process is blocking or ignoring SIGTTOU signals, the process is allowed to perform the operation and the SIGTTOU signal is not sent. loophole slay ๐Ÿ’…

in all the functions, even though fd is an open file descriptor, the functions affect the underlying terminal file โ€” not just the open file description associated with that particular file descriptor. fr fr, it's deeper than just your fd ๐ŸŒŠ

๐Ÿงš the raw/sane girlies

cfmakeraw() sets the flags stored in the termios structure to a state disabling all input and output processing, giving a "raw I/O path" ๐Ÿ”ฅ while cfmakesane() sets them to a state similar to those of a newly created terminal device ๐ŸŒฑ

โš ๏ธ note bestie: there is no function to reverse this effect. why? because there are a variety of processing options that could be re-enabled, and the correct method is for an application to snapshot the current terminal state using tcgetattr(), set raw or sane mode with cfmakeraw() or cfmakesane() and the subsequent tcsetattr(), and then use another tcsetattr() with the saved state to revert to the previous terminal state. save your progress like a video game, periodt ๐ŸŽฎ

๐Ÿ” tcgetattr

tcgetattr() copies the parameters associated with the terminal referenced by fd into the termios structure referenced by t ๐Ÿ’œ

this function is allowed from a background process โ€” however, the terminal attributes may be subsequently changed by a foreground process. so like, enjoy your stale data ๐Ÿ™ˆ

๐Ÿ› ๏ธ tcsetattr

tcsetattr() sets the parameters associated with the terminal from the termios structure referenced by t โœจ

the action argument is one of the following values, as specified in the include file <termios.h>:

  • TCSANOW โ€” the change occurs immediately. no waiting, we move ๐Ÿƒโ€โ™€๏ธ
  • TCSADRAIN โ€” the change occurs after all output written to fd has been transmitted to the terminal. this value of action should be used when changing parameters that affect output ๐Ÿ‘€
  • TCSAFLUSH โ€” the change occurs after all output written to fd has been transmitted to the terminal. additionally, any input that has been received but not read is discarded ๐Ÿ—‘๏ธ

the action may be modified by or'ing in TCSASOFT which causes the values of the c_cflag, c_ispeed, and c_ospeed fields to be ignored ๐Ÿ™…โ€โ™€๏ธ

๐Ÿšจ the 0 baud rate is used to terminate the connection. if 0 is specified as the output speed to the function tcsetattr(), modem control will no longer be asserted on the terminal, disconnecting the terminal. like, that's the hangup button bestie ๐Ÿ’€

if zero is specified as the input speed to the function tcsetattr(), the input baud rate will be set to the same value as that specified by the output baud rate ๐Ÿ’ซ

if tcsetattr() is unable to make any of the requested changes, it returns -1 and sets errno ๐Ÿ˜ญ otherwise, it makes all of the requested changes it can. and if the specified input and output baud rates differ and are a combination that is not supported, neither baud rate is changed ๐Ÿ™ƒ

๐Ÿ“ return values & errors

upon successful completion, the functions tcgetattr() and tcsetattr() return a value of 0 โญ otherwise, they return -1 and the global variable errno is set to indicate the error, as follows:

  • [EBADF] โ€” the fd argument to tcgetattr() or tcsetattr() was not a valid file descriptor ๐Ÿ˜ฌ
  • [EINTR] โ€” the tcsetattr() function was interrupted by a signal โšก
  • [EINVAL] โ€” the action argument to the tcsetattr() function was not valid, or an attempt was made to change an attribute represented in the termios structure to an unsupported value ๐Ÿ™…โ€โ™€๏ธ
  • [ENOTTY] โ€” the file associated with the fd argument to tcgetattr() or tcsetattr() is not a terminal ๐Ÿ’€

๐ŸŒˆ SEE ALSO ๐ŸŒˆ

tcsendbreak(3), termios(4) โ€” go read these next, bestie, they're linked ๐Ÿซถ


โญ STANDARDS โญ

the cfgetispeed(), cfsetispeed(), cfgetospeed(), cfsetospeed(), tcgetattr() and tcsetattr() functions are expected to be compliant with the IEEE Std 1003.1-1988 ("POSIX.1") specification ๐Ÿ‘‘

but the cfmakeraw(), cfmakesane() and cfsetspeed() functions, as well as the TCSASOFT option to the tcsetattr() function, are extensions to the IEEE Std 1003.1-1988 ("POSIX.1") specification. rebels, honestly ๐Ÿ’…โœจ


ok that's the whole page, you ate that ๐Ÿฝ๏ธ๐Ÿ’– this one's from Debian, dated July 15, 2020 โ€” mwah, go touch some terminals, love u bye ๐ŸŽ€๐Ÿฆ‹๐Ÿ’œ

๐Ÿ“ฆ where this page comes from
  • freebsd 15.1: base 15.1
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms