I have been looking int the man 3 tcgetattr (as I want to change the terminal settings in a program) and found this.
int tcgetattr(int fd, struct termios *termios_p);
int tcsetattr(int fd, int optional_actions,
const struct termios *termios_p);
I would like to know what fd
should mean? (it seems to be stdin
, yet I do not understand why)?
My comprehension is that terminal is input and output together, as my understanding was that a /dev/tty
or /dev/pty
yields stdin
, stdout
and stderr
together.
fd
stands for file descriptor, which is a reference to an OS file object. Because it is a reference, multiple different file descriptors may refer to the same file object.
stdin
, stdout
, and stderr
are FILE *
objects -- actual pointers to stdio FILE
datastructures. You can get the file descriptor that refers to the underlying OS object with the fileno
function.
So there's two levels of indirection going on here. The FILE *
could all refer to the same FILE
, but they don't; there are 3 separate FILE
objects for stdin
, stdout
, and stderr
. These FILE
objects each contain a file descriptor, normally 0, 1 and 2 (I say normally -- the OS/lib sets them up this way and they'll only change if you explicitly change them in your program). The 3 file descriptors will then normally all refer to the same underlying OS object, which is a single terminal object.
Since there's (generally) only one terminal, and all these file descriptors (usually) refer to it, it doesn't matter which fd (0, 1, or 2) you use as the first argument to tcsetaddr
.
Note that it is possible for these fd
s to refer to different objects -- if you start your program with redirections (<
or >
in the shell) then one or more of them will refer to some other file object and not the terminal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With