Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!decwrl!mejac!orchard.la.locus.com!fafnir.la.locus.com!fafnir.la.locus.com!richard From: richard@locus.com (Richard M. Mathews) Newsgroups: comp.unix.wizards Subject: Re: dup2 Message-ID: Date: 12 Feb 91 05:00:18 GMT References: <27B1CA1C.22559@ics.uci.edu> <15136@smoke.brl.mil> Organization: Locus Computing Corporation, Los Angeles, California Lines: 20 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: main() { dup2(-1,fileno(stdout)); /* this returns -1 as expected */ printf("stdout *should* still be OK\n"); } Only the version posted by Chet Ramey gets this right, but only Doug Gwyn's version does everything right with errno, including mapping EINVAL to EBADF. I would post what I consider to be the right answer, but I have licensing problems. I can recommend taking ideas from both Gwyn and Ramey; perhaps one of them would be willing to post the result of such a merge. Richard M. Mathews Freedom for Lithuania richard@locus.com Laisve! lcc!richard@seas.ucla.edu ...!{uunet|ucla-se|turnkey}!lcc!richard