1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\" 1993 Michael Haardt, Ian Jackson.
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
27 .\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
29 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 2002-09-28, aeb
32 .TH DUP 2 1994-08-21 "Linux 1.1.46" "Linux Programmer's Manual"
34 dup, dup2 \- duplicate a file descriptor
37 .B #include <unistd.h>
39 .BI "int dup(int " oldfd );
40 .BI "int dup2(int " oldfd ", int " newfd );
43 .BR dup "() and " dup2 ()
44 create a copy of the file descriptor
47 After a successful return from \fBdup\fR() or \fBdup2\fR(),
48 the old and new file descriptors may be used interchangeably.
49 They refer to the same open file description (see
51 and thus share file offset and file status flags;
52 for example, if the file offset is modified by using
54 on one of the descriptors, the position is also changed for the other.
56 The two descriptors do not share file descriptor flags
57 (the close-on-exec flag).
58 The close-on-exec flag
62 for the duplicate descriptor is off.
65 uses the lowest-numbered unused descriptor for the new descriptor.
68 .RI "makes " newfd " be the copy of " oldfd ", closing " newfd
71 .BR dup "() and " dup2 ()
72 return the new descriptor, or \-1 if an error occurred (in which case,
74 is set appropriately).
79 isn't an open file descriptor, or
81 is out of the allowed range for file descriptors.
84 (Linux only) This may be returned by
86 during a race condition with open() and dup().
91 call was interrupted by a signal.
94 The process already has the maximum number of file
95 descriptors open and tried to open a new one.
99 is different from that returned by
100 .BR fcntl( "..., " F_DUPFD ", ..." )
103 is out of range. On some systems
105 also sometimes returns
112 was open, any errors that would have been reported at
114 time, are lost. A careful programmer will not use
120 SVr4, SVID, POSIX, X/OPEN, BSD 4.3. SVr4 documents additional
121 EINTR and ENOLINK error conditions. POSIX.1 adds EINTR.
122 The EBUSY return is Linux-specific.