]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/dup.2
Consistent use of terms "open file description",
[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 .\" 1993 Michael Haardt, Ian Jackson.
5 .\"
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.
9 .\"
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.
14 .\"
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
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
26 .\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
27 .\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
28 .\" Fixed typoes.
29 .\" Modified 1997-01-31, Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 2002-09-28, aeb
31 .\"
32 .TH DUP 2 1994-08-21 "Linux 1.1.46" "Linux Programmer's Manual"
33 .SH NAME
34 dup, dup2 \- duplicate a file descriptor
35 .SH SYNOPSIS
36 .nf
37 .B #include <unistd.h>
38 .sp
39 .BI "int dup(int " oldfd );
40 .BI "int dup2(int " oldfd ", int " newfd );
41 .fi
42 .SH DESCRIPTION
43 .BR dup "() and " dup2 ()
44 create a copy of the file descriptor
45 .IR oldfd .
46
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
50 .BR open (2))
51 and thus share file offset and file status flags;
52 for example, if the file offset is modified by using
53 .BR lseek (2)
54 on one of the descriptors, the position is also changed for the other.
55
56 The two descriptors do not share file descriptor flags
57 (the close-on-exec flag).
58 The close-on-exec flag
59 .RB ( FD_CLOEXEC ;
60 see
61 .BR fcntl (2))
62 for the duplicate descriptor is off.
63
64 .BR dup ()
65 uses the lowest-numbered unused descriptor for the new descriptor.
66
67 .BR dup2 ()
68 .RI "makes " newfd " be the copy of " oldfd ", closing " newfd
69 first if necessary.
70 .SH "RETURN VALUE"
71 .BR dup "() and " dup2 ()
72 return the new descriptor, or \-1 if an error occurred (in which case,
73 .I errno
74 is set appropriately).
75 .SH ERRORS
76 .TP
77 .B EBADF
78 .I oldfd
79 isn't an open file descriptor, or
80 .I newfd
81 is out of the allowed range for file descriptors.
82 .TP
83 .B EBUSY
84 (Linux only) This may be returned by
85 .BR dup2 ()
86 during a race condition with open() and dup().
87 .TP
88 .B EINTR
89 The
90 .BR dup2 ()
91 call was interrupted by a signal.
92 .TP
93 .B EMFILE
94 The process already has the maximum number of file
95 descriptors open and tried to open a new one.
96 .SH WARNINGS
97 The error returned by
98 .BR dup2 ()
99 is different from that returned by
100 .BR fcntl( "..., " F_DUPFD ", ..." )
101 when
102 .I newfd
103 is out of range. On some systems
104 .BR dup2 ()
105 also sometimes returns
106 .B EINVAL
107 like
108 .BR F_DUPFD .
109
110 If
111 .I newfd
112 was open, any errors that would have been reported at
113 .IR close ()
114 time, are lost. A careful programmer will not use
115 .BR dup2 ()
116 without closing
117 .I newfd
118 first.
119 .SH "CONFORMING TO"
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.
123 .SH "SEE ALSO"
124 .BR close (2),
125 .BR fcntl (2),
126 .BR open (2)