I'd like to write a program which reads user input only from tty, instead of redirected stdin pipes, like passwd and ssh do. Is there any approach?
Many thanks
I'd like to write a program which reads user input only from tty, instead of redirected stdin pipes, like passwd and ssh do. Is there any approach?
Many thanks
You should try opening /dev/tty
directly. That's the only way I know for sure that you can bypass the stdin
redirection.
/dev/tty
is usually a symbolic link to your actual tty device, which may be /dev/console
or /dev/tty24
or pretty much anything you want, depending on the weird and wonderful way your system may be set up. The tty
command should be able to tell you which one it is (as will listing the symbolic link itself with ls -al /dev/tty
).
But as long as you open dev/tty
for input, it should get the input from your terminal rather than the standard input stream, which may have been redirected.