1.\" $NetBSD: dup.2,v 1.28 2012/01/25 00:28:35 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 January 23, 2012 33.Dt DUP 2 34.Os 35.Sh NAME 36.Nm dup , 37.Nm dup2 , 38.Nm dup3 39.Nd duplicate an existing file descriptor 40.Sh LIBRARY 41.Lb libc 42.Sh SYNOPSIS 43.In unistd.h 44.Ft int 45.Fn dup "int oldd" 46.Ft int 47.Fn dup2 "int oldd" "int newd" 48.Ft int 49.Fn dup3 "int oldd" "int newd" "int flags" 50.Sh DESCRIPTION 51.Fn dup 52duplicates an existing object descriptor and returns its value to 53the calling process 54.Fa ( newd 55= 56.Fn dup oldd ) . 57The argument 58.Fa oldd 59is a small non-negative integer index in 60the per-process descriptor table. 61The value must be less than the size of the table, which is returned by 62.Xr getdtablesize 3 . 63The new descriptor returned by the call 64is the lowest numbered descriptor currently not in use by the process. 65.Pp 66The object referenced by the descriptor does not distinguish 67between 68.Fa oldd 69and 70.Fa newd 71in any way. 72Thus if 73.Fa newd 74and 75.Fa oldd 76are duplicate references to an open 77file, 78.Xr read 2 , 79.Xr write 2 80and 81.Xr lseek 2 82calls all move a single pointer into the file, 83and append mode, non-blocking I/O and asynchronous I/O options 84are shared between the references. 85If a separate pointer into the file is desired, a different 86object reference to the file must be obtained by issuing an 87additional 88.Xr open 2 89call. 90The close-on-exec flag on the new file descriptor is unset. 91.Pp 92In 93.Fn dup2 , 94the value of the new descriptor 95.Fa newd 96is specified. 97If this descriptor is already 98in use, the descriptor is first deallocated as if a 99.Xr close 2 100call had been done first. 101If 102.Fa newd 103and 104.Fa oldd 105are the same, the call has no effect. 106.Pp 107.Fn dup3 108behaves exactly like 109.Fn dup2 110only it allows extra 111.Fa flags 112to be set on the returned file descriptor. 113The following flags are valid: 114.Bl -tag -width O_NONBLOCK -offset indent 115.It Dv O_CLOEXEC 116Set the 117.Dq close-on-exec 118property. 119.It Dv O_NONBLOCK 120Sets non-blocking I/O. 121.It Dv O_NOSIGPIPE 122Return 123.Er EPIPE 124instead of raising 125.Dv SIGPIPE . 126.El 127.Sh RETURN VALUES 128The value \-1 is returned if an error occurs in either call. 129The external variable 130.Va errno 131indicates the cause of the error. 132.Sh ERRORS 133All three functions may fail if: 134.Bl -tag -width Er 135.It Bq Er EBADF 136.Fa oldd 137is not a valid active descriptor 138or 139.Fa newd 140is not in the range of valid file descriptors. 141.El 142.Pp 143The 144.Fn dup 145function may also fail if: 146.Bl -tag -width Er 147.It Bq Er EMFILE 148Too many descriptors are active. 149.El 150.Pp 151The 152.Fn dup3 153function will also fail if: 154.Bl -tag -width Er 155.It Bq Er EINVAL 156.Fa flags 157is other than 158.Dv O_NONBLOCK 159or 160.Dv O_CLOEXEC . 161.El 162.Sh SEE ALSO 163.Xr accept 2 , 164.Xr close 2 , 165.Xr fcntl 2 , 166.Xr open 2 , 167.Xr pipe 2 , 168.Xr socket 2 , 169.Xr socketpair 2 , 170.Xr getdtablesize 3 171.Sh STANDARDS 172The 173.Fn dup 174and 175.Fn dup2 176functions conform to 177.St -p1003.1-90 . 178.Sh HISTORY 179The 180.Fn dup3 181function is inspired from Linux and appeared in 182.Nx 6.0 . 183