Checking if it's a TTY
In this recipe, we'll start looking at some of the C functions to examine TTYs. Here, we mean TTY in the broadest sense, meaning both TTY and PTY.
The program we will write here will check if stdout is a terminal. If it's not, it will print an error message.
Knowing how to check if stdin, stdout, or stderr are terminal devices will enable you to write error checks for programs that require a terminal to work.
Getting ready
For this recipe, we'll need the GCC compiler, the Make tool, and the generic Makefile. The generic Makefile can be downloaded from this chapter's GitHub folder, at https://github.com/PacktPublishing/Linux-System-Programming-Techniques/tree/master/ch9.
How to do it…
Here, we'll write a small program that prints an error message if stdout is not a terminal:
- Write the following small program in a file and save it as
ttyinfo.c
. We use two new functions here. The first one isisatty...