unix question about stdin
Chris Torek
chris at mimsy.UUCP
Sun Apr 23 05:34:23 AEST 1989
In article <5414 at cs.Buffalo.EDU> ugkamins at sunybcs.uucp (John Kaminski) writes:
>The original post wanted to know how to reconnect the stdin to the terminal
>after it had been attached to a file, assumedly through redirection. What I
>just tried on a BSD system, and works:
>
> fclose(stdin);
> *stdin = *fopen("/dev/tty", "r");
This is a horrible thing to do. (It does not work in some SysVs and
SunOSes.) The original question indicated that the program was to
read from stdin
switch to an alternate file
switch back to stdin
and that this was all done from within a single C program being written
by the questioner. The simplest approach is:
main()
{
FILE *fp;
read_commands(stdin);
if ((fp = fopen(other_file, "r")) == NULL)
... error ...
read_commands(fp);
(void) fclose(fp);
read_commands(stdin);
}
If the `switch to a file' is due to a particular command, read_commands()
can maintain its own stack of files (possibly by being recursive).
It is not a good idea to assign to or through any of the `standard'
streams. Instead, declare your own pointer and make it point to one
of the standard streams. There is nothing wrong with
register FILE *fp = stdin;
... work with fp ...
but there is with
fileno(stdin) = expression;
and the like.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.unix.wizards
mailing list