Re: What should tty? do if handed a port that's not even vaguely a tty? Lassi Kortela 10 Aug 2019 21:26 UTC

> Per POSIX, isatty
> is allowed to set errno to ENOTTY when handed a file descriptor to
> something that's not a terminal device, like one opened to a regular
> file.  Should we signal an error, or just return #f in that case?  Ditto
> if the port handed to tty? does not have a file descriptor, like a
> string port.

The ordinary usage of isatty() is e.g. checking whether or not to do
ANSI color / line editing on stdin/stdout. In these situations
stdin/stdout can often reasonably be regular files or pipes. Therefore a
#t/#f return with no exception raised would be idiomatic.

The POSIX spec is curious in that isatty() may or may not set errno when
it returns false. We can check this with:

     errno = 0
     if (isatty(fd)) {
         return true
     } else if (errno == 0) {
         return false
     } else {
         raise error based on errno
     }

We just have to make sure that we are dealing with the thread-local
errno in a multi-threaded Scheme (and that the thread after the isatty()
call is the same OS thread as the one before the call).