dup2() system call
Forrie Aldrich
forrie at morwyn.UUCP
Wed Apr 17 13:46:18 AEST 1991
Brandon S. Allbery writes:
|
| As quoted from <2180 at estevax.UUCP> by iain at 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 <ioctl.h>
| #include <errno.h>
|
| #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 <errno.h>
#include <fcntl.h>
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 at morywn.UUCP
| <email paths> |
CREATIVE CONNECTIONS| ...uunet!zinn!eci!morwyn!forrie |Graphic Illustration
------------------\-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=/------------------
\___ PO Box 1541 - Dover, NH 03820 ___/
--
--------------------=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--------------------
Forrest Aldrich, Jr.| ...uunet!eci!morwyn!forrie |forrie at morywn.UUCP
| <email paths> |
CREATIVE CONNECTIONS| ...uunet!zinn!eci!morwyn!forrie |Graphic Illustration
------------------\-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=/------------------
\___ PO Box 1541 - Dover, NH 03820 ___/
More information about the Comp.unix.wizards
mailing list