DUP(2)
NAME
dup, dup2 − duplicate a descriptor
USAGE
newd = dup(oldd)
int newd, oldd;
dup2(oldd, newd)
int oldd, newd;
DESCRIPTION
Dup duplicates an existing object descriptor. The argument oldd 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 getdtablesize(2). The new descriptor newd returned by the call is the lowest-numbered descriptor that the process is not currently using.
The object that the descriptor refers to does not distinguish between references to oldd and newd in any way. Thus, if newd and oldd are duplicate references to an open file, read(2), write(2) and lseek(2) calls all move a single pointer into the file. If a separate pointer into the file is desired, you must create a different object reference to the file, by issuing an additional open(2) call.
In the second form of the call, the value of the newd desired is specified. If this descriptor is already in use, the descriptor is deallocated first, as if a close(2) call had been done first.
RETURN VALUE
Both dup and dup2 return −1 if an error occurs, and set the external variable errno to indicate the cause of the error.
ERRORS
Dup and dup2 fail if:
[EBADF] Oldd or newd is not a valid active descriptor.
[EMFILE] Too many descriptors are active.
RELATED INFORMATION
accept(2), open(2), close(2), pipe(2), socket(2), socketpair(2), getdtablesize(2)