123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #include <config.h>
- #include <unistd.h>
- #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
- # include <errno.h>
- # include <signal.h>
- # include <io.h>
- # define WIN32_LEAN_AND_MEAN
- # include <windows.h>
- # include "msvc-inval.h"
- # include "msvc-nothrow.h"
- # undef write
- # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
- static ssize_t
- write_nothrow (int fd, const void *buf, size_t count)
- {
- ssize_t result;
- TRY_MSVC_INVAL
- {
- result = write (fd, buf, count);
- }
- CATCH_MSVC_INVAL
- {
- result = -1;
- errno = EBADF;
- }
- DONE_MSVC_INVAL;
- return result;
- }
- # else
- # define write_nothrow write
- # endif
- ssize_t
- rpl_write (int fd, const void *buf, size_t count)
- {
- for (;;)
- {
- ssize_t ret = write_nothrow (fd, buf, count);
- if (ret < 0)
- {
- # if GNULIB_NONBLOCKING
- if (errno == ENOSPC)
- {
- HANDLE h = (HANDLE) _get_osfhandle (fd);
- if (GetFileType (h) == FILE_TYPE_PIPE)
- {
-
- DWORD state;
- if (GetNamedPipeHandleState (h, &state, NULL, NULL, NULL,
- NULL, 0)
- && (state & PIPE_NOWAIT) != 0)
- {
-
- DWORD out_size;
- DWORD in_size;
- if (GetNamedPipeInfo (h, NULL, &out_size, &in_size, NULL))
- {
- size_t reduced_count = count;
-
- if (out_size != 0 && out_size < reduced_count)
- reduced_count = out_size;
- if (in_size != 0 && in_size < reduced_count)
- reduced_count = in_size;
- if (reduced_count < count)
- {
-
- count = reduced_count;
- continue;
- }
- }
-
- errno = EAGAIN;
- }
- }
- }
- else
- # endif
- {
- # if GNULIB_SIGPIPE
- if (GetLastError () == ERROR_NO_DATA
- && GetFileType ((HANDLE) _get_osfhandle (fd))
- == FILE_TYPE_PIPE)
- {
-
- raise (SIGPIPE);
-
- errno = EPIPE;
- }
- # endif
- }
- }
- return ret;
- }
- }
- #endif
|