2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
21 #include "signature.h"
22 SIGNATURE_CHECK (pipe2
, int, (int[2], int));
26 #if defined _WIN32 && ! defined __CYGWIN__
27 /* Get declarations of the native Windows API functions. */
28 # define WIN32_LEAN_AND_MEAN
30 /* Get _get_osfhandle. */
31 # if GNULIB_MSVC_NOTHROW
32 # include "msvc-nothrow.h"
38 #include "binary-io.h"
40 #if GNULIB_NONBLOCKING
41 # include "nonblocking.h"
44 /* Return true if FD is open. */
48 #if defined _WIN32 && ! defined __CYGWIN__
49 /* On native Windows, the initial state of unassigned standard file
50 descriptors is that they are open but point to an
51 INVALID_HANDLE_VALUE, and there is no fcntl. */
52 return (HANDLE
) _get_osfhandle (fd
) != INVALID_HANDLE_VALUE
;
55 # error Please port fcntl to your platform
57 return 0 <= fcntl (fd
, F_GETFL
);
61 /* Return true if FD is not inherited to child processes. */
65 #if defined _WIN32 && ! defined __CYGWIN__
66 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
68 ASSERT (GetHandleInformation (h
, &flags
));
69 return (flags
& HANDLE_FLAG_INHERIT
) == 0;
72 ASSERT ((flags
= fcntl (fd
, F_GETFD
)) >= 0);
73 return (flags
& FD_CLOEXEC
) != 0;
77 #if ! GNULIB_NONBLOCKING
79 get_nonblocking_flag (int fd
)
81 # if defined _WIN32 && ! defined __CYGWIN__
85 # error Please port fcntl to your platform
88 ASSERT ((flags
= fcntl (fd
, F_GETFL
)) >= 0);
89 return (flags
& O_NONBLOCK
) != 0;
100 for (use_nonblocking
= 0; use_nonblocking
<= !!O_NONBLOCK
; use_nonblocking
++)
101 for (use_cloexec
= 0; use_cloexec
<= !!O_CLOEXEC
; use_cloexec
++)
108 o_flags
|= O_NONBLOCK
;
110 o_flags
|= O_CLOEXEC
;
114 ASSERT (pipe2 (fd
, o_flags
) >= 0);
117 ASSERT (fd
[0] != fd
[1]);
118 ASSERT (is_open (fd
[0]));
119 ASSERT (is_open (fd
[1]));
122 ASSERT (is_cloexec (fd
[0]));
123 ASSERT (is_cloexec (fd
[1]));
127 ASSERT (!is_cloexec (fd
[0]));
128 ASSERT (!is_cloexec (fd
[1]));
132 ASSERT (get_nonblocking_flag (fd
[0]) == 1);
133 ASSERT (get_nonblocking_flag (fd
[1]) == 1);
137 ASSERT (get_nonblocking_flag (fd
[0]) == 0);
138 ASSERT (get_nonblocking_flag (fd
[1]) == 0);
141 ASSERT (close (fd
[0]) == 0);
142 ASSERT (close (fd
[1]) == 0);
145 return test_exit_status
;