isatty(0) also changes errno when returns 0. This causes unexpected error handling.

> RETURN VALUE
>       isatty() returns 1 if fd is an open file descriptor referring to a terminal; otherwise 0 is returned, and errno is set to indicate the error.