TCGETATTR(3P)

πŸ’… TCGETATTR(3P) β€” bestie edition πŸ’…

okay so this one lives in the POSIX Programmer's Manual, and it's called tcgetattr. yes it's giving "terminal girlie" and honestly? iconic.

⚠️ heads up babes: this page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so go peek at the corresponding Linux manual page for the Linux deets), or the interface might not even be implemented on Linux at all. 😭

✨ NAME ✨

tcgetattr β€” get the parameters associated with the terminal πŸŽ€


✨ SYNOPSIS ✨

#include <termios.h>

int tcgetattr(int fildes, struct termios *termios_p);

🌸 DESCRIPTION 🌸

okay so tcgetattr() shall get the parameters associated with the terminal referred to by fildes and store them in the termios structure referenced by termios_p. simple as that. πŸ’–

the fildes argument is an open file descriptor associated with a terminal.

the termios_p argument is a pointer to a termios structure.

and the best part? the tcgetattr() operation is allowed from any process. no gatekeeping, we love that for us. πŸ‘‘

now, baud rate tea β˜• β€” if the terminal device supports different input and output baud rates, the baud rates stored in the termios structure returned by tcgetattr() shall reflect the actual baud rates, even if they are equal. if differing baud rates are not supported, the rate returned as the output baud rate shall be the actual baud rate. and if the terminal device does not support split baud rates, the input baud rate stored in the termios structure shall be the output rate (as one of the symbolic values). periodt. ✨


🌈 RETURN VALUE 🌈

  • upon successful completion, 0 shall be returned 🫢
  • otherwise, -1 shall be returned and errno set to indicate the error πŸ’€

πŸ”₯ ERRORS πŸ”₯

the tcgetattr() function shall fail if:

  • EBADF β€” the fildes argument is not a valid file descriptor. not a real one bestie 😭
  • ENOTTY β€” the file associated with fildes is not a terminal. wrong vibe, wrong file 🚫
the following sections are informative. (aka, lore, not law πŸ’…)

πŸ¦‹ EXAMPLES πŸ¦‹

None. β€” yeah. nothing here. empty. a void. moving on πŸ’€


πŸŽ€ APPLICATION USAGE πŸŽ€

None. β€” also empty. the manpage said "no notes" and meant it. ✨


πŸ’œ RATIONALE πŸ’œ

okay so this part is important, listen up 🫢

care must be taken when changing the terminal attributes. applications should always do a tcgetattr(), save the termios structure values returned, and then do a tcsetattr(), changing only the necessary fields. the application should use the values saved from the tcgetattr() to reset the terminal state whenever it is done with the terminal. why?? because terminal attributes apply to the underlying port and not to each individual open instance β€” that is, all processes that have used the terminal see the latest attribute changes. it's a shared vibe, not a private one. 🌐

a program that uses these functions should be written to catch all signals and take other appropriate actions to ensure that when the program terminates β€” whether planned or not β€” the terminal device's state is restored to its original state. no leaving the terminal in a cursed state, bestie. 🚨

now for the historical lore πŸ“œ: existing practice dealing with error returns when only part of a request can be honored is based on calls to the ioctl() function. in historical BSD and System V implementations, the corresponding ioctl() returns zero if the requested actions were semantically correct, even if some of the requested changes could not be made. many existing applications assume this behavior and would no longer work correctly if the return value were changed from zero to -1 in this case.

and note that either specification has a problem 😭:

  • when zero is returned, it implies everything succeeded even if some of the changes were not made.
  • when -1 is returned, it implies everything failed even though some of the changes were made.

neither is giving flawless, tbh.

applications that need all of the requested changes made to work properly should follow tcsetattr() with a call to tcgetattr() and compare the appropriate field values. iykyk. πŸ’…


⭐ FUTURE DIRECTIONS ⭐

None. β€” the future is undefined but in a chill way. πŸ¦‹


πŸ‘‘ SEE ALSO πŸ‘‘

  • tcsetattr()
  • the Base Definitions volume of POSIX.1‐2017, Chapter 11, General Terminal Interface, <termios.h>

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, we ate and left zero crumbs πŸ’–βœ¨ this cutie was brought to you by IEEE/The Open Group, dated 2017, and yes it's still TCGETATTR(3P) β€” slay, periodt, goodnight πŸŒ™πŸŽ€

πŸ“¦ where this page comes from