1dup2(3C) Standard C Library Functions dup2(3C)
2
3
4
6 dup2 - duplicate an open file descriptor
7
9 #include <unistd.h>
10
11 int dup2(int fildes, int fildes2);
12
13
15 The dup2() function causes the file descriptor fildes2 to refer to the
16 same file as fildes. The fildes argument is a file descriptor referring
17 to an open file, and fildes2 is a non-negative integer less than the
18 current value for the maximum number of open file descriptors allowed
19 the calling process. See getrlimit(2). If fildes2 already refers to an
20 open file, not fildes, it is closed first. If fildes2 refers to fildes,
21 or if fildes is not a valid open file descriptor, fildes2 will not be
22 closed first.
23
24
25 The dup2() function is equivalent to fcntl(fildes, F_DUP2FD, fildes2).
26
28 Upon successful completion a non-negative integer representing the file
29 descriptor is returned. Otherwise, −1 is returned and errno is set to
30 indicate the error.
31
33 The dup2() function will fail if:
34
35 EBADF The fildes argument is not a valid open file descriptor.
36
37
38 EBADF The fildes2 argument is negative or is not less than the cur‐
39 rent resource limit returned by getrlimit(RLIMIT_NOFILE,
40 ...).
41
42
43 EINTR A signal was caught during the dup2() call.
44
45
46 EMFILE The process has too many open files. See fcntl(2).
47
48
50 See attributes(5) for descriptions of the following attributes:
51
52
53
54
55 ┌─────────────────────────────┬─────────────────────────────┐
56 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
57 ├─────────────────────────────┼─────────────────────────────┤
58 │Interface Stability │Standard │
59 ├─────────────────────────────┼─────────────────────────────┤
60 │MT-Level │Async-Signal-Safe │
61 └─────────────────────────────┴─────────────────────────────┘
62
64 close(2), creat(2), exec(2), fcntl(2), getrlimit(2), open(2), pipe(2),
65 attributes(5), standards(5)
66
67
68
69SunOS 5.11 19 Dec 2003 dup2(3C)