DUP(2)DUP(2)NAME
dup, dup2 - duplicate a descriptor
SYNOPSIS
int dup(int fd);
int dup2(int fd, int fd2);
DESCRIPTION
The dup function duplicates an existing object descriptor. The
argument fd is a small non-negative integer index in the per-process
descriptor table. The value must be less than the size of the table.
The size of the table for POSIX programs is OPEN_MAX; for other
programs the size of the table is the value returned by
getdtablesize(2). The new descriptor returned by dup is the lowest
numbered descriptor that is not currently in use by the process.
The object referenced by the descriptor does not distinguish, in any
way, between references using fd and the new descriptor returned.
Thus, if the returned descriptor and are duplicate references to an
open file, read(2), write(2), and lseek(2) calls all move a single
pointer into the file, and append mode, non-blocking I/O and
asynchronous I/O options are shared between the references. If a
separate pointer into the file is desired, a different object reference
to the file must be obtained by issuing an additional open(2) call.
The close-on-exec flag on the new file descriptor is unset.
With dup2, fd2 specifies the desired value of the new descriptor. If
the descriptor fd2 is already in use, it is first deallocated as if a
close(2) call had been performed on it.
RETURN VALUE
The value -1 is returned if an error occurs in either call. The
external variable errno indicates the cause of the error.
ERRORS
The dup and dup2 functions fail if:
[EBADF] fd or fd2 is not a valid active descriptor
[EMFILE] Too many descriptors are active.
Additionally, dup2 fails if:
[EINTR] The dup2 function was interrupted by a signal.
SEE ALSOclose(2), creat(2), exec(2), fcntl(2), open(2), pipe(2)
August 1, 1992 DUP(2)