dup2()

duplicate open file descriptor onto another file descriptor 

Function


SYNOPSIS

#include <unistd.h>

int dup(int fildes, int fildes2);


DESCRIPTION

The dup2() function duplicates an open file descriptor. Specifically, it provides an alternate interface to the service provided by the fcntl() function using the F_DUPFD constant command value, with fildes2 for its third argument. The duplicated file descriptor shares any locks with the original.

On success, dup2() returns a new file descriptor that has the following in common with the original:

The new file descriptor is set to remain open across exec() functions.

The call:

fid = dup2(fildes, fildes2);

is equivalent to:

close(fildes2);
fid = fcntl(fildes, F_DUPFD, fildes2);

with the following exceptions:


PARAMETERS

fildes 

Is the file descriptor to duplicate.

fildes2 

Is the file descriptor that fildes is duplicated onto.


RETURN VALUES

If successful, dup2() returns a nonnegative integer; namely, the duplicated file descriptor, which is the lowest available descriptor. If failed, it returns a -1 and sets errno to the corresponding value.

EBADF 

fildes is not a valid open file descriptor.

EINTR 

A signal was caught during the dup2() system call.

EMFILE 

The number of file descriptors exceeds OPEN_MAX.


CONFORMANCE

POSIX.1 (1996)


MULTITHREAD SAFETY LEVEL

Async-signal-safe.


PORTING ISSUES

None.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
close(), creat(), dup(), execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), fcntl(), lockf(), open(), pipe(), socket()


PTC MKS Toolkit 10.4 Documentation Build 39.