xref: /freebsd/lib/libc/gen/dup3.3 (revision ae07a5805b1906f29e786f415d67bef334557bd3)
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 May 17, 2025
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
50The close-on-fork flag on the new file descriptor is determined by the
51.Dv O_CLOFORK
52bit in
53.Fa flags .
54.Pp
55If
56.Fa oldd
57\*(Ne
58.Fa newd
59and
60.Fa flags
61== 0,
62the behavior is identical to
63.Li dup2(oldd, newd) .
64.Pp
65If
66.Fa oldd
67==
68.Fa newd ,
69then
70.Fn dup3
71fails, unlike
72.Xr dup2 2 .
73.Sh RETURN VALUES
74The value -1 is returned if an error occurs.
75The external variable
76.Va errno
77indicates the cause of the error.
78.Sh ERRORS
79The
80.Fn dup3
81function fails if:
82.Bl -tag -width Er
83.It Bq Er EBADF
84The
85.Fa oldd
86argument is not a valid active descriptor or the
87.Fa newd
88argument is negative or exceeds the maximum allowable descriptor number
89.It Bq Er EINVAL
90The
91.Fa oldd
92argument is equal to the
93.Fa newd
94argument.
95.It Bq Er EINVAL
96The
97.Fa flags
98argument has bits set other than
99.Dv O_CLOEXEC
100or
101.Dv O_CLOFORK .
102.El
103.Sh SEE ALSO
104.Xr accept 2 ,
105.Xr close 2 ,
106.Xr dup2 2 ,
107.Xr fcntl 2 ,
108.Xr getdtablesize 2 ,
109.Xr open 2 ,
110.Xr pipe 2 ,
111.Xr socket 2 ,
112.Xr socketpair 2
113.Sh STANDARDS
114The
115.Fn dup3
116function does not conform to any standard.
117.Sh HISTORY
118The
119.Fn dup3
120function appeared in
121.Fx 10.0 .
122The
123.Dv O_CLOFORK
124flag appeared in
125.Fx 15.0 .
126