1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
5 .\" and Copyright (C) 2005, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
30 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
31 .\" Modified 2002-09-28, aeb
32 .\" 2009-01-12, mtk, reordered text in DESCRIPTION and added some
33 .\" details for dup2().
35 .TH DUP 2 2008-01-12 "Linux" "Linux Programmer's Manual"
37 dup, dup2 \- duplicate a file descriptor
40 .B #include <unistd.h>
42 .BI "int dup(int " oldfd );
43 .BI "int dup2(int " oldfd ", int " newfd );
49 create a copy of the file descriptor
53 uses the lowest-numbered unused descriptor for the new descriptor.
56 .RI "makes " newfd " be the copy of " oldfd ", closing " newfd
57 first if necessary, but note the following:
61 is not a valid file descriptor, then the call fails, and
67 is a valid file descriptor, and
73 does nothing, and returns
76 After a successful return from
80 the old and new file descriptors may be used interchangeably.
81 They refer to the same open file description (see
83 and thus share file offset and file status flags;
84 for example, if the file offset is modified by using
86 on one of the descriptors, the offset is also changed for the other.
88 The two descriptors do not share file descriptor flags
89 (the close-on-exec flag).
90 The close-on-exec flag
94 for the duplicate descriptor is off.
99 return the new descriptor, or \-1 if an error occurred (in which case,
101 is set appropriately).
106 isn't an open file descriptor, or
108 is out of the allowed range for file descriptors.
111 (Linux only) This may be returned by
113 during a race condition with
121 call was interrupted by a signal.
124 The process already has the maximum number of file
125 descriptors open and tried to open a new one.
127 SVr4, 4.3BSD, POSIX.1-2001.
128 .\" SVr4 documents additional
129 .\" EINTR and ENOLINK error conditions. POSIX.1 adds EINTR.
130 .\" The EBUSY return is Linux-specific.
132 The error returned by
134 is different from that returned by
135 .BR fcntl( "..., " F_DUPFD ", ..." )
141 also sometimes returns
148 was open, any errors that would have been reported at
151 A careful programmer will not use