1.\" $NetBSD: dup.2,v 1.27 2011/07/22 11:02:39 wiz 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 July 16, 2011 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.El 122.Sh RETURN VALUES 123The value \-1 is returned if an error occurs in either call. 124The external variable 125.Va errno 126indicates the cause of the error. 127.Sh ERRORS 128All three functions may fail if: 129.Bl -tag -width Er 130.It Bq Er EBADF 131.Fa oldd 132is not a valid active descriptor 133or 134.Fa newd 135is not in the range of valid file descriptors. 136.El 137.Pp 138The 139.Fn dup 140function may also fail if: 141.Bl -tag -width Er 142.It Bq Er EMFILE 143Too many descriptors are active. 144.El 145.Pp 146The 147.Fn dup3 148function will also fail if: 149.Bl -tag -width Er 150.It Bq Er EINVAL 151.Fa flags 152is other than 153.Dv O_NONBLOCK 154or 155.Dv O_CLOEXEC . 156.El 157.Sh SEE ALSO 158.Xr accept 2 , 159.Xr close 2 , 160.Xr fcntl 2 , 161.Xr open 2 , 162.Xr pipe 2 , 163.Xr socket 2 , 164.Xr socketpair 2 , 165.Xr getdtablesize 3 166.Sh STANDARDS 167The 168.Fn dup 169and 170.Fn dup2 171functions conform to 172.St -p1003.1-90 . 173.Sh HISTORY 174The 175.Fn dup3 176function is inspired from Linux and appeared in 177.Nx 6.0 . 178