dup(2)dup(2)Name
dup, dup2 - duplicate an open file descriptor
Syntax
newd = dup(oldd)
int newd, oldd;
dup2(oldd, newd)
int oldd, newd;
Description
The system call duplicates an existing object descriptor. The argument
oldd is a small non-negative integer index in the per-process descrip‐
tor table. The value must be less than the size of the table, which is
returned by The new descriptor, newd, returned by the call is the low‐
est numbered descriptor that is not currently in use by the process.
The object referenced by the descriptor does not distinguish between
references using oldd and newd in any way. Thus, if newd and oldd are
duplicate references to an open file, and calls all move a single
pointer into the file. If a separate pointer into the file is desired,
a different object reference to the file must be obtained by issuing an
additional call.
In the second form of the call, specify the value of newd needed. If
this descriptor is already in use, the descriptor is first deallocated
as if a call had been done.
Return Values
The value -1 is returned if an error occurs in either call. The exter‐
nal variable errno indicates the cause of the error.
Diagnostics
The and system calls fail under the following conditions:
[EBADF] The oldd or newd is not a valid active descriptor.
[EMFILE] Too many descriptors are active.
[EINTR] The or function was terminated prematurely by a signal.
See Alsoaccept(2), close(2), getdtablesize(2), lseek(2), open(2), pipe(2),
read(2), socket(2), socketpair(2), write(2)dup(2)