1DUP(2) System Calls Manual DUP(2)
2
3
4
6 dup, dup2 - duplicate a descriptor
7
9 newd = dup(oldd)
10 int newd, oldd;
11
12 dup2(oldd, newd)
13 int oldd, newd;
14
16 Dup duplicates an existing object descriptor. The argument oldd is a
17 small non-negative integer index in the per-process descriptor table.
18 The value must be less than the size of the table, which is returned by
19 getdtablesize(2). The new descriptor returned by the call, newd, is
20 the lowest numbered descriptor that is not currently in use by the
21 process.
22
23 The object referenced by the descriptor does not distinguish between
24 references using oldd and newd in any way. Thus if newd and oldd are
25 duplicate references to an open file, read(2), write(2) and lseek(2)
26 calls all move a single pointer into the file, and append mode, non-
27 blocking I/O and asynchronous I/O options are shared between the refer‐
28 ences. If a separate pointer into the file is desired, a different
29 object reference to the file must be obtained by issuing an additional
30 open(2) call. The close-on-exec flag on the new file descriptor is
31 unset.
32
33 In the second form of the call, the value of newd desired is specified.
34 If this descriptor is already in use, the descriptor is first deallo‐
35 cated as if a close(2) call had been done first.
36
38 The value -1 is returned if an error occurs in either call. The exter‐
39 nal variable errno indicates the cause of the error.
40
42 Dup and dup2 fail if:
43
44 [EBADF] Oldd or newd is not a valid active descriptor
45
46 [EMFILE] Too many descriptors are active.
47
49 accept(2), open(2), close(2), fcntl(2), pipe(2), socket(2), socket‐
50 pair(2), getdtablesize(2)
51
52
53
544th Berkeley Distribution May 13, 1986 DUP(2)