xref: /freebsd/lib/libc/gen/dup3.3 (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1.\" Copyright (c) 2013 Jilles Tjoelker
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.Dd August 16, 2013
26.Dt DUP3 3
27.Os
28.Sh NAME
29.Nm dup3
30.Nd duplicate an existing file descriptor
31.Sh LIBRARY
32.Lb libc
33.Sh SYNOPSIS
34.In fcntl.h
35.In unistd.h
36.Ft int
37.Fn dup3 "int oldd" "int newd" "int flags"
38.Sh DESCRIPTION
39The
40.Fn dup3
41function
42duplicates an existing object descriptor
43while allowing the value of the new descriptor to be specified.
44.Pp
45The close-on-exec flag on the new file descriptor is determined by the
46.Dv O_CLOEXEC
47bit in
48.Fa flags .
49.Pp
50If
51.Fa oldd
52\*(Ne
53.Fa newd
54and
55.Fa flags
56== 0,
57the behavior is identical to
58.Li dup2(oldd, newd) .
59.Pp
60If
61.Fa oldd
62==
63.Fa newd ,
64then
65.Fn dup3
66fails, unlike
67.Xr dup2 2 .
68.Sh RETURN VALUES
69The value -1 is returned if an error occurs.
70The external variable
71.Va errno
72indicates the cause of the error.
73.Sh ERRORS
74The
75.Fn dup3
76function fails if:
77.Bl -tag -width Er
78.It Bq Er EBADF
79The
80.Fa oldd
81argument is not a valid active descriptor or the
82.Fa newd
83argument is negative or exceeds the maximum allowable descriptor number
84.It Bq Er EINVAL
85The
86.Fa oldd
87argument is equal to the
88.Fa newd
89argument.
90.It Bq Er EINVAL
91The
92.Fa flags
93argument has bits set other than
94.Dv O_CLOEXEC .
95.El
96.Sh SEE ALSO
97.Xr accept 2 ,
98.Xr close 2 ,
99.Xr dup2 2 ,
100.Xr fcntl 2 ,
101.Xr getdtablesize 2 ,
102.Xr open 2 ,
103.Xr pipe 2 ,
104.Xr socket 2 ,
105.Xr socketpair 2
106.Sh STANDARDS
107The
108.Fn dup3
109function does not conform to any standard.
110.Sh HISTORY
111The
112.Fn dup3
113function appeared in
114.Fx 10.0 .
115