.BI "int dup2(int " oldfd ", int " newfd );
.fi
.SH DESCRIPTION
-.BR dup " and " dup2
+.BR dup "() and " dup2 ()
create a copy of the file descriptor
.IR oldfd .
-After successful return of \fBdup\fR or \fBdup2\fR,
-the old and new descriptors may be used interchangeably. They share
-locks, file position pointers and flags; for example, if the file
-position is modified by using
-.B lseek
+After a successful return from \fBdup\fR() or \fBdup2\fR(),
+the old and new file descriptors may be used interchangeably.
+They refer to the same open file description (see
+.BR open (2))
+and thus share file offset and file status flags;
+for example, if the file offset is modified by using
+.BR lseek (2)
on one of the descriptors, the position is also changed for the other.
-The two descriptors do not share the close-on-exec flag, however.
+The two descriptors do not share file descriptor flags
+(the close-on-exec flag).
+The close-on-exec flag
+.RB ( FD_CLOEXEC ;
+see
+.BR fcntl (2))
+for the duplicate descriptor is off.
-.B dup
+.BR dup ()
uses the lowest-numbered unused descriptor for the new descriptor.
-.B dup2
+.BR dup2 ()
.RI "makes " newfd " be the copy of " oldfd ", closing " newfd
first if necessary.
.SH "RETURN VALUE"
-.BR dup " and " dup2
+.BR dup "() and " dup2 ()
return the new descriptor, or \-1 if an error occurred (in which case,
.I errno
is set appropriately).
.TP
.B EBUSY
(Linux only) This may be returned by
-.B dup2
+.BR dup2 ()
during a race condition with open() and dup().
.TP
.B EINTR
The
-.B dup2
+.BR dup2 ()
call was interrupted by a signal.
.TP
.B EMFILE
The process already has the maximum number of file
descriptors open and tried to open a new one.
-.SH WARNING
+.SH WARNINGS
The error returned by
-.B dup2
+.BR dup2 ()
is different from that returned by
.BR fcntl( "..., " F_DUPFD ", ..." )
when
.I newfd
is out of range. On some systems
-.B dup2
+.BR dup2 ()
also sometimes returns
.B EINVAL
like
.BR F_DUPFD .
-.SH BUGS
+
If
.I newfd
was open, any errors that would have been reported at
.IR close ()
time, are lost. A careful programmer will not use
-.B dup2
+.BR dup2 ()
without closing
.I newfd
first.