write.c 5.0 KB

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