Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!boingo.med.jhu.edu!haven!adm!smoke!gwyn From: gwyn@smoke.brl.mil (Doug Gwyn) Newsgroups: comp.unix.wizards Subject: Re: dup2 Message-ID: <15136@smoke.brl.mil> Date: 8 Feb 91 21:40:31 GMT References: <27B1CA1C.22559@ics.uci.edu> Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 41 In article <27B1CA1C.22559@ics.uci.edu> schmidt@ics.uci.edu (Doug Schmidt) writes: > 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? /* dup2 -- 7th Edition UNIX system call emulation for UNIX System V last edit: 11-Feb-1987 D A Gwyn */ #include #include extern int close(), fcntl(); int dup2( oldfd, newfd ) int oldfd; /* already-open file descriptor */ int newfd; /* desired duplicate descriptor */ { register int ret; /* for fcntl() return value */ register int save; /* for saving entry errno */ if ( oldfd == newfd ) return oldfd; /* be careful not to close() */ save = errno; /* save entry errno */ (void) close( newfd ); /* in case newfd is open */ /* (may have just clobbered the original errno value) */ ret = fcntl( oldfd, F_DUPFD, newfd ); /* dupe it */ if ( ret >= 0 ) errno = save; /* restore entry errno */ else /* fcntl() returned error */ if ( errno == EINVAL ) errno = EBADF; /* we think of everything */ return ret; /* return file descriptor */ }