Which OS? On Linux, the usual trick for doing this is to verify that stderr is still connected to tty:
if (isatty(2))
and if so, open a new read file descriptor for this terminal:
new_stdin = open("/proc/self/fd/2", O_RDONLY);
then duplicate the new file descriptor on stdin (which closes the old stdin):
dup2(new_stdin, 0);
(If stderr is also redirected, then isatty(2) will return false, and you will have to refuse.)
source share