2 Copyright (C) 2009-2013 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 <http://www.gnu.org/licenses/>. */
21 #include "signature.h"
22 SIGNATURE_CHECK (pipe2, int, (int[2], int));
27 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
28 /* Get declarations of the native Windows API functions. */
29 # define WIN32_LEAN_AND_MEAN
31 /* Get _get_osfhandle. */
32 # include "msvc-nothrow.h"
35 #include "binary-io.h"
37 #if GNULIB_NONBLOCKING
38 # include "nonblocking.h"
41 /* Return true if FD is open. */
45 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
46 /* On native Windows, the initial state of unassigned standard file
47 descriptors is that they are open but point to an
48 INVALID_HANDLE_VALUE, and there is no fcntl. */
49 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
52 # error Please port fcntl to your platform
54 return 0 <= fcntl (fd, F_GETFL);
58 /* Return true if FD is not inherited to child processes. */
62 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
63 HANDLE h = (HANDLE) _get_osfhandle (fd);
65 ASSERT (GetHandleInformation (h, &flags));
66 return (flags & HANDLE_FLAG_INHERIT) == 0;
69 ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
70 return (flags & FD_CLOEXEC) != 0;
74 #if ! GNULIB_NONBLOCKING
76 get_nonblocking_flag (int fd)
78 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
82 # error Please port fcntl to your platform
85 ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0);
86 return (flags & O_NONBLOCK) != 0;
97 for (use_nonblocking = 0; use_nonblocking <= !!O_NONBLOCK; use_nonblocking++)
98 for (use_cloexec = 0; use_cloexec <= !!O_CLOEXEC; use_cloexec++)
105 o_flags |= O_NONBLOCK;
107 o_flags |= O_CLOEXEC;
111 ASSERT (pipe2 (fd, o_flags) >= 0);
114 ASSERT (fd[0] != fd[1]);
115 ASSERT (is_open (fd[0]));
116 ASSERT (is_open (fd[1]));
119 ASSERT (is_cloexec (fd[0]));
120 ASSERT (is_cloexec (fd[1]));
124 ASSERT (!is_cloexec (fd[0]));
125 ASSERT (!is_cloexec (fd[1]));
129 ASSERT (get_nonblocking_flag (fd[0]) == 1);
130 ASSERT (get_nonblocking_flag (fd[1]) == 1);
134 ASSERT (get_nonblocking_flag (fd[0]) == 0);
135 ASSERT (get_nonblocking_flag (fd[1]) == 0);
138 ASSERT (close (fd[0]) == 0);
139 ASSERT (close (fd[1]) == 0);