dup.2: Rewrite the description of dup() somewhat

As can be seen by any number of StackOverflow questions, people
persistently misunderstand what dup() does, and the existing manual
page text, which talks of "copying" a file descriptor doesn't help.
Rewrite the text a little to try to prevent some of these
misunderstandings, in particular noting at the start that dup()
allocates a new file descriptor.

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2021-03-23 10:35:38 +01:00
parent 18e408ec5f
commit f5270fe6f8
1 changed files with 11 additions and 7 deletions

View File

@ -53,18 +53,22 @@ dup, dup2, dup3 \- duplicate a file descriptor
.SH DESCRIPTION
The
.BR dup ()
system call creates a copy of the file descriptor
.IR oldfd ,
using the lowest-numbered unused file descriptor for the new descriptor.
system call allocates a new file descriptor that refers to the same
open file description as the descriptor
.IR oldfd .
(For an explanation of open file descriptions, see
.BR open (2).)
The new file descriptor number is guaranteed to be the lowest-numbered
file descriptor that was unused in the calling process.
.PP
After a successful return,
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;
Since the two file descriptors refer to the same open file description,
they share file offset and file status flags;
for example, if the file offset is modified by using
.BR lseek (2)
on one of the file descriptors, the offset is also changed for the other.
on one of the file descriptors,
the offset is also changed for the other file descriptor.
.PP
The two file descriptors do not share file descriptor flags
(the close-on-exec flag).