stdio-write.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* POSIX compatible FILE stream write function.
  2. Copyright (C) 2008-2010 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. # if !DEPENDS_ON_LIBINTL /* avoid collision with intl/printf.c */
  55. int
  56. printf (const char *format, ...)
  57. {
  58. int retval;
  59. va_list args;
  60. va_start (args, format);
  61. retval = vfprintf (stdout, format, args);
  62. va_end (args);
  63. return retval;
  64. }
  65. # endif
  66. # endif
  67. # if !REPLACE_FPRINTF_POSIX /* avoid collision with fprintf.c */
  68. int
  69. fprintf (FILE *stream, const char *format, ...)
  70. {
  71. int retval;
  72. va_list args;
  73. va_start (args, format);
  74. retval = vfprintf (stream, format, args);
  75. va_end (args);
  76. return retval;
  77. }
  78. # endif
  79. # if !REPLACE_VPRINTF_POSIX /* avoid collision with vprintf.c */
  80. int
  81. vprintf (const char *format, va_list args)
  82. {
  83. return vfprintf (stdout, format, args);
  84. }
  85. # endif
  86. # if !REPLACE_VFPRINTF_POSIX /* avoid collision with vfprintf.c */
  87. int
  88. vfprintf (FILE *stream, const char *format, va_list args)
  89. #undef vfprintf
  90. {
  91. CALL_WITH_SIGPIPE_EMULATION (int, vfprintf (stream, format, args), ret == EOF)
  92. }
  93. # endif
  94. int
  95. putchar (int c)
  96. {
  97. return fputc (c, stdout);
  98. }
  99. int
  100. fputc (int c, FILE *stream)
  101. #undef fputc
  102. {
  103. CALL_WITH_SIGPIPE_EMULATION (int, fputc (c, stream), ret == EOF)
  104. }
  105. int
  106. fputs (const char *string, FILE *stream)
  107. #undef fputs
  108. {
  109. CALL_WITH_SIGPIPE_EMULATION (int, fputs (string, stream), ret == EOF)
  110. }
  111. int
  112. puts (const char *string)
  113. #undef puts
  114. {
  115. FILE *stream = stdout;
  116. CALL_WITH_SIGPIPE_EMULATION (int, puts (string), ret == EOF)
  117. }
  118. size_t
  119. fwrite (const void *ptr, size_t s, size_t n, FILE *stream)
  120. #undef fwrite
  121. {
  122. CALL_WITH_SIGPIPE_EMULATION (size_t, fwrite (ptr, s, n, stream), ret < n)
  123. }
  124. # endif
  125. #endif