Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!calvin.spp.cornell.edu!richard From: richard@calvin.spp.cornell.edu (Richard Brittain) Newsgroups: comp.sys.ibm.pc Subject: Re: i/o redirection in msdos Summary: The following works for me Message-ID: <1989Dec12.181708.14997@calvin.spp.cornell.edu> Date: 12 Dec 89 18:17:08 GMT References: <8157@cg-atla.UUCP> Reply-To: richard@calvin.spp.cornell.edu (Richard Brittain) Organization: Cornell Space Plasma Physics Group Lines: 41 In article <8157@cg-atla.UUCP> fredex@cg-atla.UUCP (Fred Smith) writes: >I have recently run into an issue where I need to DISABLE temporarily any >i/O redirection that may be in effect. ... >old = dup(fileno(stdin)); >new = freopen ("CON", "r", stdin); >/* stuff here to do the subhell */ >stream = fdopen (old, "r"); ... >At first sight, this all seems to work ok. I can go to a subshell and >all seems fine there, and I can return to my main program and all >still seems fine. However the SECOND time I try to do this to go >back to another subshell, the redirection is not un-done correctly, >in some way I have not been able to diagnose--the keyboard is I had some difficulty doing this in Turbo C 2.0 and finally suspected the dup() or dup2() library functions. I wrote my own (they are direct interfaces to the dos services so they are very simple) and got the following to work. int inp, inp2; inp = dup(0); inp2 = open("CON", O_TEXT|O_RDONLY); dup2(0,inp2); system("your program here"); close(inp2); dup2(0,inp); close(inp); There was no way I ever got it to work using stream i/o functions only, but you can always fdopen(0,"r"). I had another routine where I needed to save/ close/restore both stdin and stdout, and I found that the TC library routines worked on one and failed on the other ( I can't remember which ). Now I use my own but it is still something of a mystery. Richard Brittain, School of Elect. Eng., Upson Hall Cornell University, Ithaca, NY 14853 ARPA: richard@calvin.spp.cornell.edu UUCP: {uunet,uw-beaver,rochester,cmcl2}!cornell!calvin!richard