[Go to CFHT Home Page] Man Pages
Back to Software Index  BORDER=0Manpage Top Level
    dup2(3C) manual page Table of Contents

Name

dup2 - duplicate an open file descriptor

Synopsis

#include <unistd.h>

int dup2(int fildes, int fildes2);

MT-Level

Unsafe

Async-Signal-Safe

Description

dup2() causes the file descriptor fildes2 to refer to the same file as fildes. fildes 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 referred 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.

Return Values

Upon successful completion a non-negative integer, namely, the file descriptor, is returned. Otherwise, a value of -1 is returned and errno is set to indicate the error.

Errors

dup2() will fail if one or more of the following are true:

EBADF
fildes is not a valid open file descriptor.
EINTR
a signal was caught during the dup2() call.
EMFILE
the process has too many open files (see fcntl(2) ).

See Also

close(2) , creat(2) , exec(2) , fcntl(2) , getrlimit(2) , open(2) , pipe(2) , lockf(3C)


Table of Contents