Filtering I/O -- both of them.

Leo de Wit leo at ehviea.ine.philips.nl
Tue Jul 10 06:39:38 AEST 1990


In article <1990Jul08.223103.1244 at virtech.uucp> cpcahil at virtech.UUCP (Conor P. Cahill) writes:
|In article <1990Jul8.120742.18213 at lth.se> d89cb at efd.lth.se (Christian Brunschen) writes:
    []
|>BUT (and here's my question) :
|>how do I actually redirect stdin / stdout to the pipes I created ?
|
|>stdin = fdopen (MyPipe [1], "r");
|
|This is real close.  what you do is:
|
|	(void) fclose(stdin);		/* close stdin 			*/
|	(void) close(0);		/* should be unnecessary	*/
|	i = dup(pipe_read_fd);		/* i should be 0		*/
|	(void) fdopen(i, "r");		/* stdin should map to pipe 0 	*/
|	close(pipe_read_fd);		/* dont need anymore		*/
|
|The reason for doing the dup() & fdopen is to ensure that fd0 gets 
|assigned to stdin since there may be software that does a read(0,...).
|
|Note that I did no error checking.  You should add the appropriate error
|checking for each step (other than the close(0) which should fail if 
|stdin was appropriately mapped to 0).

The dup() should indeed return 0 (or alternatively, use
dup2(pipe_read_fd,0)), but I think there is no guarantee that
fdopen(i,"r") returns stdin (though 'old-style' stdio implementations
will probably do so).

How about:
    (void)dup2(pipe_read,fileno(stdin));
    (void)close(pipe_read);

(I'm pretty sure there are problems with this as well)?

    Leo.



More information about the Comp.lang.c mailing list