]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/dup.2
Reordered text in DESCRIPTION and added some details for dup2().
[thirdparty/man-pages.git] / man2 / dup.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
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>
6 .\"
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.
10 .\"
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.
15 .\"
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
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\"
27 .\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
29 .\" Fixed typoes.
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().
34 .\"
35 .TH DUP 2 2008-01-12 "Linux" "Linux Programmer's Manual"
36 .SH NAME
37 dup, dup2 \- duplicate a file descriptor
38 .SH SYNOPSIS
39 .nf
40 .B #include <unistd.h>
41 .sp
42 .BI "int dup(int " oldfd );
43 .BI "int dup2(int " oldfd ", int " newfd );
44 .fi
45 .SH DESCRIPTION
46 .BR dup ()
47 and
48 .BR dup2 ()
49 create a copy of the file descriptor
50 .IR oldfd .
51
52 .BR dup ()
53 uses the lowest-numbered unused descriptor for the new descriptor.
54
55 .BR dup2 ()
56 .RI "makes " newfd " be the copy of " oldfd ", closing " newfd
57 first if necessary, but note the following:
58 .IP * 3
59 If
60 .I olfd
61 is not a valid file descriptor, then the call fails, and
62 .I newfd
63 is not closed.
64 .IP *
65 If
66 .I oldfd
67 is a valid file descriptor, and
68 .I newfd
69 has the same value as
70 .IR oldfd ,
71 then
72 .BR dup2 ()
73 does nothing, and returns
74 .IR newfd .
75 .PP
76 After a successful return from
77 .BR dup ()
78 or
79 .BR dup2 (),
80 the old and new file descriptors may be used interchangeably.
81 They refer to the same open file description (see
82 .BR open (2))
83 and thus share file offset and file status flags;
84 for example, if the file offset is modified by using
85 .BR lseek (2)
86 on one of the descriptors, the offset is also changed for the other.
87
88 The two descriptors do not share file descriptor flags
89 (the close-on-exec flag).
90 The close-on-exec flag
91 .RB ( FD_CLOEXEC ;
92 see
93 .BR fcntl (2))
94 for the duplicate descriptor is off.
95 .SH "RETURN VALUE"
96 .BR dup ()
97 and
98 .BR dup2 ()
99 return the new descriptor, or \-1 if an error occurred (in which case,
100 .I errno
101 is set appropriately).
102 .SH ERRORS
103 .TP
104 .B EBADF
105 .I oldfd
106 isn't an open file descriptor, or
107 .I newfd
108 is out of the allowed range for file descriptors.
109 .TP
110 .B EBUSY
111 (Linux only) This may be returned by
112 .BR dup2 ()
113 during a race condition with
114 .BR open (2)
115 and
116 .BR dup ().
117 .TP
118 .B EINTR
119 The
120 .BR dup2 ()
121 call was interrupted by a signal.
122 .TP
123 .B EMFILE
124 The process already has the maximum number of file
125 descriptors open and tried to open a new one.
126 .SH "CONFORMING TO"
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.
131 .SH NOTES
132 The error returned by
133 .BR dup2 ()
134 is different from that returned by
135 .BR fcntl( "..., " F_DUPFD ", ..." )
136 when
137 .I newfd
138 is out of range.
139 On some systems
140 .BR dup2 ()
141 also sometimes returns
142 .B EINVAL
143 like
144 .BR F_DUPFD .
145
146 If
147 .I newfd
148 was open, any errors that would have been reported at
149 .BR close (2)
150 time are lost.
151 A careful programmer will not use
152 .BR dup2 ()
153 without closing
154 .I newfd
155 first.
156 .SH "SEE ALSO"
157 .BR close (2),
158 .BR fcntl (2),
159 .BR open (2)