write.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* POSIX compatible write() function.
  2. Copyright (C) 2008-2011 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. /* Replace this function only if module 'nonblocking' or module 'sigpipe' is
  18. requested. */
  19. #if GNULIB_NONBLOCKING || GNULIB_SIGPIPE
  20. /* On native Windows platforms, SIGPIPE does not exist. When write() is
  21. called on a pipe with no readers, WriteFile() fails with error
  22. GetLastError() = ERROR_NO_DATA, and write() in consequence fails with
  23. error EINVAL. */
  24. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  25. # include <errno.h>
  26. # include <signal.h>
  27. # include <io.h>
  28. # define WIN32_LEAN_AND_MEAN /* avoid including junk */
  29. # include <windows.h>
  30. ssize_t
  31. rpl_write (int fd, const void *buf, size_t count)
  32. #undef write
  33. {
  34. for (;;)
  35. {
  36. ssize_t ret = write (fd, buf, count);
  37. if (ret < 0)
  38. {
  39. # if GNULIB_NONBLOCKING
  40. if (errno == ENOSPC)
  41. {
  42. HANDLE h = (HANDLE) _get_osfhandle (fd);
  43. if (GetFileType (h) == FILE_TYPE_PIPE)
  44. {
  45. /* h is a pipe or socket. */
  46. DWORD state;
  47. if (GetNamedPipeHandleState (h, &state, NULL, NULL, NULL,
  48. NULL, 0)
  49. && (state & PIPE_NOWAIT) != 0)
  50. {
  51. /* h is a pipe in non-blocking mode.
  52. We can get here in four situations:
  53. 1. When the pipe buffer is full.
  54. 2. When count <= pipe_buf_size and the number of
  55. free bytes in the pipe buffer is < count.
  56. 3. When count > pipe_buf_size and the number of free
  57. bytes in the pipe buffer is > 0, < pipe_buf_size.
  58. 4. When count > pipe_buf_size and the pipe buffer is
  59. entirely empty.
  60. The cases 1 and 2 are POSIX compliant. In cases 3 and
  61. 4 POSIX specifies that write() must split the request
  62. and succeed with a partial write. We fix case 4.
  63. We don't fix case 3 because it is not essential for
  64. programs. */
  65. DWORD out_size; /* size of the buffer for outgoing data */
  66. DWORD in_size; /* size of the buffer for incoming data */
  67. if (GetNamedPipeInfo (h, NULL, &out_size, &in_size, NULL))
  68. {
  69. size_t reduced_count = count;
  70. /* In theory we need only one of out_size, in_size.
  71. But I don't know which of the two. The description
  72. is ambiguous. */
  73. if (out_size != 0 && out_size < reduced_count)
  74. reduced_count = out_size;
  75. if (in_size != 0 && in_size < reduced_count)
  76. reduced_count = in_size;
  77. if (reduced_count < count)
  78. {
  79. /* Attempt to write only the first part. */
  80. count = reduced_count;
  81. continue;
  82. }
  83. }
  84. /* Change errno from ENOSPC to EAGAIN. */
  85. errno = EAGAIN;
  86. }
  87. }
  88. }
  89. else
  90. # endif
  91. {
  92. # if GNULIB_SIGPIPE
  93. if (GetLastError () == ERROR_NO_DATA
  94. && GetFileType ((HANDLE) _get_osfhandle (fd))
  95. == FILE_TYPE_PIPE)
  96. {
  97. /* Try to raise signal SIGPIPE. */
  98. raise (SIGPIPE);
  99. /* If it is currently blocked or ignored, change errno from
  100. EINVAL to EPIPE. */
  101. errno = EPIPE;
  102. }
  103. # endif
  104. }
  105. }
  106. return ret;
  107. }
  108. }
  109. # endif
  110. #endif