This source file includes following definitions.
- is_open
- is_cloexec
- is_nonblocking
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19 #include <unistd.h>
20
21 #include "signature.h"
22 SIGNATURE_CHECK (pipe, int, (int[2]));
23
24 #include <fcntl.h>
25 #include <stdbool.h>
26
27 #if defined _WIN32 && ! defined __CYGWIN__
28
29 # define WIN32_LEAN_AND_MEAN
30 # include <windows.h>
31
32 # if GNULIB_MSVC_NOTHROW
33 # include "msvc-nothrow.h"
34 # else
35 # include <io.h>
36 # endif
37 #endif
38
39 #include "binary-io.h"
40 #include "macros.h"
41
42
43 static bool
44 is_open (int fd)
45 {
46 #if defined _WIN32 && ! defined __CYGWIN__
47
48
49
50 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
51 #else
52 # ifndef F_GETFL
53 # error Please port fcntl to your platform
54 # endif
55 return 0 <= fcntl (fd, F_GETFL);
56 #endif
57 }
58
59
60 static bool
61 is_cloexec (int fd)
62 {
63 #if defined _WIN32 && ! defined __CYGWIN__
64 HANDLE h = (HANDLE) _get_osfhandle (fd);
65 DWORD flags;
66 ASSERT (GetHandleInformation (h, &flags));
67 return (flags & HANDLE_FLAG_INHERIT) == 0;
68 #else
69 int flags;
70 ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
71 return (flags & FD_CLOEXEC) != 0;
72 #endif
73 }
74
75
76 static bool
77 is_nonblocking (int fd)
78 {
79 #if defined _WIN32 && ! defined __CYGWIN__
80
81 return 0;
82 #else
83 int flags;
84 ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0);
85 return (flags & O_NONBLOCK) != 0;
86 #endif
87 }
88
89 int
90 main ()
91 {
92 int fd[2];
93
94 fd[0] = -1;
95 fd[1] = -1;
96 ASSERT (pipe (fd) >= 0);
97 ASSERT (fd[0] >= 0);
98 ASSERT (fd[1] >= 0);
99 ASSERT (fd[0] != fd[1]);
100 ASSERT (is_open (fd[0]));
101 ASSERT (is_open (fd[1]));
102 ASSERT (!is_cloexec (fd[0]));
103 ASSERT (!is_cloexec (fd[1]));
104 ASSERT (!is_nonblocking (fd[0]));
105 ASSERT (!is_nonblocking (fd[1]));
106
107 return 0;
108 }