exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / dup.c
blobdb2680369b74c2d3f99339f7c7a2c03a417dc935
1 /* Duplicate an open file descriptor.
3 Copyright (C) 2011-2024 Free Software Foundation, Inc.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include <unistd.h>
23 #include <errno.h>
25 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
26 # include "msvc-inval.h"
27 #endif
29 #undef dup
31 #if defined _WIN32 && !defined __CYGWIN__
32 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
33 static int
34 dup_nothrow (int fd)
36 int result;
38 TRY_MSVC_INVAL
40 result = _dup (fd);
42 CATCH_MSVC_INVAL
44 result = -1;
45 errno = EBADF;
47 DONE_MSVC_INVAL;
49 return result;
51 # else
52 # define dup_nothrow _dup
53 # endif
54 #elif defined __KLIBC__
55 # include <fcntl.h>
56 # include <sys/stat.h>
58 # include <InnoTekLIBC/backend.h>
60 static int
61 dup_nothrow (int fd)
63 int dupfd;
64 struct stat sbuf;
66 dupfd = dup (fd);
67 if (dupfd == -1 && errno == ENOTSUP \
68 && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
70 char path[_MAX_PATH];
72 /* Get a path from fd */
73 if (!__libc_Back_ioFHToPath (fd, path, sizeof (path)))
74 dupfd = open (path, O_RDONLY);
77 return dupfd;
79 #else
80 # define dup_nothrow dup
81 #endif
83 int
84 rpl_dup (int fd)
86 int result = dup_nothrow (fd);
87 #if REPLACE_FCHDIR
88 if (result >= 0)
89 result = _gl_register_dup (fd, result);
90 #endif
91 return result;