This source file includes following definitions.
- write_nothrow
- rpl_write
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <config.h>
19
20
21 #include <unistd.h>
22
23
24
25
26
27
28 #if defined _WIN32 && ! defined __CYGWIN__
29
30 # include <errno.h>
31 # include <signal.h>
32 # include <io.h>
33
34 # define WIN32_LEAN_AND_MEAN
35 # include <windows.h>
36
37 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
38 # include "msvc-inval.h"
39 # endif
40 # if GNULIB_MSVC_NOTHROW
41 # include "msvc-nothrow.h"
42 # else
43 # include <io.h>
44 # endif
45
46
47 # undef GetNamedPipeHandleState
48 # define GetNamedPipeHandleState GetNamedPipeHandleStateA
49
50 # undef write
51
52 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
53 static ssize_t
54 write_nothrow (int fd, const void *buf, size_t count)
55 {
56 ssize_t result;
57
58 TRY_MSVC_INVAL
59 {
60 result = _write (fd, buf, count);
61 }
62 CATCH_MSVC_INVAL
63 {
64 result = -1;
65 errno = EBADF;
66 }
67 DONE_MSVC_INVAL;
68
69 return result;
70 }
71 # else
72 # define write_nothrow _write
73 # endif
74
75 ssize_t
76 rpl_write (int fd, const void *buf, size_t count)
77 {
78 for (;;)
79 {
80 ssize_t ret = write_nothrow (fd, buf, count);
81
82 if (ret < 0)
83 {
84 # if GNULIB_NONBLOCKING
85 if (errno == ENOSPC)
86 {
87 HANDLE h = (HANDLE) _get_osfhandle (fd);
88 if (GetFileType (h) == FILE_TYPE_PIPE)
89 {
90
91 DWORD state;
92 if (GetNamedPipeHandleState (h, &state, NULL, NULL, NULL,
93 NULL, 0)
94 && (state & PIPE_NOWAIT) != 0)
95 {
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 DWORD out_size;
111 DWORD in_size;
112 if (GetNamedPipeInfo (h, NULL, &out_size, &in_size, NULL))
113 {
114 size_t reduced_count = count;
115
116
117
118 if (out_size != 0 && out_size < reduced_count)
119 reduced_count = out_size;
120 if (in_size != 0 && in_size < reduced_count)
121 reduced_count = in_size;
122 if (reduced_count < count)
123 {
124
125 count = reduced_count;
126 continue;
127 }
128 }
129
130 errno = EAGAIN;
131 }
132 }
133 }
134 else
135 # endif
136 {
137 # if GNULIB_SIGPIPE
138 if (GetLastError () == ERROR_NO_DATA
139 && GetFileType ((HANDLE) _get_osfhandle (fd))
140 == FILE_TYPE_PIPE)
141 {
142
143 raise (SIGPIPE);
144
145
146 errno = EPIPE;
147 }
148 # endif
149 }
150 }
151 return ret;
152 }
153 }
154
155 #endif