Try using isatty() in the file descriptor:
The isatty() function isatty() file descriptor fd refers to a valid terminal type device.
The fileno() function checks the stream of arguments and returns its integer descriptor.
Note that stderr always in file descriptor 2, so you really don't need fileno() in this particular case.
source share