mktime: don’t consult daylight
[gnulib.git] / lib / dup3.c
blobd1ebb0d88ca758f9804f8f752e5228c93cfe068f
1 /* Copy a file descriptor, applying specific flags.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <unistd.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
26 #include "binary-io.h"
28 int
29 dup3 (int oldfd, int newfd, int flags)
31 #if HAVE_DUP3
32 # undef dup3
33 # if HAVE_SETDTABLESIZE
34 /* Avoid a cygwin crasher. */
35 setdtablesize (newfd + 1);
36 # endif
37 /* Try the system call first, if it exists. (We may be running with a glibc
38 that has the function but with an older kernel that lacks it.) */
40 /* Cache the information whether the system call really exists. */
41 static int have_dup3_really; /* 0 = unknown, 1 = yes, -1 = no */
42 if (have_dup3_really >= 0)
44 int result = dup3 (oldfd, newfd, flags);
45 if (!(result < 0 && errno == ENOSYS))
47 have_dup3_really = 1;
48 /* On NetBSD dup3 is a no-op when oldfd == newfd, but we are
49 expected to fail with error EINVAL. */
50 # ifdef __NetBSD__
51 if (newfd == oldfd)
53 errno = EINVAL;
54 return -1;
56 # endif
57 # if REPLACE_FCHDIR
58 if (0 <= result)
59 result = _gl_register_dup (oldfd, newfd);
60 # endif
61 return result;
63 have_dup3_really = -1;
66 #endif
68 if (newfd < 0 || newfd >= getdtablesize () || fcntl (oldfd, F_GETFD) == -1)
70 errno = EBADF;
71 return -1;
74 if (newfd == oldfd)
76 errno = EINVAL;
77 return -1;
80 /* Check the supported flags.
81 Note that O_NONBLOCK is not supported, because setting it on newfd
82 would implicitly also set it on oldfd. */
83 if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
85 errno = EINVAL;
86 return -1;
89 if (flags & O_CLOEXEC)
91 int result;
92 close (newfd);
93 result = fcntl (oldfd, F_DUPFD_CLOEXEC, newfd);
94 if (newfd < result)
96 close (result);
97 errno = EIO;
98 result = -1;
100 if (result < 0)
101 return -1;
103 else if (dup2 (oldfd, newfd) < 0)
104 return -1;
106 #if O_BINARY
107 if (flags & O_BINARY)
108 set_binary_mode (newfd, O_BINARY);
109 else if (flags & O_TEXT)
110 set_binary_mode (newfd, O_TEXT);
111 #endif
113 return newfd;