DUP2(3C) | Standard C Library Functions | DUP2(3C) |
dup2, dup3 - duplicate an open file descriptor
#include <unistd.h> int dup2(int fildes, int fildes2);
int dup3(int fildes, int fildes2, int flags);
The dup2() function causes the file descriptor fildes2 to refer to the same file as fildes. The fildes argument is a file descriptor referring to an open file, and fildes2 is a non-negative integer less than the current value for the maximum number of open file descriptors allowed the calling process. See getrlimit(2). If fildes2 already refers to an open file, not fildes, it is closed first. If fildes2 refers to fildes, or if fildes is not a valid open file descriptor, fildes2 will not be closed first.
The dup2() function is equivalent to fcntl(fildes, F_DUP2FD, fildes2). The returned file descriptor will have neither the close-on-exec (FD_CLOEXEC) or close-on-fork (FD_CLOFORK) flags set on it, regardless of what is set on fildes.
The dup3() function works similarly to the dup2() function with two exceptions. If fildes and fildes2 point to the same file descriptor, -1 is returned and errno set to EINVAL. The flags argument allows for control of the returned file descriptor's flags. Valid values are the bitwise-inclusive-OR of:
O_CLOEXEC
O_CLOFORK
Upon successful completion a non-negative integer representing the file descriptor is returned. Otherwise, −1 is returned and errno is set to indicate the error.
The dup2() and dup3() functions will fail if:
EBADF
EBADF
EINTR
EMFILE
Additionally, the dup3() function will fail if:
EINVAL
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
Interface Stability | Standard |
MT-Level | Async-Signal-Safe |
close(2), creat(2), exec(2), fcntl(2), fork(2), getrlimit(2), open(2), pipe(2), attributes(7), standards(7)
June 21, 2024 | OmniOS |