annotate lib/fcntl.c @ 40190:ef116535bf1a

relocatable-prog: Use $ORIGIN trick on more platforms. * m4/relocatable.m4 (gl_RELOCATABLE_BODY): Use $ORIGIN trick also on FreeBSD >= 7.3, DragonFly >= 3.0, NetBSD >= 8.0, OpenBSD >= 5.4, Solaris >= 10, Haiku. But don't use it on Android. * build-aux/reloc-ldflags: Allow the use of the $ORIGIN trick also on Hurd, FreeBSD, DragonFly, NetBSD, OpenBSD, Solaris, Haiku.
author Bruno Haible <bruno@clisp.org>
date Wed, 20 Feb 2019 02:39:52 +0100
parents ed6a8b3113de
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Provide file descriptor control.
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
2
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 39882
diff changeset
3 Copyright (C) 2009-2019 Free Software Foundation, Inc.
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8 (at your option) any later version.
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13 GNU General Public License for more details.
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18883
diff changeset
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18 /* Written by Eric Blake <ebb9@byu.net>. */
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20 #include <config.h>
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22 /* Specification. */
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
23 #include <fcntl.h>
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
24
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
25 #include <errno.h>
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
26 #include <limits.h>
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
27 #include <stdarg.h>
12491
ad883448fbdf Reduce namespace pollution on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 12456
diff changeset
28 #include <unistd.h>
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
30 #ifdef __KLIBC__
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
31 # define INCL_DOS
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
32 # include <os2.h>
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
33 #endif
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
35 #if defined _WIN32 && ! defined __CYGWIN__
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
36 /* Get declarations of the native Windows API functions. */
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
37 # define WIN32_LEAN_AND_MEAN
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
38 # include <windows.h>
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
39
15752
b86e9061a6d0 New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14232
diff changeset
40 /* Get _get_osfhandle. */
18883
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
41 # if GNULIB_MSVC_NOTHROW
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
42 # include "msvc-nothrow.h"
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
43 # else
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
44 # include <io.h>
19886582ca8d Implement a way to opt out from MSVC support.
Bruno Haible <bruno@clisp.org>
parents: 18626
diff changeset
45 # endif
15752
b86e9061a6d0 New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14232
diff changeset
46
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
47 /* Upper bound on getdtablesize(). See lib/getdtablesize.c. */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
48 # define OPEN_MAX_MAX 0x10000
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
49
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
50 /* Duplicate OLDFD into the first available slot of at least NEWFD,
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
51 which must be positive, with FLAGS determining whether the duplicate
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
52 will be inheritable. */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
53 static int
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
54 dupfd (int oldfd, int newfd, int flags)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
55 {
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
56 /* Mingw has no way to create an arbitrary fd. Iterate until all
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
57 file descriptors less than newfd are filled up. */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
58 HANDLE curr_process = GetCurrentProcess ();
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
59 HANDLE old_handle = (HANDLE) _get_osfhandle (oldfd);
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
60 unsigned char fds_to_close[OPEN_MAX_MAX / CHAR_BIT];
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
61 unsigned int fds_to_close_bound = 0;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
62 int result;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
63 BOOL inherit = flags & O_CLOEXEC ? FALSE : TRUE;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
64 int mode;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
65
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
66 if (newfd < 0 || getdtablesize () <= newfd)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
67 {
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
68 errno = EINVAL;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
69 return -1;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
70 }
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
71 if (old_handle == INVALID_HANDLE_VALUE
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
72 || (mode = setmode (oldfd, O_BINARY)) == -1)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
73 {
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
74 /* oldfd is not open, or is an unassigned standard file
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
75 descriptor. */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
76 errno = EBADF;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
77 return -1;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
78 }
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
79 setmode (oldfd, mode);
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
80 flags |= mode;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
81
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
82 for (;;)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
83 {
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
84 HANDLE new_handle;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
85 int duplicated_fd;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
86 unsigned int index;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
87
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
88 if (!DuplicateHandle (curr_process, /* SourceProcessHandle */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
89 old_handle, /* SourceHandle */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
90 curr_process, /* TargetProcessHandle */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
91 (PHANDLE) &new_handle, /* TargetHandle */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
92 (DWORD) 0, /* DesiredAccess */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
93 inherit, /* InheritHandle */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
94 DUPLICATE_SAME_ACCESS)) /* Options */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
95 {
17767
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
96 switch (GetLastError ())
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
97 {
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
98 case ERROR_TOO_MANY_OPEN_FILES:
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
99 errno = EMFILE;
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
100 break;
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
101 case ERROR_INVALID_HANDLE:
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
102 case ERROR_INVALID_TARGET_HANDLE:
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
103 case ERROR_DIRECT_ACCESS_HANDLE:
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
104 errno = EBADF;
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
105 break;
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
106 case ERROR_INVALID_PARAMETER:
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
107 case ERROR_INVALID_FUNCTION:
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
108 case ERROR_INVALID_ACCESS:
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
109 errno = EINVAL;
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
110 break;
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
111 default:
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
112 errno = EACCES;
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
113 break;
bc4693f4bc9b modules/fcntl: fix error reporting by dupfd
Eli Zaretskii <eliz@gnu.org>
parents: 17576
diff changeset
114 }
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
115 result = -1;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
116 break;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
117 }
16326
067aa8df247a accept4, fcntl, socket modules: Avoid warnings on x86_64 mingw64.
Marc-André Lureau <marcandre.lureau@redhat.com>
parents: 16214
diff changeset
118 duplicated_fd = _open_osfhandle ((intptr_t) new_handle, flags);
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
119 if (duplicated_fd < 0)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
120 {
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
121 CloseHandle (new_handle);
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
122 result = -1;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
123 break;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
124 }
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
125 if (newfd <= duplicated_fd)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
126 {
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
127 result = duplicated_fd;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
128 break;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
129 }
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
130
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
131 /* Set the bit duplicated_fd in fds_to_close[]. */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
132 index = (unsigned int) duplicated_fd / CHAR_BIT;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
133 if (fds_to_close_bound <= index)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
134 {
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
135 if (sizeof fds_to_close <= index)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
136 /* Need to increase OPEN_MAX_MAX. */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
137 abort ();
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
138 memset (fds_to_close + fds_to_close_bound, '\0',
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
139 index + 1 - fds_to_close_bound);
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
140 fds_to_close_bound = index + 1;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
141 }
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
142 fds_to_close[index] |= 1 << ((unsigned int) duplicated_fd % CHAR_BIT);
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
143 }
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
144
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
145 /* Close the previous fds that turned out to be too small. */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
146 {
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
147 int saved_errno = errno;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
148 unsigned int duplicated_fd;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
149
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
150 for (duplicated_fd = 0;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
151 duplicated_fd < fds_to_close_bound * CHAR_BIT;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
152 duplicated_fd++)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
153 if ((fds_to_close[duplicated_fd / CHAR_BIT]
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
154 >> (duplicated_fd % CHAR_BIT))
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
155 & 1)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
156 close (duplicated_fd);
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
157
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
158 errno = saved_errno;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
159 }
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
160
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
161 # if REPLACE_FCHDIR
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
162 if (0 <= result)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
163 result = _gl_register_dup (oldfd, result);
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
164 # endif
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
165 return result;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
166 }
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
167 #endif /* W32 */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
168
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
169 /* Forward declarations, because we '#undef fcntl' in the middle of this
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
170 compilation unit. */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
171 /* Our implementation of fcntl (fd, F_DUPFD, target). */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
172 static int rpl_fcntl_DUPFD (int fd, int target);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
173 /* Our implementation of fcntl (fd, F_DUPFD_CLOEXEC, target). */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
174 static int rpl_fcntl_DUPFD_CLOEXEC (int fd, int target);
18207
a2fadbadc563 dup, dup2, fcntl: support a directory fd on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 18189
diff changeset
175 #ifdef __KLIBC__
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
176 /* Adds support for fcntl on directories. */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
177 static int klibc_fcntl (int fd, int action, /* arg */...);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
178 #endif
18207
a2fadbadc563 dup, dup2, fcntl: support a directory fd on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 18189
diff changeset
179
a2fadbadc563 dup, dup2, fcntl: support a directory fd on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents: 18189
diff changeset
180
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
181 /* Perform the specified ACTION on the file descriptor FD, possibly
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
182 using the argument ARG further described below. This replacement
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
183 handles the following actions, and forwards all others on to the
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
184 native fcntl. An unrecognized ACTION returns -1 with errno set to
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
185 EINVAL.
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
186
12455
dbc90a3fc4ed fcntl: work around cygwin bug in F_DUPFD
Eric Blake <ebb9@byu.net>
parents: 12454
diff changeset
187 F_DUPFD - duplicate FD, with int ARG being the minimum target fd.
dbc90a3fc4ed fcntl: work around cygwin bug in F_DUPFD
Eric Blake <ebb9@byu.net>
parents: 12454
diff changeset
188 If successful, return the duplicate, which will be inheritable;
dbc90a3fc4ed fcntl: work around cygwin bug in F_DUPFD
Eric Blake <ebb9@byu.net>
parents: 12454
diff changeset
189 otherwise return -1 and set errno.
dbc90a3fc4ed fcntl: work around cygwin bug in F_DUPFD
Eric Blake <ebb9@byu.net>
parents: 12454
diff changeset
190
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
191 F_DUPFD_CLOEXEC - duplicate FD, with int ARG being the minimum
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
192 target fd. If successful, return the duplicate, which will not be
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
193 inheritable; otherwise return -1 and set errno.
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
194
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
195 F_GETFD - ARG need not be present. If successful, return a
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
196 non-negative value containing the descriptor flags of FD (only
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
197 FD_CLOEXEC is portable, but other flags may be present); otherwise
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
198 return -1 and set errno. */
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
199
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
200 int
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
201 fcntl (int fd, int action, /* arg */...)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
202 #undef fcntl
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
203 #ifdef __KLIBC__
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
204 # define fcntl klibc_fcntl
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
205 #endif
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
206 {
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
207 va_list arg;
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
208 int result = -1;
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
209 va_start (arg, action);
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
210 switch (action)
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
211 {
12455
dbc90a3fc4ed fcntl: work around cygwin bug in F_DUPFD
Eric Blake <ebb9@byu.net>
parents: 12454
diff changeset
212 case F_DUPFD:
dbc90a3fc4ed fcntl: work around cygwin bug in F_DUPFD
Eric Blake <ebb9@byu.net>
parents: 12454
diff changeset
213 {
dbc90a3fc4ed fcntl: work around cygwin bug in F_DUPFD
Eric Blake <ebb9@byu.net>
parents: 12454
diff changeset
214 int target = va_arg (arg, int);
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
215 result = rpl_fcntl_DUPFD (fd, target);
12455
dbc90a3fc4ed fcntl: work around cygwin bug in F_DUPFD
Eric Blake <ebb9@byu.net>
parents: 12454
diff changeset
216 break;
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
217 }
12455
dbc90a3fc4ed fcntl: work around cygwin bug in F_DUPFD
Eric Blake <ebb9@byu.net>
parents: 12454
diff changeset
218
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
219 case F_DUPFD_CLOEXEC:
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
220 {
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
221 int target = va_arg (arg, int);
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
222 result = rpl_fcntl_DUPFD_CLOEXEC (fd, target);
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
223 break;
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
224 }
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
225
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
226 #if !HAVE_FCNTL
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
227 case F_GETFD:
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
228 {
19595
beb2ad957aca Simplify code. Drop support for Borland C++ on Windows.
Bruno Haible <bruno@clisp.org>
parents: 19484
diff changeset
229 # if defined _WIN32 && ! defined __CYGWIN__
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
230 HANDLE handle = (HANDLE) _get_osfhandle (fd);
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
231 DWORD flags;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
232 if (handle == INVALID_HANDLE_VALUE
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
233 || GetHandleInformation (handle, &flags) == 0)
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
234 errno = EBADF;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
235 else
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
236 result = (flags & HANDLE_FLAG_INHERIT) ? 0 : FD_CLOEXEC;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
237 # else /* !W32 */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
238 /* Use dup2 to reject invalid file descriptors. No way to
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
239 access this information, so punt. */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
240 if (0 <= dup2 (fd, fd))
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
241 result = 0;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
242 # endif /* !W32 */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
243 break;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
244 } /* F_GETFD */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
245 #endif /* !HAVE_FCNTL */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
246
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
247 /* Implementing F_SETFD on mingw is not trivial - there is no
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
248 API for changing the O_NOINHERIT bit on an fd, and merely
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
249 changing the HANDLE_FLAG_INHERIT bit on the underlying handle
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
250 can lead to odd state. It may be possible by duplicating the
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
251 handle, using _open_osfhandle with the right flags, then
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
252 using dup2 to move the duplicate onto the original, but that
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
253 is not supported for now. */
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
254
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
255 default:
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
256 {
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
257 #if HAVE_FCNTL
39817
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
258 switch (action)
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
259 {
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
260 #ifdef F_BARRIERFSYNC /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
261 case F_BARRIERFSYNC:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
262 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
263 #ifdef F_CHKCLEAN /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
264 case F_CHKCLEAN:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
265 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
266 #ifdef F_CLOSEM /* NetBSD, HP-UX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
267 case F_CLOSEM:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
268 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
269 #ifdef F_FLUSH_DATA /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
270 case F_FLUSH_DATA:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
271 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
272 #ifdef F_FREEZE_FS /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
273 case F_FREEZE_FS:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
274 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
275 #ifdef F_FULLFSYNC /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
276 case F_FULLFSYNC:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
277 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
278 #ifdef F_GETCONFINED /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
279 case F_GETCONFINED:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
280 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
281 #ifdef F_GETDEFAULTPROTLEVEL /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
282 case F_GETDEFAULTPROTLEVEL:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
283 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
284 #ifdef F_GETFD /* POSIX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
285 case F_GETFD:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
286 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
287 #ifdef F_GETFL /* POSIX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
288 case F_GETFL:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
289 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
290 #ifdef F_GETLEASE /* Linux */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
291 case F_GETLEASE:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
292 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
293 #ifdef F_GETNOSIGPIPE /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
294 case F_GETNOSIGPIPE:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
295 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
296 #ifdef F_GETOWN /* POSIX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
297 case F_GETOWN:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
298 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
299 #ifdef F_GETPIPE_SZ /* Linux */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
300 case F_GETPIPE_SZ:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
301 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
302 #ifdef F_GETPROTECTIONCLASS /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
303 case F_GETPROTECTIONCLASS:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
304 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
305 #ifdef F_GETPROTECTIONLEVEL /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
306 case F_GETPROTECTIONLEVEL:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
307 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
308 #ifdef F_GET_SEALS /* Linux */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
309 case F_GET_SEALS:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
310 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
311 #ifdef F_GETSIG /* Linux */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
312 case F_GETSIG:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
313 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
314 #ifdef F_MAXFD /* NetBSD */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
315 case F_MAXFD:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
316 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
317 #ifdef F_RECYCLE /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
318 case F_RECYCLE:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
319 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
320 #ifdef F_SETFIFOENH /* HP-UX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
321 case F_SETFIFOENH:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
322 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
323 #ifdef F_THAW_FS /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
324 case F_THAW_FS:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
325 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
326 /* These actions take no argument. */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
327 result = fcntl (fd, action);
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
328 break;
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
329
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
330 #ifdef F_ADD_SEALS /* Linux */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
331 case F_ADD_SEALS:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
332 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
333 #ifdef F_BADFD /* Solaris */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
334 case F_BADFD:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
335 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
336 #ifdef F_CHECK_OPENEVT /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
337 case F_CHECK_OPENEVT:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
338 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
339 #ifdef F_DUP2FD /* FreeBSD, AIX, Solaris */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
340 case F_DUP2FD:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
341 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
342 #ifdef F_DUP2FD_CLOEXEC /* FreeBSD, Solaris */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
343 case F_DUP2FD_CLOEXEC:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
344 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
345 #ifdef F_DUP2FD_CLOFORK /* Solaris */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
346 case F_DUP2FD_CLOFORK:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
347 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
348 #ifdef F_DUPFD /* POSIX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
349 case F_DUPFD:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
350 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
351 #ifdef F_DUPFD_CLOEXEC /* POSIX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
352 case F_DUPFD_CLOEXEC:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
353 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
354 #ifdef F_DUPFD_CLOFORK /* Solaris */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
355 case F_DUPFD_CLOFORK:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
356 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
357 #ifdef F_GETXFL /* Solaris */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
358 case F_GETXFL:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
359 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
360 #ifdef F_GLOBAL_NOCACHE /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
361 case F_GLOBAL_NOCACHE:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
362 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
363 #ifdef F_MAKECOMPRESSED /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
364 case F_MAKECOMPRESSED:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
365 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
366 #ifdef F_MOVEDATAEXTENTS /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
367 case F_MOVEDATAEXTENTS:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
368 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
369 #ifdef F_NOCACHE /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
370 case F_NOCACHE:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
371 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
372 #ifdef F_NODIRECT /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
373 case F_NODIRECT:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
374 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
375 #ifdef F_NOTIFY /* Linux */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
376 case F_NOTIFY:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
377 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
378 #ifdef F_OPLKACK /* IRIX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
379 case F_OPLKACK:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
380 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
381 #ifdef F_OPLKREG /* IRIX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
382 case F_OPLKREG:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
383 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
384 #ifdef F_RDAHEAD /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
385 case F_RDAHEAD:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
386 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
387 #ifdef F_SETBACKINGSTORE /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
388 case F_SETBACKINGSTORE:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
389 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
390 #ifdef F_SETCONFINED /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
391 case F_SETCONFINED:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
392 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
393 #ifdef F_SETFD /* POSIX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
394 case F_SETFD:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
395 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
396 #ifdef F_SETFL /* POSIX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
397 case F_SETFL:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
398 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
399 #ifdef F_SETLEASE /* Linux */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
400 case F_SETLEASE:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
401 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
402 #ifdef F_SETNOSIGPIPE /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
403 case F_SETNOSIGPIPE:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
404 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
405 #ifdef F_SETOWN /* POSIX */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
406 case F_SETOWN:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
407 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
408 #ifdef F_SETPIPE_SZ /* Linux */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
409 case F_SETPIPE_SZ:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
410 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
411 #ifdef F_SETPROTECTIONCLASS /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
412 case F_SETPROTECTIONCLASS:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
413 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
414 #ifdef F_SETSIG /* Linux */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
415 case F_SETSIG:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
416 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
417 #ifdef F_SINGLE_WRITER /* macOS */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
418 case F_SINGLE_WRITER:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
419 #endif
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
420 /* These actions take an 'int' argument. */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
421 {
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
422 int x = va_arg (arg, int);
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
423 result = fcntl (fd, action, x);
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
424 }
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
425 break;
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
426
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
427 default:
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
428 /* Other actions take a pointer argument. */
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
429 {
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
430 void *p = va_arg (arg, void *);
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
431 result = fcntl (fd, action, p);
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
432 }
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
433 break;
1c0d31ca4c49 fcntl: Don't access nonexistent optional argument.
Bruno Haible <bruno@clisp.org>
parents: 19595
diff changeset
434 }
12456
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
435 #else
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
436 errno = EINVAL;
f3aceada3c52 fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents: 12455
diff changeset
437 #endif
12454
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
438 break;
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
439 }
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
440 }
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
441 va_end (arg);
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
442 return result;
f7624052e60d fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff changeset
443 }
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
444
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
445 static int
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
446 rpl_fcntl_DUPFD (int fd, int target)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
447 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
448 int result;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
449 #if !HAVE_FCNTL
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
450 result = dupfd (fd, target, 0);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
451 #elif FCNTL_DUPFD_BUGGY || REPLACE_FCHDIR
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
452 /* Detect invalid target; needed for cygwin 1.5.x. */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
453 if (target < 0 || getdtablesize () <= target)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
454 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
455 result = -1;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
456 errno = EINVAL;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
457 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
458 else
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
459 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
460 /* Haiku alpha 2 loses fd flags on original. */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
461 int flags = fcntl (fd, F_GETFD);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
462 if (flags < 0)
39882
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
463 result = -1;
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
464 else
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
465 {
39882
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
466 result = fcntl (fd, F_DUPFD, target);
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
467 if (0 <= result && fcntl (fd, F_SETFD, flags) == -1)
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
468 {
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
469 int saved_errno = errno;
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
470 close (result);
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
471 result = -1;
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
472 errno = saved_errno;
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
473 }
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
474 # if REPLACE_FCHDIR
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
475 if (0 <= result)
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
476 result = _gl_register_dup (fd, result);
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
477 # endif
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
478 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
479 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
480 #else
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
481 result = fcntl (fd, F_DUPFD, target);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
482 #endif
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
483 return result;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
484 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
485
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
486 static int
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
487 rpl_fcntl_DUPFD_CLOEXEC (int fd, int target)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
488 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
489 int result;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
490 #if !HAVE_FCNTL
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
491 result = dupfd (fd, target, O_CLOEXEC);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
492 #else /* HAVE_FCNTL */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
493 # if defined __HAIKU__
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
494 /* On Haiku, the system fcntl (fd, F_DUPFD_CLOEXEC, target) sets
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
495 the FD_CLOEXEC flag on fd, not on target. Therefore avoid the
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
496 system fcntl in this case. */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
497 # define have_dupfd_cloexec -1
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
498 # else
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
499 /* Try the system call first, if the headers claim it exists
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
500 (that is, if GNULIB_defined_F_DUPFD_CLOEXEC is 0), since we
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
501 may be running with a glibc that has the macro but with an
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
502 older kernel that does not support it. Cache the
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
503 information on whether the system call really works, but
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
504 avoid caching failure if the corresponding F_DUPFD fails
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
505 for any reason. 0 = unknown, 1 = yes, -1 = no. */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
506 static int have_dupfd_cloexec = GNULIB_defined_F_DUPFD_CLOEXEC ? -1 : 0;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
507 if (0 <= have_dupfd_cloexec)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
508 {
39882
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
509 result = fcntl (fd, F_DUPFD_CLOEXEC, target);
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
510 if (0 <= result || errno != EINVAL)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
511 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
512 have_dupfd_cloexec = 1;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
513 # if REPLACE_FCHDIR
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
514 if (0 <= result)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
515 result = _gl_register_dup (fd, result);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
516 # endif
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
517 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
518 else
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
519 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
520 result = rpl_fcntl_DUPFD (fd, target);
39882
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
521 if (result >= 0)
423dd81f09f4 fcntl: Make it possible to namespace the defined symbol, part 2.
Bruno Haible <bruno@clisp.org>
parents: 39880
diff changeset
522 have_dupfd_cloexec = -1;
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
523 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
524 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
525 else
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
526 # endif
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
527 result = rpl_fcntl_DUPFD (fd, target);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
528 if (0 <= result && have_dupfd_cloexec == -1)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
529 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
530 int flags = fcntl (result, F_GETFD);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
531 if (flags < 0 || fcntl (result, F_SETFD, flags | FD_CLOEXEC) == -1)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
532 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
533 int saved_errno = errno;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
534 close (result);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
535 errno = saved_errno;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
536 result = -1;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
537 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
538 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
539 #endif /* HAVE_FCNTL */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
540 return result;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
541 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
542
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
543 #undef fcntl
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
544
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
545 #ifdef __KLIBC__
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
546
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
547 static int
40105
ed6a8b3113de fcntl: Fix syntax error (regression from 2018-10-05).
KO Myung-Hun <komh78@gmail.com>
parents: 40057
diff changeset
548 klibc_fcntl (int fd, int action, /* arg */...)
39880
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
549 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
550 va_list arg_ptr;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
551 int arg;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
552 struct stat sbuf;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
553 int result;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
554
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
555 va_start (arg_ptr, action);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
556 arg = va_arg (arg_ptr, int);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
557 result = fcntl (fd, action, arg);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
558 /* EPERM for F_DUPFD, ENOTSUP for others */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
559 if (result == -1 && (errno == EPERM || errno == ENOTSUP)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
560 && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
561 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
562 ULONG ulMode;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
563
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
564 switch (action)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
565 {
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
566 case F_DUPFD:
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
567 /* Find available fd */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
568 while (fcntl (arg, F_GETFL) != -1 || errno != EBADF)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
569 arg++;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
570
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
571 result = dup2 (fd, arg);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
572 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
573
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
574 /* Using underlying APIs is right ? */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
575 case F_GETFD:
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
576 if (DosQueryFHState (fd, &ulMode))
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
577 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
578
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
579 result = (ulMode & OPEN_FLAGS_NOINHERIT) ? FD_CLOEXEC : 0;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
580 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
581
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
582 case F_SETFD:
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
583 if (arg & ~FD_CLOEXEC)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
584 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
585
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
586 if (DosQueryFHState (fd, &ulMode))
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
587 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
588
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
589 if (arg & FD_CLOEXEC)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
590 ulMode |= OPEN_FLAGS_NOINHERIT;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
591 else
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
592 ulMode &= ~OPEN_FLAGS_NOINHERIT;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
593
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
594 /* Filter supported flags. */
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
595 ulMode &= (OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
596 | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
597
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
598 if (DosSetFHState (fd, ulMode))
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
599 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
600
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
601 result = 0;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
602 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
603
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
604 case F_GETFL:
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
605 result = 0;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
606 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
607
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
608 case F_SETFL:
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
609 if (arg != 0)
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
610 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
611
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
612 result = 0;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
613 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
614
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
615 default:
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
616 errno = EINVAL;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
617 break;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
618 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
619 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
620
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
621 va_end (arg_ptr);
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
622
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
623 return result;
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
624 }
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
625
dfddbe1350b4 fcntl: Make it possible to namespace the defined symbol.
Bruno Haible <bruno@clisp.org>
parents: 39827
diff changeset
626 #endif