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