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