.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
-.\" 1993 Michael Haardt, Ian Jackson.
+.\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
+.\" and Copyright (C) 2005, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" Fixed typoes.
.\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
.\" Modified 2002-09-28, aeb
+.\" 2009-01-12, mtk, reordered text in DESCRIPTION and added some
+.\" details for dup2().
.\"
-.TH DUP 2 1994-08-21 "Linux" "Linux Programmer's Manual"
+.TH DUP 2 2008-01-12 "Linux" "Linux Programmer's Manual"
.SH NAME
dup, dup2 \- duplicate a file descriptor
.SH SYNOPSIS
create a copy of the file descriptor
.IR oldfd .
+.BR dup ()
+uses the lowest-numbered unused descriptor for the new descriptor.
+
+.BR dup2 ()
+.RI "makes " newfd " be the copy of " oldfd ", closing " newfd
+first if necessary, but note the following:
+.IP * 3
+If
+.I olfd
+is not a valid file descriptor, then the call fails, and
+.I newfd
+is not closed.
+.IP *
+If
+.I oldfd
+is a valid file descriptor, and
+.I newfd
+has the same value as
+.IR oldfd ,
+then
+.BR dup2 ()
+does nothing, and returns
+.IR newfd .
+.PP
After a successful return from
.BR dup ()
or
see
.BR fcntl (2))
for the duplicate descriptor is off.
-
-.BR dup ()
-uses the lowest-numbered unused descriptor for the new descriptor.
-
-.BR dup2 ()
-.RI "makes " newfd " be the copy of " oldfd ", closing " newfd
-first if necessary.
.SH "RETURN VALUE"
.BR dup ()
and
.I newfd
was open, any errors that would have been reported at
.BR close (2)
-time, are lost.
+time are lost.
A careful programmer will not use
.BR dup2 ()
without closing