stdio-write.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* POSIX compatible FILE stream write function.
  2. Copyright (C) 2008-2013 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 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <stdio.h>
  17. /* Replace these functions 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. This write() function is at the basis of the function
  24. which flushes the buffer of a FILE stream. */
  25. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  26. # include <errno.h>
  27. # include <signal.h>
  28. # include <io.h>
  29. # define WIN32_LEAN_AND_MEAN /* avoid including junk */
  30. # include <windows.h>
  31. # include "msvc-nothrow.h"
  32. # if GNULIB_NONBLOCKING
  33. # define CLEAR_ERRNO \
  34. errno = 0;
  35. # define HANDLE_ENOSPC \
  36. if (errno == ENOSPC && ferror (stream)) \
  37. { \
  38. int fd = fileno (stream); \
  39. if (fd >= 0) \
  40. { \
  41. HANDLE h = (HANDLE) _get_osfhandle (fd); \
  42. if (GetFileType (h) == FILE_TYPE_PIPE) \
  43. { \
  44. /* h is a pipe or socket. */ \
  45. DWORD state; \
  46. if (GetNamedPipeHandleState (h, &state, NULL, NULL, \
  47. NULL, NULL, 0) \
  48. && (state & PIPE_NOWAIT) != 0) \
  49. /* h is a pipe in non-blocking mode. \
  50. Change errno from ENOSPC to EAGAIN. */ \
  51. errno = EAGAIN; \
  52. } \
  53. } \
  54. } \
  55. else
  56. # else
  57. # define CLEAR_ERRNO
  58. # define HANDLE_ENOSPC
  59. # endif
  60. # if GNULIB_SIGPIPE
  61. # define CLEAR_LastError \
  62. SetLastError (0);
  63. # define HANDLE_ERROR_NO_DATA \
  64. if (GetLastError () == ERROR_NO_DATA && ferror (stream)) \
  65. { \
  66. int fd = fileno (stream); \
  67. if (fd >= 0 \
  68. && GetFileType ((HANDLE) _get_osfhandle (fd)) \
  69. == FILE_TYPE_PIPE) \
  70. { \
  71. /* Try to raise signal SIGPIPE. */ \
  72. raise (SIGPIPE); \
  73. /* If it is currently blocked or ignored, change errno from \
  74. EINVAL to EPIPE. */ \
  75. errno = EPIPE; \
  76. } \
  77. } \
  78. else
  79. # else
  80. # define CLEAR_LastError
  81. # define HANDLE_ERROR_NO_DATA
  82. # endif
  83. # define CALL_WITH_SIGPIPE_EMULATION(RETTYPE, EXPRESSION, FAILED) \
  84. if (ferror (stream)) \
  85. return (EXPRESSION); \
  86. else \
  87. { \
  88. RETTYPE ret; \
  89. CLEAR_ERRNO \
  90. CLEAR_LastError \
  91. ret = (EXPRESSION); \
  92. if (FAILED) \
  93. { \
  94. HANDLE_ENOSPC \
  95. HANDLE_ERROR_NO_DATA \
  96. ; \
  97. } \
  98. return ret; \
  99. }
  100. # if !REPLACE_PRINTF_POSIX /* avoid collision with printf.c */
  101. int
  102. printf (const char *format, ...)
  103. {
  104. int retval;
  105. va_list args;
  106. va_start (args, format);
  107. retval = vfprintf (stdout, format, args);
  108. va_end (args);
  109. return retval;
  110. }
  111. # endif
  112. # if !REPLACE_FPRINTF_POSIX /* avoid collision with fprintf.c */
  113. int
  114. fprintf (FILE *stream, const char *format, ...)
  115. {
  116. int retval;
  117. va_list args;
  118. va_start (args, format);
  119. retval = vfprintf (stream, format, args);
  120. va_end (args);
  121. return retval;
  122. }
  123. # endif
  124. # if !REPLACE_VPRINTF_POSIX /* avoid collision with vprintf.c */
  125. int
  126. vprintf (const char *format, va_list args)
  127. {
  128. return vfprintf (stdout, format, args);
  129. }
  130. # endif
  131. # if !REPLACE_VFPRINTF_POSIX /* avoid collision with vfprintf.c */
  132. int
  133. vfprintf (FILE *stream, const char *format, va_list args)
  134. #undef vfprintf
  135. {
  136. CALL_WITH_SIGPIPE_EMULATION (int, vfprintf (stream, format, args), ret == EOF)
  137. }
  138. # endif
  139. int
  140. putchar (int c)
  141. {
  142. return fputc (c, stdout);
  143. }
  144. int
  145. fputc (int c, FILE *stream)
  146. #undef fputc
  147. {
  148. CALL_WITH_SIGPIPE_EMULATION (int, fputc (c, stream), ret == EOF)
  149. }
  150. int
  151. fputs (const char *string, FILE *stream)
  152. #undef fputs
  153. {
  154. CALL_WITH_SIGPIPE_EMULATION (int, fputs (string, stream), ret == EOF)
  155. }
  156. int
  157. puts (const char *string)
  158. #undef puts
  159. {
  160. FILE *stream = stdout;
  161. CALL_WITH_SIGPIPE_EMULATION (int, puts (string), ret == EOF)
  162. }
  163. size_t
  164. fwrite (const void *ptr, size_t s, size_t n, FILE *stream)
  165. #undef fwrite
  166. {
  167. CALL_WITH_SIGPIPE_EMULATION (size_t, fwrite (ptr, s, n, stream), ret < n)
  168. }
  169. # endif
  170. #endif