2.11BSD/man/cat2/dup.0
DUP(2) UNIX Programmer's Manual DUP(2)
NAME
dup, dup2 - duplicate a descriptor
SYNOPSIS
newd = dup(oldd)
int newd, oldd;
dup2(oldd, newd)
int oldd, newd;
DESCRIPTION
_D_u_p duplicates an existing object descriptor. The argument
_o_l_d_d is a small non-negative integer index in the per-
process descriptor table. The value must be less than the
size of the table, which is returned by _g_e_t_d_t_a_b_l_e_s_i_z_e(2).
The new descriptor returned by the call, _n_e_w_d, is the lowest
numbered descriptor that is not currently in use by the pro-
cess.
The object referenced by the descriptor does not distinguish
between references using _o_l_d_d and _n_e_w_d in any way. Thus if
_n_e_w_d and _o_l_d_d are duplicate references to an open file,
_r_e_a_d(2), _w_r_i_t_e(2) and _l_s_e_e_k(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 _o_p_e_n(2) call. The close-on-exec flag on the new
file descriptor is unset.
In the second form of the call, the value of _n_e_w_d desired is
specified. If this descriptor is already in use, the
descriptor is first deallocated as if a _c_l_o_s_e(2) call had
been done first.
RETURN VALUE
The value -1 is returned if an error occurs in either call.
The external variable _e_r_r_n_o indicates the cause of the
error.
ERRORS
_D_u_p and _d_u_p_2 fail if:
[EBADF] _O_l_d_d or _n_e_w_d is not a valid active descriptor
[EMFILE] Too many descriptors are active.
SEE ALSO
accept(2), open(2), close(2), fcntl(2), pipe(2), socket(2),
socketpair(2), getdtablesize(2)
Printed 11/26/99 May 13, 1986 1