TCSETATTR(3)
๐ 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 thetermiosstructure referenced byt๐cfsetispeed()โ sets the input baud rate in thetermiosstructure referenced byttospeedโจcfgetospeed()โ returns the output baud rate in thetermiosstructure referenced byt๐cfsetospeed()โ sets the output baud rate in thetermiosstructure referenced byttospeed๐ธcfsetspeed()โ sets both the input and output baud rate in thetermiosstructure referenced byttospeed. 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 aSIGTTOUsignal. however โ if the calling process is blocking or ignoringSIGTTOUsignals, the process is allowed to perform the operation and theSIGTTOUsignal 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 usingtcgetattr(), set raw or sane mode withcfmakeraw()orcfmakesane()and the subsequenttcsetattr(), and then use anothertcsetattr()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 tofdhas been transmitted to the terminal. this value ofactionshould be used when changing parameters that affect output ๐TCSAFLUSHโ the change occurs after all output written tofdhas 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. if0is specified as the output speed to the functiontcsetattr(), 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]โ thefdargument totcgetattr()ortcsetattr()was not a valid file descriptor ๐ฌ[EINTR]โ thetcsetattr()function was interrupted by a signal โก[EINVAL]โ theactionargument to thetcsetattr()function was not valid, or an attempt was made to change an attribute represented in thetermiosstructure to an unsupported value ๐ โโ๏ธ[ENOTTY]โ the file associated with thefdargument totcgetattr()ortcsetattr()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
- Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms