Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!njin!princeton!phoenix.Princeton.EDU!pfalstad From: pfalstad@phoenix.Princeton.EDU (Paul Falstad) Newsgroups: comp.unix.wizards Subject: Re: dup2 Message-ID: <6050@idunno.Princeton.EDU> Date: 7 Feb 91 22:16:32 GMT References: <27B1CA1C.22559@ics.uci.edu> Sender: news@idunno.Princeton.EDU Distribution: usa Organization: The Royal Society For Putting Things On Top Of Other Things Lines: 43 schmidt@ics.uci.edu (Doug Schmidt) wrote: > I'm curious, is it possible to implement the dup2() system >call using only routines available in the standard C library and other >existing system calls? Two ways, at least. 1: close and dup only int dup2(int fd1,int fd2) { close(fd2); fd1 = dup(fd1); if (fd1 != fd2) fd1 = movefd(fd1,fd2); return fd1; } int movefd(int fd1,int fd2) { int fe; if (fd1 == -1) return -1; if ((fe = dup(fd1)) != fd2) fe = movefd(fe); close(fd1); return fe; } 2: fcntl int dup2(int fd1,int fd2) { close(fd2); return(fcntl(fd1,F_DUPFD,fd2)); } -- Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD 10 PRINT "PRINCETON CS" | #include 20 GOTO 10 | [Your blood pressure just went up.] Princeton University would like to apologize to everyone for this article.