]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/dup.2
Formatting fixes
[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 ()
44 and
45 .BR dup2 ()
46 create a copy of the file descriptor
47 .IR oldfd .
48
49 After a successful return from \fBdup\fR() or \fBdup2\fR(),
50 the old and new file descriptors may be used interchangeably.
51 They refer to the same open file description (see
52 .BR open (2))
53 and thus share file offset and file status flags;
54 for example, if the file offset is modified by using
55 .BR lseek (2)
56 on one of the descriptors, the offset is also changed for the other.
57
58 The two descriptors do not share file descriptor flags
59 (the close-on-exec flag).
60 The close-on-exec flag
61 .RB ( FD_CLOEXEC ;
62 see
63 .BR fcntl (2))
64 for the duplicate descriptor is off.
65
66 .BR dup ()
67 uses the lowest-numbered unused descriptor for the new descriptor.
68
69 .BR dup2 ()
70 .RI "makes " newfd " be the copy of " oldfd ", closing " newfd
71 first if necessary.
72 .SH "RETURN VALUE"
73 .BR dup ()
74 and
75 .BR dup2 ()
76 return the new descriptor, or \-1 if an error occurred (in which case,
77 .I errno
78 is set appropriately).
79 .SH ERRORS
80 .TP
81 .B EBADF
82 .I oldfd
83 isn't an open file descriptor, or
84 .I newfd
85 is out of the allowed range for file descriptors.
86 .TP
87 .B EBUSY
88 (Linux only) This may be returned by
89 .BR dup2 ()
90 during a race condition with
91 .BR open ()
92 and
93 .BR dup ().
94 .TP
95 .B EINTR
96 The
97 .BR dup2 ()
98 call was interrupted by a signal.
99 .TP
100 .B EMFILE
101 The process already has the maximum number of file
102 descriptors open and tried to open a new one.
103 .SH WARNINGS
104 The error returned by
105 .BR dup2 ()
106 is different from that returned by
107 .BR fcntl( "..., " F_DUPFD ", ..." )
108 when
109 .I newfd
110 is out of range. On some systems
111 .BR dup2 ()
112 also sometimes returns
113 .B EINVAL
114 like
115 .BR F_DUPFD .
116
117 If
118 .I newfd
119 was open, any errors that would have been reported at
120 .IR close ()
121 time, are lost. A careful programmer will not use
122 .BR dup2 ()
123 without closing
124 .I newfd
125 first.
126 .SH "CONFORMING TO"
127 SVr4, SVID, POSIX, X/OPEN, 4.3BSD. SVr4 documents additional
128 EINTR and ENOLINK error conditions. POSIX.1 adds EINTR.
129 The EBUSY return is Linux-specific.
130 .SH "SEE ALSO"
131 .BR close (2),
132 .BR fcntl (2),
133 .BR open (2)