glob: Fix compilation error on NetBSD 7.0 and OpenBSD 6.0.
[gnulib/ericb.git] / lib / write.c
blobb81db620819d58441479ba342d04e5bb8fe6823d
1 /* POSIX compatible write() function.
2 Copyright (C) 2008-2017 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2008.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include <unistd.h>
23 /* On native Windows platforms, SIGPIPE does not exist. When write() is
24 called on a pipe with no readers, WriteFile() fails with error
25 GetLastError() = ERROR_NO_DATA, and write() in consequence fails with
26 error EINVAL. */
28 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
30 # include <errno.h>
31 # include <signal.h>
32 # include <io.h>
34 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
35 # include <windows.h>
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
46 # undef write
48 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
49 static ssize_t
50 write_nothrow (int fd, const void *buf, size_t count)
52 ssize_t result;
54 TRY_MSVC_INVAL
56 result = write (fd, buf, count);
58 CATCH_MSVC_INVAL
60 result = -1;
61 errno = EBADF;
63 DONE_MSVC_INVAL;
65 return result;
67 # else
68 # define write_nothrow write
69 # endif
71 ssize_t
72 rpl_write (int fd, const void *buf, size_t count)
74 for (;;)
76 ssize_t ret = write_nothrow (fd, buf, count);
78 if (ret < 0)
80 # if GNULIB_NONBLOCKING
81 if (errno == ENOSPC)
83 HANDLE h = (HANDLE) _get_osfhandle (fd);
84 if (GetFileType (h) == FILE_TYPE_PIPE)
86 /* h is a pipe or socket. */
87 DWORD state;
88 if (GetNamedPipeHandleState (h, &state, NULL, NULL, NULL,
89 NULL, 0)
90 && (state & PIPE_NOWAIT) != 0)
92 /* h is a pipe in non-blocking mode.
93 We can get here in four situations:
94 1. When the pipe buffer is full.
95 2. When count <= pipe_buf_size and the number of
96 free bytes in the pipe buffer is < count.
97 3. When count > pipe_buf_size and the number of free
98 bytes in the pipe buffer is > 0, < pipe_buf_size.
99 4. When count > pipe_buf_size and the pipe buffer is
100 entirely empty.
101 The cases 1 and 2 are POSIX compliant. In cases 3 and
102 4 POSIX specifies that write() must split the request
103 and succeed with a partial write. We fix case 4.
104 We don't fix case 3 because it is not essential for
105 programs. */
106 DWORD out_size; /* size of the buffer for outgoing data */
107 DWORD in_size; /* size of the buffer for incoming data */
108 if (GetNamedPipeInfo (h, NULL, &out_size, &in_size, NULL))
110 size_t reduced_count = count;
111 /* In theory we need only one of out_size, in_size.
112 But I don't know which of the two. The description
113 is ambiguous. */
114 if (out_size != 0 && out_size < reduced_count)
115 reduced_count = out_size;
116 if (in_size != 0 && in_size < reduced_count)
117 reduced_count = in_size;
118 if (reduced_count < count)
120 /* Attempt to write only the first part. */
121 count = reduced_count;
122 continue;
125 /* Change errno from ENOSPC to EAGAIN. */
126 errno = EAGAIN;
130 else
131 # endif
133 # if GNULIB_SIGPIPE
134 if (GetLastError () == ERROR_NO_DATA
135 && GetFileType ((HANDLE) _get_osfhandle (fd))
136 == FILE_TYPE_PIPE)
138 /* Try to raise signal SIGPIPE. */
139 raise (SIGPIPE);
140 /* If it is currently blocked or ignored, change errno from
141 EINVAL to EPIPE. */
142 errno = EPIPE;
144 # endif
147 return ret;
151 #endif