DUP(3) Library Functions Manual DUP(3)
NAME
dup dup2 —
duplicate an open file descriptor
SYNOPSIS
#include <unistd.h> int dup(int fildes); int dup2(int fildes, int fildes2);
DESCRIPTION
The dup() and dup2() functions provide an alternative interface to the service provided by fcntl(3) using the F_DUPFD command. The call:
fid = dup(fildes);
fid = fcntl(fildes, F_DUPFD, 0);
fid = dup2(fildes, fildes2);
close(fildes2); fid = fcntl(fildes, F_DUPFD, fildes2);
- 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 is -1 upon failure.
RETURN VALUE
Upon successful completion a non-negative integer, namely the file descriptor, is returned. Otherwise, -1 is returned and errno is set to indicate the error.
ERRORS
The dup() function will fail if:
- [EBADF]
- The fildes argument is not a valid open file descriptor.
- [EMFILE]
- The number of file descriptors in use by this process would exceed {OPEN_MAX}.
The dup2() function will fail if:
EXAMPLES
None.
APPLICATION USAGE
None.
FUTURE DIRECTIONS
None.
SEE ALSO
close(3) , fcntl(3) , open(3) , <unistd.h> .
DERIVATION
Derived from Issue 1 of the SVID.
February 1997 posix.fail