don't use hash mark for comments
[unix-history] / usr / src / lib / libc / sys / dup.2
CommitLineData
ff35cc74
KM
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
28a14409 5.\" @(#)dup.2 6.3 (Berkeley) %G%
ff35cc74 6.\"
d4d39c08 7.TH DUP 2 ""
ff35cc74
KM
8.UC 4
9.SH NAME
936824e3 10dup, dup2 \- duplicate a descriptor
ff35cc74
KM
11.SH SYNOPSIS
12.nf
936824e3
KM
13.ft B
14newd = dup(oldd)
15int newd, oldd;
ff35cc74 16.PP
936824e3
KM
17.ft B
18dup2(oldd, newd)
19int oldd, newd;
ff35cc74
KM
20.fi
21.SH DESCRIPTION
936824e3
KM
22.I Dup
23duplicates an existing object descriptor.
24The argument \fIoldd\fP is a small non-negative integer index in
25the per-process descriptor table. The value must be less
26than the size of the table, which is returned by
27.IR getdtablesize (2).
b5f0627a
SS
28The new descriptor returned by the call,
29.I newd,
30is the lowest numbered descriptor that is
936824e3 31not currently in use by the process.
ff35cc74 32.PP
936824e3
KM
33The object referenced by the descriptor does not distinguish
34between references using \fIoldd\fP and \fInewd\fP in any way.
35Thus if \fInewd\fP and \fIoldd\fP are duplicate references to an open
36file,
37.IR read (2),
38.IR write (2)
39and
40.IR lseek (2)
28a14409
MK
41calls all move a single pointer into the file,
42and append mode, non-blocking I/O and asynchronous I/O options
43are shared between the references.
936824e3
KM
44If a separate pointer into the file is desired, a different
45object reference to the file must be obtained by issuing an
46additional
47.IR open (2)
48call.
28a14409 49The close-on-exec flag on the new file descriptor is unset.
ff35cc74 50.PP
936824e3
KM
51In the second form of the call, the value of
52.IR newd
53desired is specified. If this descriptor is already
54in use, the descriptor is first deallocated as if a
55.IR close (2)
56call had been done first.
57.SH "RETURN VALUE
58The value \-1 is returned if an error occurs in either call.
59The external variable
60.I errno
61indicates the cause of the error.
62.SH "ERRORS
63.I Dup
64and
ff35cc74 65.I dup2
936824e3
KM
66fail if:
67.TP 15
68[EBADF]
69\fIOldd\fP or
70\fInewd\fP is not a valid active descriptor
71.TP 15
72[EMFILE]
73Too many descriptors are active.
74.SH "SEE ALSO"
75accept(2),
76open(2),
77close(2),
28a14409 78fcntl(2),
936824e3
KM
79pipe(2),
80socket(2),
81socketpair(2),
82getdtablesize(2)