Path: utzoo!attcan!uunet!mcsun!hp4nl!phigate!ehviea!leo From: leo@ehviea.ine.philips.nl (Leo de Wit) Newsgroups: comp.lang.c Subject: Re: Filtering I/O -- both of them. Message-ID: <826@ehviea.ine.philips.nl> Date: 9 Jul 90 20:39:38 GMT References: <1990Jul8.120742.18213@lth.se> <1990Jul08.223103.1244@virtech.uucp> Reply-To: leo@ehviea.UUCP (Leo de Wit) Organization: Philips I&E Eindhoven Lines: 35 In article <1990Jul08.223103.1244@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes: |In article <1990Jul8.120742.18213@lth.se> d89cb@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.