DUP(2)

NAME
dup, dup2 - duplicate fildescriptors

SYNOPSIS
#include<unistd.h>
int dup (int fildes);
int dup2 (int old, int new);

DESCRIPTION
dup() duplicates the filedescriptor filedes and returns either the duplicated file descriptor.

dup2() duplicates the file descriptor old, and returns a new file descriptor with a value equal to newfd. If newfd was open, it is closed before duplication.

dup() and dup2() both return negative values on error.

ERRORS
-EBADF is returned when an bad file descriptor is passed to either, for the original file descriptor that is being duplicated.

-EINVAL is returned by dup2() when an invalid new descriptor is passed.

-EMFILE is returned when the system runs out of file descriptors. See open(2) for a discussion on how to circumvent this.

FILES
linux/fs/fcntl.c, /usr/include/linux/sys.h, /usr/include/unistd.h

SEE ALSO
open(2)