"temporarily" redirecting stdin

Dave Paulson dpaulso at relay.nswc.navy.mil
Fri Dec 15 05:57:45 AEST 1989


i have a program which wants to initialize itself by reading in a
configuration file.  a lex/yacc scanner/parser pair will handle the
reading of the config file.  now, the scanner that lex generates
gathers its tokens from stdin (a behavior i want to retain),
but i'll need to use stdin for my own purposes after the
initialization is complete.   

what i propose to do is summarized in the following pseudocode:

	stdin_save = dup(fileno(stdin));  /* stdin_save is an int */
	freopen("config.file","r",stdin); /* connect stdin to the config file*/
	yyparse();			  /* works ok, input comes from file */
	close(fileno(stdin));		  /* free stdin descriptor */
	dup(stdin_save);		  /* restore old stdin */
	
this doesn't work.  after the second dup(), reading stdin results
in EOF.  what have i overlooked?


-- 
Dave Paulson			Synetics / Naval Surface Warfare Center
dpaulso at relay.nswc.navy.mil	voice:	 (703) 663-2137



More information about the Comp.unix.questions mailing list