The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

NAME

dup, dup2 - duplicate an open file descriptor

SYNOPSIS

#include <unistd.h>

int dup(int fildes); int dup2(int fildes, int fildes2);

DESCRIPTION

The dup() and dup2() functions provide an alternative interface to the service provided by fcntl() using the F_DUPFD command. The call:
fid = dup(fildes);

is equivalent to:
fid = fcntl(fildes, F_DUPFD, 0);

The call:
fid = dup2(fildes, fildes2);

is equivalent to:
close(fildes2);
fid = fcntl(fildes, F_DUPFD, fildes2);

except for the following:

RETURN VALUE

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

ERRORS

The dup() function will fail if:

[EBADF]
The fildes argument is not a valid open file descriptor.

[EMFILE]
The number of file descriptors in use by this process would exceed {OPEN_MAX}.

The dup2() function will fail if:

[EBADF]
The fildes argument is not a valid open file descriptor or the argument fildes2 is negative or greater than or equal to {OPEN_MAX} .

[EINTR]
The dup2() function was interrupted by a signal.

EXAMPLES

None.

APPLICATION USAGE

None.

FUTURE DIRECTIONS

None.

SEE ALSO

close(), fcntl(), open(), <unistd.h>.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]