1.\" $NetBSD: dup.2,v 1.18 2004/05/13 10:20:58 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 June 4, 1993 33.Dt DUP 2 34.Os 35.Sh NAME 36.Nm dup , 37.Nm dup2 38.Nd duplicate an existing file descriptor 39.Sh LIBRARY 40.Lb libc 41.Sh SYNOPSIS 42.In unistd.h 43.Ft int 44.Fn dup "int oldd" 45.Ft int 46.Fn dup2 "int oldd" "int newd" 47.Sh DESCRIPTION 48.Fn dup 49duplicates an existing object descriptor and returns its value to 50the calling process 51.Fa ( newd 52= 53.Fn dup oldd ) . 54The argument 55.Fa oldd 56is a small non-negative integer index in 57the per-process descriptor table. 58The value must be less than the size of the table, which is returned by 59.Xr getdtablesize 3 . 60The new descriptor returned by the call 61is the lowest numbered descriptor currently not in use by the process. 62.Pp 63The object referenced by the descriptor does not distinguish 64between 65.Fa oldd 66and 67.Fa newd 68in any way. 69Thus if 70.Fa newd 71and 72.Fa oldd 73are duplicate references to an open 74file, 75.Xr read 2 , 76.Xr write 2 77and 78.Xr lseek 2 79calls all move a single pointer into the file, 80and append mode, non-blocking I/O and asynchronous I/O options 81are shared between the references. 82If a separate pointer into the file is desired, a different 83object reference to the file must be obtained by issuing an 84additional 85.Xr open 2 86call. 87The close-on-exec flag on the new file descriptor is unset. 88.Pp 89In 90.Fn dup2 , 91the value of the new descriptor 92.Fa newd 93is specified. 94If this descriptor is already 95in use, the descriptor 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 127The 128.Fn dup 129and 130.Fn dup2 131functions conform to 132.St -p1003.1-90 . 133