DUP2(3C) SysV DUP2(3C)
NAME
dup2 - duplicate an open file descriptor
SYNOPSIS
int dup2 (fildes, fildes2)
int fildes, fildes2;
DESCRIPTION
The dup2 function provides an alternate interface to the service provided
by the fcntl(2) function using the F_DUPFD command.
fildes is a file descriptor referring to an open file, and fildes2 is a
non-negative integer less than NOFILES. dup2 causes fildes2 to refer to
the same file as fildes. If fildes2 already referred to an open file, it
is closed first.
The call:
fid = dup2 (fildes, fildes2);
is equivalent to:
close (fildes2);
fid= fcntl(fildes, F_DUPFD, fildes2);
except for the following:
If fildes2 is less than 0 or greater than or equal to {OPEN_MAX},
dup2 returns -1 with errno set to [EBADF].
If fildes is a valid file descriptor and is equal to fildes2, dup2
returns fildes2 without closing it.
If fildes is not a valid file descriptor, dup2 returns -1 and does
not close fildes2. The value returned is equal to the value of
fildes2 upon successful completion or -1 upon failure.
ERRORS
dup2 fails if:
[EBADF] fildes is not a valid open file descriptor.
[EMFILE] NOFILES file descriptors are currently open.
dup2 also fails if:
[EINTR] The dup2 function was terminated prematurely by a signal.
SEE ALSO
creat(2), close(2), exec(2), fcntl(2), open(2), pipe(2), lockf(3C).
DIAGNOSTICS
Upon successful completion a non-negative integer, namely the file
descriptor, is returned. Otherwise, a value of -1 is returned and errno
is set to indicate the error.