Merge commit 'b572c3a256e7bf1e4eecc8c36448c08093240a6a' into stable
[gnulib.git] / tests / test-pipe.c
1 /* Test of pipe.
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 (pipe, int, (int[2]));
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 /* Get _get_osfhandle.  */
33 # include "msvc-nothrow.h"
34 #endif
35
36 #include "binary-io.h"
37 #include "macros.h"
38
39 /* Return true if FD is open.  */
40 static bool
41 is_open (int fd)
42 {
43 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
44   /* On Win32, the initial state of unassigned standard file
45      descriptors is that they are open but point to an
46      INVALID_HANDLE_VALUE, and there is no fcntl.  */
47   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
48 #else
49 # ifndef F_GETFL
50 #  error Please port fcntl to your platform
51 # endif
52   return 0 <= fcntl (fd, F_GETFL);
53 #endif
54 }
55
56 /* Return true if FD is not inherited to child processes.  */
57 static bool
58 is_cloexec (int fd)
59 {
60 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
61   HANDLE h = (HANDLE) _get_osfhandle (fd);
62   DWORD flags;
63   ASSERT (GetHandleInformation (h, &flags));
64   return (flags & HANDLE_FLAG_INHERIT) == 0;
65 #else
66   int flags;
67   ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
68   return (flags & FD_CLOEXEC) != 0;
69 #endif
70 }
71
72 /* Return true if FD is in non-blocking mode.  */
73 static bool
74 is_nonblocking (int fd)
75 {
76 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
77   /* We don't use the non-blocking mode for sockets here.  */
78   return 0;
79 #else
80   int flags;
81   ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0);
82   return (flags & O_NONBLOCK) != 0;
83 #endif
84 }
85
86 int
87 main ()
88 {
89   int fd[2];
90
91   fd[0] = -1;
92   fd[1] = -1;
93   ASSERT (pipe (fd) >= 0);
94   ASSERT (fd[0] >= 0);
95   ASSERT (fd[1] >= 0);
96   ASSERT (fd[0] != fd[1]);
97   ASSERT (is_open (fd[0]));
98   ASSERT (is_open (fd[1]));
99   ASSERT (!is_cloexec (fd[0]));
100   ASSERT (!is_cloexec (fd[1]));
101   ASSERT (!is_nonblocking (fd[0]));
102   ASSERT (!is_nonblocking (fd[1]));
103
104   return 0;
105 }