2 Copyright (C) 2009-2011 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "signature.h"
23 SIGNATURE_CHECK (pipe2, int, (int[2], int));
28 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
29 /* Get declarations of the Win32 API functions. */
30 # define WIN32_LEAN_AND_MEAN
32 /* Get _get_osfhandle. */
33 # include "msvc-nothrow.h"
36 #include "binary-io.h"
38 #if GNULIB_NONBLOCKING
39 # include "nonblocking.h"
42 /* Return true if FD is open. */
46 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
47 /* On Win32, the initial state of unassigned standard file
48 descriptors is that they are open but point to an
49 INVALID_HANDLE_VALUE, and there is no fcntl. */
50 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
53 # error Please port fcntl to your platform
55 return 0 <= fcntl (fd, F_GETFL);
59 /* Return true if FD is not inherited to child processes. */
63 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
64 HANDLE h = (HANDLE) _get_osfhandle (fd);
66 ASSERT (GetHandleInformation (h, &flags));
67 return (flags & HANDLE_FLAG_INHERIT) == 0;
70 ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
71 return (flags & FD_CLOEXEC) != 0;
75 #if ! GNULIB_NONBLOCKING
77 get_nonblocking_flag (int fd)
79 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
83 # error Please port fcntl to your platform
86 ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0);
87 return (flags & O_NONBLOCK) != 0;
98 for (use_nonblocking = 0; use_nonblocking <= !!O_NONBLOCK; use_nonblocking++)
99 for (use_cloexec = 0; use_cloexec <= !!O_CLOEXEC; use_cloexec++)
106 o_flags |= O_NONBLOCK;
108 o_flags |= O_CLOEXEC;
112 ASSERT (pipe2 (fd, o_flags) >= 0);
115 ASSERT (fd[0] != fd[1]);
116 ASSERT (is_open (fd[0]));
117 ASSERT (is_open (fd[1]));
120 ASSERT (is_cloexec (fd[0]));
121 ASSERT (is_cloexec (fd[1]));
125 ASSERT (!is_cloexec (fd[0]));
126 ASSERT (!is_cloexec (fd[1]));
130 ASSERT (get_nonblocking_flag (fd[0]) == 1);
131 ASSERT (get_nonblocking_flag (fd[1]) == 1);
135 ASSERT (get_nonblocking_flag (fd[0]) == 0);
136 ASSERT (get_nonblocking_flag (fd[1]) == 0);
139 ASSERT (close (fd[0]) == 0);
140 ASSERT (close (fd[1]) == 0);