1 /* POSIX compatible write() function.
2 Copyright (C) 2008-2011 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 Lesser 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Replace this function only if module 'nonblocking' or module 'sigpipe' is
25 #if GNULIB_NONBLOCKING || GNULIB_SIGPIPE
27 /* On native Windows platforms, SIGPIPE does not exist. When write() is
28 called on a pipe with no readers, WriteFile() fails with error
29 GetLastError() = ERROR_NO_DATA, and write() in consequence fails with
32 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
38 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
42 rpl_write (int fd
, const void *buf
, size_t count
)
47 ssize_t ret
= write (fd
, buf
, count
);
51 # if GNULIB_NONBLOCKING
54 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
55 if (GetFileType (h
) == FILE_TYPE_PIPE
)
57 /* h is a pipe or socket. */
59 if (GetNamedPipeHandleState (h
, &state
, NULL
, NULL
, NULL
,
61 && (state
& PIPE_NOWAIT
) != 0)
63 /* h is a pipe in non-blocking mode.
64 We can get here in four situations:
65 1. When the pipe buffer is full.
66 2. When count <= pipe_buf_size and the number of
67 free bytes in the pipe buffer is < count.
68 3. When count > pipe_buf_size and the number of free
69 bytes in the pipe buffer is > 0, < pipe_buf_size.
70 4. When count > pipe_buf_size and the pipe buffer is
72 The cases 1 and 2 are POSIX compliant. In cases 3 and
73 4 POSIX specifies that write() must split the request
74 and succeed with a partial write. We fix case 4.
75 We don't fix case 3 because it is not essential for
77 DWORD out_size
; /* size of the buffer for outgoing data */
78 DWORD in_size
; /* size of the buffer for incoming data */
79 if (GetNamedPipeInfo (h
, NULL
, &out_size
, &in_size
, NULL
))
81 size_t reduced_count
= count
;
82 /* In theory we need only one of out_size, in_size.
83 But I don't know which of the two. The description
85 if (out_size
!= 0 && out_size
< reduced_count
)
86 reduced_count
= out_size
;
87 if (in_size
!= 0 && in_size
< reduced_count
)
88 reduced_count
= in_size
;
89 if (reduced_count
< count
)
91 /* Attempt to write only the first part. */
92 count
= reduced_count
;
96 /* Change errno from ENOSPC to EAGAIN. */
105 if (GetLastError () == ERROR_NO_DATA
106 && GetFileType ((HANDLE
) _get_osfhandle (fd
))
109 /* Try to raise signal SIGPIPE. */
111 /* If it is currently blocked or ignored, change errno from