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 | .\" | |
d4d39c08 | 5 | .\" @(#)dup.2 6.1 (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). | |
28 | The new descriptor | |
29 | .I newd | |
30 | returned by the call is the lowest numbered descriptor which is | |
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) | |
41 | calls all move a single pointer into the file. | |
42 | If a separate pointer into the file is desired, a different | |
43 | object reference to the file must be obtained by issuing an | |
44 | additional | |
45 | .IR open (2) | |
46 | call. | |
ff35cc74 | 47 | .PP |
936824e3 KM |
48 | In the second form of the call, the value of |
49 | .IR newd | |
50 | desired is specified. If this descriptor is already | |
51 | in use, the descriptor is first deallocated as if a | |
52 | .IR close (2) | |
53 | call had been done first. | |
54 | .SH "RETURN VALUE | |
55 | The value \-1 is returned if an error occurs in either call. | |
56 | The external variable | |
57 | .I errno | |
58 | indicates the cause of the error. | |
59 | .SH "ERRORS | |
60 | .I Dup | |
61 | and | |
ff35cc74 | 62 | .I dup2 |
936824e3 KM |
63 | fail if: |
64 | .TP 15 | |
65 | [EBADF] | |
66 | \fIOldd\fP or | |
67 | \fInewd\fP is not a valid active descriptor | |
68 | .TP 15 | |
69 | [EMFILE] | |
70 | Too many descriptors are active. | |
71 | .SH "SEE ALSO" | |
72 | accept(2), | |
73 | open(2), | |
74 | close(2), | |
75 | pipe(2), | |
76 | socket(2), | |
77 | socketpair(2), | |
78 | getdtablesize(2) |