This source file includes following definitions.
- dup3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19
20 #include <unistd.h>
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25
26 #include "binary-io.h"
27
28 int
29 dup3 (int oldfd, int newfd, int flags)
30 {
31 #if HAVE_DUP3
32 # undef dup3
33 # if HAVE_SETDTABLESIZE
34
35 setdtablesize (newfd + 1);
36 # endif
37
38
39 {
40
41 static int have_dup3_really;
42 if (have_dup3_really >= 0)
43 {
44 int result = dup3 (oldfd, newfd, flags);
45 if (!(result < 0 && errno == ENOSYS))
46 {
47 have_dup3_really = 1;
48 # if REPLACE_FCHDIR
49 if (0 <= result)
50 result = _gl_register_dup (oldfd, newfd);
51 # endif
52 return result;
53 }
54 have_dup3_really = -1;
55 }
56 }
57 #endif
58
59 if (newfd < 0 || newfd >= getdtablesize () || fcntl (oldfd, F_GETFD) == -1)
60 {
61 errno = EBADF;
62 return -1;
63 }
64
65 if (newfd == oldfd)
66 {
67 errno = EINVAL;
68 return -1;
69 }
70
71
72
73
74 if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
75 {
76 errno = EINVAL;
77 return -1;
78 }
79
80 if (flags & O_CLOEXEC)
81 {
82 int result;
83 close (newfd);
84 result = fcntl (oldfd, F_DUPFD_CLOEXEC, newfd);
85 if (newfd < result)
86 {
87 close (result);
88 errno = EIO;
89 result = -1;
90 }
91 if (result < 0)
92 return -1;
93 }
94 else if (dup2 (oldfd, newfd) < 0)
95 return -1;
96
97 #if O_BINARY
98 if (flags & O_BINARY)
99 set_binary_mode (newfd, O_BINARY);
100 else if (flags & O_TEXT)
101 set_binary_mode (newfd, O_TEXT);
102 #endif
103
104 return newfd;
105 }