This source file includes following definitions.
- dup2_nothrow
- ms_windows_dup2
- klibc_dup2dirfd
- klibc_dup2
- rpl_dup2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22
23 #include <unistd.h>
24
25 #include <errno.h>
26 #include <fcntl.h>
27
28 #undef dup2
29
30 #if defined _WIN32 && ! defined __CYGWIN__
31
32
33 # define WIN32_LEAN_AND_MEAN
34 # include <windows.h>
35
36 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
37 # include "msvc-inval.h"
38 # endif
39
40
41 # if GNULIB_MSVC_NOTHROW
42 # include "msvc-nothrow.h"
43 # else
44 # include <io.h>
45 # endif
46
47 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
48 static int
49 dup2_nothrow (int fd, int desired_fd)
50 {
51 int result;
52
53 TRY_MSVC_INVAL
54 {
55 result = _dup2 (fd, desired_fd);
56 }
57 CATCH_MSVC_INVAL
58 {
59 errno = EBADF;
60 result = -1;
61 }
62 DONE_MSVC_INVAL;
63
64 return result;
65 }
66 # else
67 # define dup2_nothrow _dup2
68 # endif
69
70 static int
71 ms_windows_dup2 (int fd, int desired_fd)
72 {
73 int result;
74
75
76
77
78 if (fd == desired_fd)
79 {
80 if ((HANDLE) _get_osfhandle (fd) == INVALID_HANDLE_VALUE)
81 {
82 errno = EBADF;
83 return -1;
84 }
85 return fd;
86 }
87
88
89
90 if (desired_fd < 0)
91 {
92 errno = EBADF;
93 return -1;
94 }
95
96 result = dup2_nothrow (fd, desired_fd);
97
98 if (result == 0)
99 result = desired_fd;
100
101 return result;
102 }
103
104 # define dup2 ms_windows_dup2
105
106 #elif defined __KLIBC__
107
108 # include <InnoTekLIBC/backend.h>
109
110 static int
111 klibc_dup2dirfd (int fd, int desired_fd)
112 {
113 int tempfd;
114 int dupfd;
115
116 tempfd = open ("NUL", O_RDONLY);
117 if (tempfd == -1)
118 return -1;
119
120 if (tempfd == desired_fd)
121 {
122 close (tempfd);
123
124 char path[_MAX_PATH];
125 if (__libc_Back_ioFHToPath (fd, path, sizeof (path)))
126 return -1;
127
128 return open(path, O_RDONLY);
129 }
130
131 dupfd = klibc_dup2dirfd (fd, desired_fd);
132
133 close (tempfd);
134
135 return dupfd;
136 }
137
138 static int
139 klibc_dup2 (int fd, int desired_fd)
140 {
141 int dupfd;
142 struct stat sbuf;
143
144 dupfd = dup2 (fd, desired_fd);
145 if (dupfd == -1 && errno == ENOTSUP \
146 && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
147 {
148 close (desired_fd);
149
150 return klibc_dup2dirfd (fd, desired_fd);
151 }
152
153 return dupfd;
154 }
155
156 # define dup2 klibc_dup2
157 #endif
158
159 int
160 rpl_dup2 (int fd, int desired_fd)
161 {
162 int result;
163
164 #ifdef F_GETFL
165
166
167
168
169
170 # if HAVE_SETDTABLESIZE
171 setdtablesize (desired_fd + 1);
172 # endif
173 if (desired_fd < 0)
174 fd = desired_fd;
175 if (fd == desired_fd)
176 return fcntl (fd, F_GETFL) == -1 ? -1 : fd;
177 #endif
178
179 result = dup2 (fd, desired_fd);
180
181
182 if (result == -1 && errno == EMFILE)
183 errno = EBADF;
184 #if REPLACE_FCHDIR
185 if (fd != desired_fd && result != -1)
186 result = _gl_register_dup (fd, result);
187 #endif
188 return result;
189 }