xref: /freebsd/lib/libc/gen/dup3.3 (revision 617aa98c6c25406d90089351dbdf69d3a205f49d)
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.\" $FreeBSD$
26.\"
27.Dd August 16, 2013
28.Dt DUP3 3
29.Os
30.Sh NAME
31.Nm dup3
32.Nd duplicate an existing file descriptor
33.Sh LIBRARY
34.Lb libc
35.Sh SYNOPSIS
36.In fcntl.h
37.In unistd.h
38.Ft int
39.Fn dup3 "int oldd" "int newd" "int flags"
40.Sh DESCRIPTION
41The
42.Fn dup3
43function
44duplicates an existing object descriptor
45while allowing the value of the new descriptor to be specified.
46.Pp
47The close-on-exec flag on the new file descriptor is determined by the
48.Dv O_CLOEXEC
49bit in
50.Fa flags .
51.Pp
52If
53.Fa oldd
54\*(Ne
55.Fa newd
56and
57.Fa flags
58== 0,
59the behavior is identical to
60.Li dup2(oldd, newd) .
61.Pp
62If
63.Fa oldd
64==
65.Fa newd ,
66then
67.Fn dup3
68fails, unlike
69.Xr dup2 2 .
70.Sh RETURN VALUES
71The value -1 is returned if an error occurs.
72The external variable
73.Va errno
74indicates the cause of the error.
75.Sh ERRORS
76The
77.Fn dup3
78function fails if:
79.Bl -tag -width Er
80.It Bq Er EBADF
81The
82.Fa oldd
83argument is not a valid active descriptor or the
84.Fa newd
85argument is negative or exceeds the maximum allowable descriptor number
86.It Bq Er EINVAL
87The
88.Fa oldd
89argument is equal to the
90.Fa newd
91argument.
92.It Bq Er EINVAL
93The
94.Fa flags
95argument has bits set other than
96.Dv O_CLOEXEC .
97.El
98.Sh SEE ALSO
99.Xr accept 2 ,
100.Xr close 2 ,
101.Xr dup2 2 ,
102.Xr fcntl 2 ,
103.Xr getdtablesize 2 ,
104.Xr open 2 ,
105.Xr pipe 2 ,
106.Xr socket 2 ,
107.Xr socketpair 2
108.Sh STANDARDS
109The
110.Fn dup3
111function does not conform to any standard.
112.Sh HISTORY
113The
114.Fn dup3
115function appeared in
116.Fx 10.0 .
117