pipe2: fix O_NONBLOCK support on mingw
[gnulib.git] / tests / test-pipe2.c
1 /* Test of pipe2.
2    Copyright (C) 2009-2011 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 #include <config.h>
19
20 #include <unistd.h>
21
22 #include "signature.h"
23 SIGNATURE_CHECK (pipe2, int, (int[2], int));
24
25 #include <fcntl.h>
26 #include <stdbool.h>
27
28 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
29 /* Get declarations of the Win32 API functions.  */
30 # define WIN32_LEAN_AND_MEAN
31 # include <windows.h>
32 #endif
33
34 #include "binary-io.h"
35 #include "macros.h"
36 #include "nonblocking.h"
37
38 /* Return true if FD is open.  */
39 static bool
40 is_open (int fd)
41 {
42 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
43   /* On Win32, the initial state of unassigned standard file
44      descriptors is that they are open but point to an
45      INVALID_HANDLE_VALUE, and there is no fcntl.  */
46   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
47 #else
48 # ifndef F_GETFL
49 #  error Please port fcntl to your platform
50 # endif
51   return 0 <= fcntl (fd, F_GETFL);
52 #endif
53 }
54
55 /* Return true if FD is not inherited to child processes.  */
56 static bool
57 is_cloexec (int fd)
58 {
59 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
60   HANDLE h = (HANDLE) _get_osfhandle (fd);
61   DWORD flags;
62   ASSERT (GetHandleInformation (h, &flags));
63   return (flags & HANDLE_FLAG_INHERIT) == 0;
64 #else
65   int flags;
66   ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
67   return (flags & FD_CLOEXEC) != 0;
68 #endif
69 }
70
71 int
72 main ()
73 {
74   int use_nonblocking;
75   int use_cloexec;
76
77   for (use_nonblocking = 0; use_nonblocking <= 1; use_nonblocking++)
78     for (use_cloexec = 0; use_cloexec <= !!O_CLOEXEC; use_cloexec++)
79       {
80         int o_flags;
81         int fd[2];
82
83         o_flags = 0;
84         if (use_nonblocking)
85           o_flags |= O_NONBLOCK;
86         if (use_cloexec)
87           o_flags |= O_CLOEXEC;
88
89         fd[0] = -1;
90         fd[1] = -1;
91         ASSERT (pipe2 (fd, o_flags) >= 0);
92         ASSERT (fd[0] >= 0);
93         ASSERT (fd[1] >= 0);
94         ASSERT (fd[0] != fd[1]);
95         ASSERT (is_open (fd[0]));
96         ASSERT (is_open (fd[1]));
97         if (use_cloexec)
98           {
99             ASSERT (is_cloexec (fd[0]));
100             ASSERT (is_cloexec (fd[1]));
101           }
102         else
103           {
104             ASSERT (!is_cloexec (fd[0]));
105             ASSERT (!is_cloexec (fd[1]));
106           }
107         if (use_nonblocking)
108           {
109             ASSERT (get_nonblocking_flag (fd[0]) == 1);
110             ASSERT (get_nonblocking_flag (fd[1]) == 1);
111           }
112         else
113           {
114             ASSERT (get_nonblocking_flag (fd[0]) == 0);
115             ASSERT (get_nonblocking_flag (fd[1]) == 0);
116           }
117
118         ASSERT (close (fd[0]) == 0);
119         ASSERT (close (fd[1]) == 0);
120       }
121
122   return 0;
123 }