xref: /openbsd/lib/libc/sys/dup.2 (revision 17df1aa7)
1.\"	$OpenBSD: dup.2,v 1.13 2007/05/31 19:19:32 jmc Exp $
2.\"	$NetBSD: dup.2,v 1.4 1995/02/27 12:32:21 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)dup.2	8.1 (Berkeley) 6/4/93
32.\"
33.Dd $Mdocdate: May 31 2007 $
34.Dt DUP 2
35.Os
36.Sh NAME
37.Nm dup ,
38.Nm dup2
39.Nd duplicate an existing file descriptor
40.Sh SYNOPSIS
41.Fd #include <unistd.h>
42.Ft int
43.Fn dup "int oldd"
44.Ft int
45.Fn dup2 "int oldd" "int newd"
46.Sh DESCRIPTION
47.Fn dup
48duplicates an existing object descriptor and returns its value to
49the calling process
50.Fa ( newd
51=
52.Fn dup oldd ) .
53The argument
54.Fa oldd
55is a small non-negative integer index in the per-process descriptor table.
56The value must be less than the size of the table, which is returned by
57.Xr getdtablesize 3 .
58The new descriptor returned by the call is the lowest numbered descriptor
59currently not in use by the process.
60.Pp
61The object referenced by the descriptor does not distinguish between
62.Fa oldd
63and
64.Fa newd
65in any way.
66Thus if
67.Fa newd
68and
69.Fa oldd
70are duplicate references to an open
71file,
72.Xr read 2 ,
73.Xr write 2
74and
75.Xr lseek 2
76calls all move a single pointer into the file,
77and append mode, non-blocking I/O and asynchronous I/O options
78are shared between the references.
79If a separate pointer into the file is desired, a different
80object reference to the file must be obtained by issuing an
81additional
82.Xr open 2
83call.
84The close-on-exec flag on the new file descriptor is unset.
85.Pp
86In
87.Fn dup2 ,
88the value of the new descriptor
89.Fa newd
90is specified.
91If this descriptor is already in use, it is first deallocated as if a
92.Xr close 2
93call had been done first.
94When
95.Fa newd
96equals
97.Fa oldd ,
98.Fn dup2
99just returns without affecting the close-on-exec flag.
100.Sh RETURN VALUES
101The value \-1 is returned if an error occurs in either call.
102The external variable
103.Va errno
104indicates the cause of the error.
105.Sh ERRORS
106.Fn dup
107and
108.Fn dup2
109fail if:
110.Bl -tag -width Er
111.It Bq Er EBADF
112.Fa oldd
113or
114.Fa newd
115is not a valid active descriptor.
116.It Bq Er EMFILE
117Too many descriptors are active.
118.El
119.Sh SEE ALSO
120.Xr accept 2 ,
121.Xr close 2 ,
122.Xr fcntl 2 ,
123.Xr open 2 ,
124.Xr pipe 2 ,
125.Xr socket 2 ,
126.Xr socketpair 2 ,
127.Xr getdtablesize 3
128.Sh STANDARDS
129.Fn dup
130and
131.Fn dup2
132are expected to conform to
133.St -p1003.1-88 .
134