Fixes #4223. Verified with QEMU on linux/amd64.
The previous implementation assumed that stdin was a terminal. It now checks the terminal's fd.