F_DUPFD2const
Duplicate a file descriptor
Usage
#include <fcntl.h>
int fcntl(int fd, F_DUPFD, int arg); int fcntl(int fd, F_DUPFD_CLOEXEC, int arg);
Library
Standard C library (libc, -lc)
Overview
F_DUPFD-
Duplicate the file descriptor fd using the lowest-numbered available file descriptor greater than or equal to arg. This is different from dup2(2), which uses exactly the file descriptor specified.
-
On success, the new file descriptor is returned.
-
See dup(2) for further details.
F_DUPFD_CLOEXEC-
As for
F_DUPFD, but additionally set the close-on-exec flag for the duplicate file descriptor. Specifying this flag permits a program to avoid an additional F_SETFD(2const) operation to set theFD_CLOEXECflag. For an explanation of why this flag is useful, see the description ofO_CLOEXECin open(2).
Return value
The new file descriptor.
On error, -1 is returned, and errno is set to indicate the error.
Errors
See fcntl(2).
- EINVAL
-
arg is negative or is greater than the maximum allowable value (see the discussion of
RLIMIT_NOFILEin getrlimit(2)).
- EMFILE
-
The per-process limit on the number of open file descriptors has been reached.
Versions
POSIX.1-2024 specifies F_DUPFD_CLOFORK, but Linux doesn't support it.
Caveats
The errors returned by dup2(2) are different from those returned by F_DUPFD.