stdio-write.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* POSIX compatible FILE stream write function.
  2. Copyright (C) 2008 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 'sigpipe' is requested. */
  18. #if GNULIB_SIGPIPE
  19. /* On native Windows platforms, SIGPIPE does not exist. When write() is
  20. called on a pipe with no readers, WriteFile() fails with error
  21. GetLastError() = ERROR_NO_DATA, and write() in consequence fails with
  22. error EINVAL. This write() function is at the basis of the function
  23. which flushes the buffer of a FILE stream. */
  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. # define CALL_WITH_SIGPIPE_EMULATION(RETTYPE, EXPRESSION, FAILED) \
  31. if (ferror (stream)) \
  32. return (EXPRESSION); \
  33. else \
  34. { \
  35. RETTYPE ret; \
  36. SetLastError (0); \
  37. ret = (EXPRESSION); \
  38. if (FAILED && GetLastError () == ERROR_NO_DATA && ferror (stream)) \
  39. { \
  40. int fd = fileno (stream); \
  41. if (fd >= 0 \
  42. && GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_PIPE)\
  43. { \
  44. /* Try to raise signal SIGPIPE. */ \
  45. raise (SIGPIPE); \
  46. /* If it is currently blocked or ignored, change errno from \
  47. EINVAL to EPIPE. */ \
  48. errno = EPIPE; \
  49. } \
  50. } \
  51. return ret; \
  52. }
  53. # if !REPLACE_PRINTF_POSIX /* avoid collision with printf.c */
  54. int
  55. printf (const char *format, ...)
  56. {
  57. int retval;
  58. va_list args;
  59. va_start (args, format);
  60. retval = vfprintf (stdout, format, args);
  61. va_end (args);
  62. return retval;
  63. }
  64. # endif
  65. # if !REPLACE_FPRINTF_POSIX /* avoid collision with fprintf.c */
  66. int
  67. fprintf (FILE *stream, const char *format, ...)
  68. {
  69. int retval;
  70. va_list args;
  71. va_start (args, format);
  72. retval = vfprintf (stream, format, args);
  73. va_end (args);
  74. return retval;
  75. }
  76. # endif
  77. # if !REPLACE_VFPRINTF_POSIX /* avoid collision with vprintf.c */
  78. int
  79. vprintf (const char *format, va_list args)
  80. {
  81. return vfprintf (stdout, format, args);
  82. }
  83. # endif
  84. # if !REPLACE_VPRINTF_POSIX /* avoid collision with vfprintf.c */
  85. int
  86. vfprintf (FILE *stream, const char *format, va_list args)
  87. #undef vfprintf
  88. {
  89. CALL_WITH_SIGPIPE_EMULATION (int, vfprintf (stream, format, args), ret == EOF)
  90. }
  91. # endif
  92. int
  93. putchar (int c)
  94. {
  95. return fputc (c, stdout);
  96. }
  97. int
  98. fputc (int c, FILE *stream)
  99. #undef fputc
  100. {
  101. CALL_WITH_SIGPIPE_EMULATION (int, fputc (c, stream), ret == EOF)
  102. }
  103. int
  104. fputs (const char *string, FILE *stream)
  105. #undef fputs
  106. {
  107. CALL_WITH_SIGPIPE_EMULATION (int, fputs (string, stream), ret == EOF)
  108. }
  109. int
  110. puts (const char *string)
  111. #undef puts
  112. {
  113. FILE *stream = stdout;
  114. CALL_WITH_SIGPIPE_EMULATION (int, puts (string), ret == EOF)
  115. }
  116. size_t
  117. fwrite (const void *ptr, size_t s, size_t n, FILE *stream)
  118. #undef fwrite
  119. {
  120. CALL_WITH_SIGPIPE_EMULATION (size_t, fwrite (ptr, s, n, stream), ret < n)
  121. }
  122. # endif
  123. #endif