2 Copyright (C) 2009 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. */
25 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
26 /* Get declarations of the Win32 API functions. */
27 # define WIN32_LEAN_AND_MEAN
31 #include "binary-io.h"
33 #define ASSERT(expr) \
38 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
45 /* Return true if FD is open. */
49 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
50 /* On Win32, 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 __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 /* Return true if FD is in non-blocking mode. */
80 is_nonblocking (int fd)
82 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
83 /* We don't use the non-blocking mode for sockets here. */
87 ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0);
88 return (flags & O_NONBLOCK) != 0;
98 #if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
99 for (use_nonblocking = 0; use_nonblocking <= 1; use_nonblocking++)
103 #if defined O_CLOEXEC
104 for (use_cloexec = 0; use_cloexec <= 1; use_cloexec++)
113 #if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
115 o_flags |= O_NONBLOCK;
117 #if defined O_CLOEXEC
119 o_flags |= O_CLOEXEC;
124 ASSERT (pipe2 (fd, o_flags) >= 0);
127 ASSERT (fd[0] != fd[1]);
128 ASSERT (is_open (fd[0]) >= 0);
129 ASSERT (is_open (fd[1]) >= 0);
132 ASSERT (is_cloexec (fd[0]));
133 ASSERT (is_cloexec (fd[1]));
137 ASSERT (!is_cloexec (fd[0]));
138 ASSERT (!is_cloexec (fd[1]));
142 ASSERT (is_nonblocking (fd[0]));
143 ASSERT (is_nonblocking (fd[1]));
147 ASSERT (!is_nonblocking (fd[0]));
148 ASSERT (!is_nonblocking (fd[1]));