Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!rpi!bu.edu!transfer!lectroid!jjmhome!zinn!eci!morwyn!forrie From: forrie@morwyn.UUCP (Forrie Aldrich) Newsgroups: comp.unix.wizards Subject: dup2() system call Keywords: dup2 Message-ID: <85@morwyn.UUCP> Date: 17 Apr 91 03:46:18 GMT Organization: CREATIVE CONNECTIONS - Dover, NH - USA Lines: 82 Brandon S. Allbery writes: | | As quoted from <2180@estevax.UUCP> by iain@estevax.UUCP (Hr Iain Lea): | +--------------- | | I am porting a program from BSD to a Sys5r2 ish derivative and need the | | dup2() function call. | +--------------- | | Is this one in the FAQ? | | /* | * Near-duplicate for dup2(). ("Near"? We discussed this for a whole bloody | * month --- I don't want to discuss it any more. BSD and System V have | * enough differing errno values that that part is pointless anyway.) | * | * Caveats: doesn't necessarily return the same errno values on failure; does | * not leave f2 open if the dup fails. | */ | | #include | #include | | #define dup2(f1,f2) (close(f2),fcntl(f1,F_DUPFD,f2)) | | ++Brandon [...] Well, for reference's sake, here's another version that I caught off the net a little while ago... hope it helps. /* 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 */ } --------------------=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-------------------- Forrest Aldrich, Jr.| ...uunet!eci!morwyn!forrie |forrie@morywn.UUCP | | CREATIVE CONNECTIONS| ...uunet!zinn!eci!morwyn!forrie |Graphic Illustration ------------------\-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=/------------------ \___ PO Box 1541 - Dover, NH 03820 ___/ -- --------------------=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-------------------- Forrest Aldrich, Jr.| ...uunet!eci!morwyn!forrie |forrie@morywn.UUCP | | CREATIVE CONNECTIONS| ...uunet!zinn!eci!morwyn!forrie |Graphic Illustration ------------------\-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=/------------------ \___ PO Box 1541 - Dover, NH 03820 ___/