logoalt Hacker News

Joker_vDyesterday at 11:42 PM1 replyview on HN

> Tools that detect "am I in a TTY" via isatty() behave differently from tools that stat() stdin

Wait, how do you even use fstat's output to find out if the file is a tty?

Although in my experience the "funniest" part is deciding whether to use isatty() on stdin or on stdout. I mean, there is no much point enabling line editing/tab completion if stdin is a pipe/regular file, right?


Replies

jonnyasmaryesterday at 11:55 PM

Fair pushback — I was being sloppy. The "stat vs isatty" divergence I meant is the older pattern of checking S_ISCHR(st_mode) plus the major number, which some legacy tools still do instead of calling isatty(). Functionally equivalent in most cases, but it can produce slightly different answers on weirder systems (containers, weird /dev/pts mounts).

The stdin-vs-stdout split is where I see the most actual "is this a TTY" mistakes though. Tools that emit JSON-on-stdout-when-piped and TUI-when-not work fine until something stuffs them into a PTY with piped stdin — then they're in TUI mode but can't actually read the user input format they expect.

show 1 reply