1.\" $OpenBSD: dup.2,v 1.8 2000/10/18 05:12:08 aaron Exp $ 2.\" $NetBSD: dup.2,v 1.4 1995/02/27 12:32:21 cgd Exp $ 3.\" 4.\" Copyright (c) 1980, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed by the University of 18.\" California, Berkeley and its contributors. 19.\" 4. Neither the name of the University nor the names of its contributors 20.\" may be used to endorse or promote products derived from this software 21.\" without specific prior written permission. 22.\" 23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" SUCH DAMAGE. 34.\" 35.\" @(#)dup.2 8.1 (Berkeley) 6/4/93 36.\" 37.Dd June 4, 1993 38.Dt DUP 2 39.Os 40.Sh NAME 41.Nm dup , 42.Nm dup2 43.Nd duplicate an existing file descriptor 44.Sh SYNOPSIS 45.Fd #include <unistd.h> 46.Ft int 47.Fn dup "int oldd" 48.Ft int 49.Fn dup2 "int oldd" "int newd" 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 the per-process descriptor table. 60The value must be less than the size of the table, which is returned by 61.Xr getdtablesize 3 . 62The new descriptor returned by the call is the lowest numbered descriptor 63currently not in use by the process. 64.Pp 65The object referenced by the descriptor does not distinguish between 66.Fa oldd 67and 68.Fa newd 69in any way. 70Thus if 71.Fa newd 72and 73.Fa oldd 74are duplicate references to an open 75file, 76.Xr read 2 , 77.Xr write 2 78and 79.Xr lseek 2 80calls all move a single pointer into the file, 81and append mode, non-blocking I/O and asynchronous I/O options 82are shared between the references. 83If a separate pointer into the file is desired, a different 84object reference to the file must be obtained by issuing an 85additional 86.Xr open 2 87call. 88The close-on-exec flag on the new file descriptor is unset. 89.Pp 90In 91.Fn dup2 , 92the value of the new descriptor 93.Fa newd 94is specified. 95If this descriptor is already in use, it is first deallocated as if a 96.Xr close 2 97call had been done first. 98.Sh RETURN VALUES 99The value \-1 is returned if an error occurs in either call. 100The external variable 101.Va errno 102indicates the cause of the error. 103.Sh ERRORS 104.Fn dup 105and 106.Fn dup2 107fail if: 108.Bl -tag -width Er 109.It Bq Er EBADF 110.Fa oldd 111or 112.Fa newd 113is not a valid active descriptor 114.It Bq Er EMFILE 115Too many descriptors are active. 116.El 117.Sh SEE ALSO 118.Xr accept 2 , 119.Xr close 2 , 120.Xr fcntl 2 , 121.Xr open 2 , 122.Xr pipe 2 , 123.Xr socket 2 , 124.Xr socketpair 2 , 125.Xr getdtablesize 3 126.Sh STANDARDS 127.Fn dup 128and 129.Fn dup2 130are expected to conform to 131.St -p1003.1-88 . 132