Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!shadooby!accuvax.nwu.edu!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.unix.wizards Subject: Re: unix question about stdin Message-ID: <17062@mimsy.UUCP> Date: 22 Apr 89 19:34:23 GMT References: <3398@udccvax1.acs.udel.EDU> <5414@cs.Buffalo.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 48 In article <5414@cs.Buffalo.EDU> ugkamins@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@mimsy.umd.edu Path: uunet!mimsy!chris