fflush.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* fflush.c -- allow flushing input streams
  2. Copyright (C) 2007-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Eric Blake. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "stdio--.h"
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include "freading.h"
  20. #include "stdio-impl.h"
  21. #include "unused-parameter.h"
  22. #undef fflush
  23. #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  24. /* Clear the stream's ungetc buffer, preserving the value of ftello (fp). */
  25. static void
  26. clear_ungetc_buffer_preserving_position (FILE *fp)
  27. {
  28. if (fp->_flags & _IO_IN_BACKUP)
  29. /* _IO_free_backup_area is a bit complicated. Simply call fseek. */
  30. fseeko (fp, 0, SEEK_CUR);
  31. }
  32. #else
  33. /* Clear the stream's ungetc buffer. May modify the value of ftello (fp). */
  34. static void
  35. clear_ungetc_buffer (FILE *fp)
  36. {
  37. # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  38. if (HASUB (fp))
  39. {
  40. fp_->_p += fp_->_r;
  41. fp_->_r = 0;
  42. }
  43. # elif defined __EMX__ /* emx+gcc */
  44. if (fp->_ungetc_count > 0)
  45. {
  46. fp->_ungetc_count = 0;
  47. fp->_rcount = - fp->_rcount;
  48. }
  49. # elif defined _IOERR /* Minix, AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
  50. /* Nothing to do. */
  51. # else /* other implementations */
  52. fseeko (fp, 0, SEEK_CUR);
  53. # endif
  54. }
  55. #endif
  56. #if ! (defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */)
  57. # if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  58. static int
  59. disable_seek_optimization (FILE *fp)
  60. {
  61. int saved_flags = fp_->_flags & (__SOPT | __SNPT);
  62. fp_->_flags = (fp_->_flags & ~__SOPT) | __SNPT;
  63. return saved_flags;
  64. }
  65. static void
  66. restore_seek_optimization (FILE *fp, int saved_flags)
  67. {
  68. fp_->_flags = (fp_->_flags & ~(__SOPT | __SNPT)) | saved_flags;
  69. }
  70. # else
  71. static void
  72. update_fpos_cache (FILE *fp _GL_UNUSED_PARAMETER,
  73. off_t pos _GL_UNUSED_PARAMETER)
  74. {
  75. # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  76. # if defined __CYGWIN__
  77. /* fp_->_offset is typed as an integer. */
  78. fp_->_offset = pos;
  79. # else
  80. /* fp_->_offset is an fpos_t. */
  81. /* Use a union, since on NetBSD, the compilation flags determine
  82. whether fpos_t is typedef'd to off_t or a struct containing a
  83. single off_t member. */
  84. union
  85. {
  86. fpos_t f;
  87. off_t o;
  88. } u;
  89. u.o = pos;
  90. fp_->_offset = u.f;
  91. # endif
  92. fp_->_flags |= __SOFF;
  93. # endif
  94. }
  95. # endif
  96. #endif
  97. /* Flush all pending data on STREAM according to POSIX rules. Both
  98. output and seekable input streams are supported. */
  99. int
  100. rpl_fflush (FILE *stream)
  101. {
  102. /* When stream is NULL, POSIX and C99 only require flushing of "output
  103. streams and update streams in which the most recent operation was not
  104. input", and all implementations do this.
  105. When stream is "an output stream or an update stream in which the most
  106. recent operation was not input", POSIX and C99 requires that fflush
  107. writes out any buffered data, and all implementations do this.
  108. When stream is, however, an input stream or an update stream in
  109. which the most recent operation was input, C99 specifies nothing,
  110. and POSIX only specifies behavior if the stream is seekable.
  111. mingw, in particular, drops the input buffer, leaving the file
  112. descriptor positioned at the end of the input buffer. I.e. ftell
  113. (stream) is lost. We don't want to call the implementation's
  114. fflush in this case.
  115. We test ! freading (stream) here, rather than fwriting (stream), because
  116. what we need to know is whether the stream holds a "read buffer", and on
  117. mingw this is indicated by _IOREAD, regardless of _IOWRT. */
  118. if (stream == NULL || ! freading (stream))
  119. return fflush (stream);
  120. #if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  121. clear_ungetc_buffer_preserving_position (stream);
  122. return fflush (stream);
  123. #else
  124. {
  125. /* Notes about the file-position indicator:
  126. 1) The file position indicator is incremented by fgetc() and decremented
  127. by ungetc():
  128. <http://www.opengroup.org/susv3/functions/fgetc.html>
  129. "... the fgetc() function shall ... advance the associated file
  130. position indicator for the stream ..."
  131. <http://www.opengroup.org/susv3/functions/ungetc.html>
  132. "The file-position indicator is decremented by each successful
  133. call to ungetc()..."
  134. 2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
  135. "The value of the file-position indicator for the stream after
  136. reading or discarding all pushed-back bytes shall be the same
  137. as it was before the bytes were pushed back."
  138. Here we are discarding all pushed-back bytes. But more specifically,
  139. 3) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> says:
  140. "[After fflush(),] the file offset of the underlying open file
  141. description shall be set to the file position of the stream, and
  142. any characters pushed back onto the stream by ungetc() ... shall
  143. be discarded." */
  144. /* POSIX does not specify fflush behavior for non-seekable input
  145. streams. Some implementations purge unread data, some return
  146. EBADF, some do nothing. */
  147. off_t pos = ftello (stream);
  148. if (pos == -1)
  149. {
  150. errno = EBADF;
  151. return EOF;
  152. }
  153. /* Clear the ungetc buffer. */
  154. clear_ungetc_buffer (stream);
  155. /* To get here, we must be flushing a seekable input stream, so the
  156. semantics of fpurge are now appropriate to clear the buffer. To
  157. avoid losing data, the lseek is also necessary. */
  158. {
  159. int result = fpurge (stream);
  160. if (result != 0)
  161. return result;
  162. }
  163. # if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  164. {
  165. /* Disable seek optimization for the next fseeko call. This tells the
  166. following fseeko call to seek to the desired position directly, rather
  167. than to seek to a block-aligned boundary. */
  168. int saved_flags = disable_seek_optimization (stream);
  169. int result = fseeko (stream, pos, SEEK_SET);
  170. restore_seek_optimization (stream, saved_flags);
  171. return result;
  172. }
  173. # else
  174. pos = lseek (fileno (stream), pos, SEEK_SET);
  175. if (pos == -1)
  176. return EOF;
  177. /* After a successful lseek, update the file descriptor's position cache
  178. in the stream. */
  179. update_fpos_cache (stream, pos);
  180. return 0;
  181. # endif
  182. }
  183. #endif
  184. }