d3aafa458b5a793f6a7e9008caea3835d495cb18
1 /* Duplicate an open file descriptor to a specified file descriptor.
3 Copyright (C) 1999, 2004-2007, 2009-2019 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* written by Paul Eggert */
32 # if defined _WIN32 && ! defined __CYGWIN__
34 /* Get declarations of the native Windows API functions. */
35 # define WIN32_LEAN_AND_MEAN
38 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
39 # include "msvc-inval.h"
42 /* Get _get_osfhandle. */
43 # if GNULIB_MSVC_NOTHROW
44 # include "msvc-nothrow.h"
49 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
51 dup2_nothrow (int fd
, int desired_fd
)
57 result
= dup2 (fd
, desired_fd
);
69 # define dup2_nothrow dup2
73 ms_windows_dup2 (int fd
, int desired_fd
)
77 /* If fd is closed, mingw hangs on dup2 (fd, fd). If fd is open,
78 dup2 (fd, fd) returns 0, but all further attempts to use fd in
79 future dup2 calls will hang. */
82 if ((HANDLE
) _get_osfhandle (fd
) == INVALID_HANDLE_VALUE
)
90 /* Wine 1.0.1 return 0 when desired_fd is negative but not -1:
91 https://bugs.winehq.org/show_bug.cgi?id=21289 */
98 result
= dup2_nothrow (fd
, desired_fd
);
106 # define dup2 ms_windows_dup2
108 # elif defined __KLIBC__
110 # include <InnoTekLIBC/backend.h>
113 klibc_dup2dirfd (int fd
, int desired_fd
)
118 tempfd
= open ("NUL", O_RDONLY
);
122 if (tempfd
== desired_fd
)
126 char path
[_MAX_PATH
];
127 if (__libc_Back_ioFHToPath (fd
, path
, sizeof (path
)))
130 return open(path
, O_RDONLY
);
133 dupfd
= klibc_dup2dirfd (fd
, desired_fd
);
141 klibc_dup2 (int fd
, int desired_fd
)
146 dupfd
= dup2 (fd
, desired_fd
);
147 if (dupfd
== -1 && errno
== ENOTSUP \
148 && !fstat (fd
, &sbuf
) && S_ISDIR (sbuf
.st_mode
))
152 return klibc_dup2dirfd (fd
, desired_fd
);
158 # define dup2 klibc_dup2
162 rpl_dup2 (int fd
, int desired_fd
)
167 /* On Linux kernels 2.6.26-2.6.29, dup2 (fd, fd) returns -EBADF.
168 On Cygwin 1.5.x, dup2 (1, 1) returns 0.
169 On Cygwin 1.7.17, dup2 (1, -1) dumps core.
170 On Cygwin 1.7.25, dup2 (1, 256) can dump core.
171 On Haiku, dup2 (fd, fd) mistakenly clears FD_CLOEXEC. */
172 # if HAVE_SETDTABLESIZE
173 setdtablesize (desired_fd
+ 1);
177 if (fd
== desired_fd
)
178 return fcntl (fd
, F_GETFL
) == -1 ? -1 : fd
;
181 result
= dup2 (fd
, desired_fd
);
183 /* Correct an errno value on FreeBSD 6.1 and Cygwin 1.5.x. */
184 if (result
== -1 && errno
== EMFILE
)
187 if (fd
!= desired_fd
&& result
!= -1)
188 result
= _gl_register_dup (fd
, result
);
193 #else /* !HAVE_DUP2 */
195 /* On older platforms, dup2 did not exist. */
199 dupfd (int fd
, int desired_fd
)
201 int duplicated_fd
= dup (fd
);
202 if (duplicated_fd
< 0 || duplicated_fd
== desired_fd
)
203 return duplicated_fd
;
206 int r
= dupfd (fd
, desired_fd
);
208 close (duplicated_fd
);
216 dup2 (int fd
, int desired_fd
)
218 int result
= fcntl (fd
, F_GETFL
) < 0 ? -1 : fd
;
219 if (result
== -1 || fd
== desired_fd
)
223 result
= fcntl (fd
, F_DUPFD
, desired_fd
);
226 result
= _gl_register_dup (fd
, result
);
229 result
= dupfd (fd
, desired_fd
);
231 if (result
== -1 && (errno
== EMFILE
|| errno
== EINVAL
))
235 #endif /* !HAVE_DUP2 */
This page took 0.043852 seconds and 3 git commands to generate.