.TH DUP 2 .UC .SH NAME dup, dup2 \- duplicate an open file descriptor .SH SYNOPSIS .B dup(fildes) .br .B int fildes; .PP .B dup2(fildes, fildes2) .br .B int fildes, fildes2; .SH DESCRIPTION Given a file descriptor returned from an .I open, .I pipe, or .IR creat (2) call, .I dup allocates another file descriptor synonymous with the original. The new file descriptor is returned. .PP In the second form of the call, .I fildes is a file descriptor referring to an open file, and .I fildes2 is a non-negative integer less than the maximum value allowed for file descriptors (approximately 19). .I Dup2 causes .I fildes2 to refer to the same file as .I fildes. If .I fildes2 already referred to an open file, it is closed first. .SH RETURN VALUE Upon successful completion, a value of 0 is returned. Otherwise, a value of \-1 is returned and .I errno is set to indicate the error. .SH ERRORS .IR Dup " and " dup2 will fail if: .TP 20 [EBADF] .I Fildes " or " fildes2 is not a valid active descriptor. .TP 20 [EMFILE] Too many descriptors are active. .SH "SEE ALSO" close(2), creat(2), open(2), pipe(2) .SH ASSEMBLER (dup = 41.) .br (file descriptor in r0) .br (new file descriptor in r1) .br .B sys dup .br (file descriptor in r0) .PP The .I dup2 entry is implemented by adding 0100 to .I fildes.