libc: Add dup3() from FreeBSD.
[dragonfly.git] / lib / libc / gen / dup3.3
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: head/lib/libc/gen/dup3.3 254488 2013-08-18 13:25:18Z jilles $
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
41 The
42 .Fn dup3
43 function
44 duplicates an existing object descriptor
45 while allowing the value of the new descriptor to be specified.
46 .Pp
47 The close-on-exec flag on the new file descriptor is determined by the
48 .Dv O_CLOEXEC
49 bit in
50 .Fa flags .
51 .Pp
52 If
53 .Fa oldd
54 \*(Ne
55 .Fa newd
56 and
57 .Fa flags
58 == 0,
59 the behavior is identical to
60 .Li dup2(oldd, newd) .
61 .Pp
62 If
63 .Fa oldd
64 ==
65 .Fa newd ,
66 then
67 .Fn dup3
68 fails, unlike
69 .Xr dup2 2 .
70 .Sh RETURN VALUES
71 The value -1 is returned if an error occurs.
72 The external variable
73 .Va errno
74 indicates the cause of the error.
75 .Sh ERRORS
76 The
77 .Fn dup3
78 function fails if:
79 .Bl -tag -width Er
80 .It Bq Er EBADF
81 The
82 .Fa oldd
83 argument is not a valid active descriptor or the
84 .Fa newd
85 argument is negative or exceeds the maximum allowable descriptor number
86 .It Bq Er EINVAL
87 The
88 .Fa oldd
89 argument is equal to the
90 .Fa newd
91 argument.
92 .It Bq Er EINVAL
93 The
94 .Fa flags
95 argument 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
109 The
110 .Fn dup3
111 function does not conform to any standard.
112 .Sh HISTORY
113 The
114 .Fn dup3
115 function appeared in
116 .Fx 10.0 .