2 Copyright (C) 2009-2018 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));
27 #if defined _WIN32 && ! defined __CYGWIN__
28 /* Get declarations of the native Windows API functions. */
29 # define WIN32_LEAN_AND_MEAN
31 /* Get _get_osfhandle. */
32 # if GNULIB_MSVC_NOTHROW
33 # include "msvc-nothrow.h"
39 #include "binary-io.h"
41 #if GNULIB_NONBLOCKING
42 # include "nonblocking.h"
45 /* Return true if FD is open. */
49 #if defined _WIN32 && ! defined __CYGWIN__
50 /* On native Windows, the initial state of unassigned standard file
51 descriptors is that they are open but point to an
52 INVALID_HANDLE_VALUE, and there is no fcntl. */
53 return (HANDLE
) _get_osfhandle (fd
) != INVALID_HANDLE_VALUE
;
56 # error Please port fcntl to your platform
58 return 0 <= fcntl (fd
, F_GETFL
);
62 /* Return true if FD is not inherited to child processes. */
66 #if defined _WIN32 && ! defined __CYGWIN__
67 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
69 ASSERT (GetHandleInformation (h
, &flags
));
70 return (flags
& HANDLE_FLAG_INHERIT
) == 0;
73 ASSERT ((flags
= fcntl (fd
, F_GETFD
)) >= 0);
74 return (flags
& FD_CLOEXEC
) != 0;
78 #if ! GNULIB_NONBLOCKING
80 get_nonblocking_flag (int fd
)
82 # if defined _WIN32 && ! defined __CYGWIN__
86 # error Please port fcntl to your platform
89 ASSERT ((flags
= fcntl (fd
, F_GETFL
)) >= 0);
90 return (flags
& O_NONBLOCK
) != 0;
101 for (use_nonblocking
= 0; use_nonblocking
<= !!O_NONBLOCK
; use_nonblocking
++)
102 for (use_cloexec
= 0; use_cloexec
<= !!O_CLOEXEC
; use_cloexec
++)
109 o_flags
|= O_NONBLOCK
;
111 o_flags
|= O_CLOEXEC
;
115 ASSERT (pipe2 (fd
, o_flags
) >= 0);
118 ASSERT (fd
[0] != fd
[1]);
119 ASSERT (is_open (fd
[0]));
120 ASSERT (is_open (fd
[1]));
123 ASSERT (is_cloexec (fd
[0]));
124 ASSERT (is_cloexec (fd
[1]));
128 ASSERT (!is_cloexec (fd
[0]));
129 ASSERT (!is_cloexec (fd
[1]));
133 ASSERT (get_nonblocking_flag (fd
[0]) == 1);
134 ASSERT (get_nonblocking_flag (fd
[1]) == 1);
138 ASSERT (get_nonblocking_flag (fd
[0]) == 0);
139 ASSERT (get_nonblocking_flag (fd
[1]) == 0);
142 ASSERT (close (fd
[0]) == 0);
143 ASSERT (close (fd
[1]) == 0);