.TH dup2() "" "" "General Function (libc)"
.PC "Duplicate a file descriptor"
.B "#include <unistd.h>"
\fBint dup2(\fIfd\^\fB, \fInewfd\^\fB) int\fI fd\^\fB, \fInewfd\^\fB;\fR
.PP
.B dup2()
duplicates the file descriptor
.IR fd .
Unlike its cousin
.BR dup() ,
.B dup2()
allows you to specify a new
file descriptor
.IR newfd ,
rather than having the system select one.
If
.I newfd
is already open, the system closes it before assigning it to the new file.
.B dup2()
returns the duplicate descriptor.
.SH "See Also"
.Xr "dup()," dup
.Xr "libc," libc
.Xr "stdio.h," stdio.h
.Xr "unistd.h" unistd.h
.br
\*(PX Standard, \(sc6.2.1
.SH Diagnostics
.B dup2()
returns a number less than zero when an error occurs,
such as a bad file descriptor or no file descriptor available.
