While reading some documentation today, I came across the libc isatty() and ttyname() functions. I was curious to see how they worked, so I decided to write a little test program:
$ cat tty.c
#include #include #include
int main(int argc, char**argv) { if ( argc != 2) { printf(“Usage: tty fdn”); exit(1); }
int fd = atoi(argv[1]);
if (isatty(fd)) { printf(“File descriptor %d is a terminal %sn”,fd, ttyname(fd)); } else { printf(“File descriptor %d is not a terminaln”,fd); }
exit(0); }
$ gcc -o tty tty.c
$ ./tty
Usage: tty fd
$ ./tty 0
File descriptor 0 is a terminal /dev/pts/4
$ ./tty 1
File descriptor 1 is a terminal /dev/pts/4
$ ./tty 4
File descriptor 4 is not a terminal
Even though this is a small meaningless program, programming in C is fun! Shibby!