Scroll to navigation

F_DUPFD(2const) F_DUPFD(2const)

NAME

F_DUPFD, F_DUPFD_CLOEXEC - duplicate a file descriptor

LIBRARY

Standard C library (libc-lc)

SYNOPSIS

#include <fcntl.h>
int fcntl(int fd, F_DUPFD, int arg);
int fcntl(int fd, F_DUPFD_CLOEXEC, int arg);

DESCRIPTION

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.
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 the FD_CLOEXEC flag. For an explanation of why this flag is useful, see the description of O_CLOEXEC in 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).

arg is negative or is greater than the maximum allowable value (see the discussion of RLIMIT_NOFILE in getrlimit(2)).
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.

STANDARDS

POSIX.1-2008.

HISTORY

SVr4, 4.3BSD, POSIX.1-2001.
Linux 2.6.24. POSIX.1-2008. (To get this definition, define _POSIX_C_SOURCE with the value 200809L or greater, or _XOPEN_SOURCE with the value 700 or greater.)

CAVEATS

The errors returned by dup2(2) are different from those returned by F_DUPFD.

SEE ALSO

dup2(2), fcntl(2)

2025-07-19 Linux man-pages (unreleased)