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 the开发者_StackOverflow社区re 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.
You can try isatty(man 3 isatty) in conjunction with fileno():
#include <unistd.h>
int isatty(int fd);
精彩评论