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