Q:

Retrieving stdin after using the redirection operator <

For a programming assignment, we have the following requirements:

It needs to be a command-line program written in C.
It needs to read text from a text document. However, we are to do this by using the Unix redirection operator < when running the program rather than having the program load the file itself. (So the program reads the text by pretending it's reading from stdin.)
After reading the data from the file, the program is to poll the user for some extra information before doing its job.

After much research, I can't find a way to retrieve the "old" stdin in order to accomplish part (3). Does anybody know how or if this is even possible?
Technically part (3) is part of a bonus section, which the instructor probably didn't implement himself (it's very lengthy), so it's possible that this is not possible and it's an oversight on his part. However, I certainly don't want to jump to this conclusion.

A:

On linux, i would open the controlling terminal /dev/tty.

A:

Which OS? On Linux the usual trick to accomplish this is to check if stderr is still connected to a tty:
if (isatty(2))

and if so, open a new reading file descriptor to that terminal:
new_stdin = open("/proc/self/fd/2", O_RDONLY);

then duplicate the new file descriptor to stdin (which closes the old stdin):
dup2(new_stdin, 0);

(If stderr has also been redirected, then isatty(2) will return false and you'll have to give up.)

