Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!bcm!dimacs.rutgers.edu!rutgers!att!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!samsung!spool.mu.edu!uunet!rbj From: rbj@uunet.UU.NET (Root Boy Jim) Newsgroups: comp.unix.internals Subject: Re: dup2 Message-ID: <122478@uunet.UU.NET> Date: 12 Feb 91 22:37:12 GMT References: <27B1CA1C.22559@ics.uci.edu> <15136@smoke.brl.mil> Organization: UUNET Communications Services, Falls Church, VA Lines: 21 In article richard@locus.com (Richard M. Mathews) writes: >I have not yet seen a correct version of code for dup2() posted. The >versions posted by Paul Falsted, Doug Gwyn, and Root Boy Jim all fail >the following test case because they close "fd2" too soon: I suppose it depend on your interpretation. If you try dup2(-1,fileno(stdout)) and end up with nowhere to print to, have you not made a copy of your input file descriptor? What does the shell do with "cmd >&- 2>&1"? And what is the "meaning" of dup2(1,1)? Anyone who trys to do this probably has a logic error somewhere, regardless of the definition. In any case, I am willing to add "if (new == old) return(new);" to my code. I also claim that I handled errno properly. It is not necessary to preserve the old value of errno if dup2 returns true. And Guy, you are right, any system that has neither dup2 nor fcntl probably has lots of problems. However, my version works in V6! -- [rbj@uunet 1] stty sane unknown mode: sane