error: Avoid "function declaration isn't a prototype" warning.
[gnulib.git] / tests / test-pipe.c
blobd6dc26733404a59073e71eacbdf7cd1e3fce37ae
1 /* Test of pipe.
2 Copyright (C) 2009-2017 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)
7 any later version.
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/>. */
17 #include <config.h>
19 #include <unistd.h>
21 #include "signature.h"
22 SIGNATURE_CHECK (pipe, int, (int[2]));
24 #include <fcntl.h>
25 #include <stdbool.h>
27 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
28 /* Get declarations of the native Windows API functions. */
29 # define WIN32_LEAN_AND_MEAN
30 # include <windows.h>
31 /* Get _get_osfhandle. */
32 # include "msvc-nothrow.h"
33 #endif
35 #include "binary-io.h"
36 #include "macros.h"
38 /* Return true if FD is open. */
39 static bool
40 is_open (int fd)
42 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
43 /* On native Windows, 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
55 /* Return true if FD is not inherited to child processes. */
56 static bool
57 is_cloexec (int fd)
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
71 /* Return true if FD is in non-blocking mode. */
72 static bool
73 is_nonblocking (int fd)
75 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
76 /* We don't use the non-blocking mode for sockets here. */
77 return 0;
78 #else
79 int flags;
80 ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0);
81 return (flags & O_NONBLOCK) != 0;
82 #endif
85 int
86 main ()
88 int fd[2];
90 fd[0] = -1;
91 fd[1] = -1;
92 ASSERT (pipe (fd) >= 0);
93 ASSERT (fd[0] >= 0);
94 ASSERT (fd[1] >= 0);
95 ASSERT (fd[0] != fd[1]);
96 ASSERT (is_open (fd[0]));
97 ASSERT (is_open (fd[1]));
98 ASSERT (!is_cloexec (fd[0]));
99 ASSERT (!is_cloexec (fd[1]));
100 ASSERT (!is_nonblocking (fd[0]));
101 ASSERT (!is_nonblocking (fd[1]));
103 return 0;