fpurge.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Flushing buffers of a FILE stream.
  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. #include <config.h>
  14. /* Specification. */
  15. #include <stdio.h>
  16. #if HAVE___FPURGE /* glibc >= 2.2, Haiku, Solaris >= 7 */
  17. # include <stdio_ext.h>
  18. #endif
  19. #include <stdlib.h>
  20. #include "stdio-impl.h"
  21. int
  22. fpurge (FILE *fp)
  23. {
  24. #if HAVE___FPURGE /* glibc >= 2.2, Haiku, Solaris >= 7, musl libc */
  25. __fpurge (fp);
  26. /* The __fpurge function does not have a return value. */
  27. return 0;
  28. #elif HAVE_FPURGE /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin 1.7 */
  29. /* Call the system's fpurge function. */
  30. # undef fpurge
  31. # if !HAVE_DECL_FPURGE
  32. extern int fpurge (FILE *);
  33. # endif
  34. int result = fpurge (fp);
  35. # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  36. if (result == 0)
  37. /* Correct the invariants that fpurge broke.
  38. <stdio.h> on BSD systems says:
  39. "The following always hold: if _flags & __SRD, _w is 0."
  40. If this invariant is not fulfilled and the stream is read-write but
  41. currently reading, subsequent putc or fputc calls will write directly
  42. into the buffer, although they shouldn't be allowed to. */
  43. if ((fp_->_flags & __SRD) != 0)
  44. fp_->_w = 0;
  45. # endif
  46. return result;
  47. #else
  48. /* Most systems provide FILE as a struct and the necessary bitmask in
  49. <stdio.h>, because they need it for implementing getc() and putc() as
  50. fast macros. */
  51. # if defined _IO_EOF_SEEN || defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  52. fp->_IO_read_end = fp->_IO_read_ptr;
  53. fp->_IO_write_ptr = fp->_IO_write_base;
  54. /* Avoid memory leak when there is an active ungetc buffer. */
  55. if (fp->_IO_save_base != NULL)
  56. {
  57. free (fp->_IO_save_base);
  58. fp->_IO_save_base = NULL;
  59. }
  60. return 0;
  61. # elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
  62. fp_->_p = fp_->_bf._base;
  63. fp_->_r = 0;
  64. fp_->_w = ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
  65. ? fp_->_bf._size
  66. : 0);
  67. /* Avoid memory leak when there is an active ungetc buffer. */
  68. if (fp_ub._base != NULL)
  69. {
  70. if (fp_ub._base != fp_->_ubuf)
  71. free (fp_ub._base);
  72. fp_ub._base = NULL;
  73. }
  74. return 0;
  75. # elif defined __EMX__ /* emx+gcc */
  76. fp->_ptr = fp->_buffer;
  77. fp->_rcount = 0;
  78. fp->_wcount = 0;
  79. fp->_ungetc_count = 0;
  80. return 0;
  81. # elif defined __minix /* Minix */
  82. fp->_ptr = fp->_buf;
  83. if (fp->_ptr != NULL)
  84. fp->_count = 0;
  85. return 0;
  86. # elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
  87. fp->_ptr = fp->_base;
  88. if (fp->_ptr != NULL)
  89. fp->_cnt = 0;
  90. return 0;
  91. # elif WIN_SDK10
  92. ((TWinSdk10File*)fp)->_ptr = ((TWinSdk10File*)fp)->_base;
  93. if (((TWinSdk10File*)fp)->_ptr != NULL)
  94. ((TWinSdk10File*)fp)->_cnt = 0;
  95. return 0;
  96. # elif defined __UCLIBC__ /* uClibc */
  97. # ifdef __STDIO_BUFFERS
  98. if (fp->__modeflags & __FLAG_WRITING)
  99. fp->__bufpos = fp->__bufstart;
  100. else if (fp->__modeflags & (__FLAG_READONLY | __FLAG_READING))
  101. fp->__bufpos = fp->__bufread;
  102. # endif
  103. return 0;
  104. # elif defined __QNX__ /* QNX */
  105. fp->_Rback = fp->_Back + sizeof (fp->_Back);
  106. fp->_Rsave = NULL;
  107. if (fp->_Mode & 0x2000 /* _MWRITE */)
  108. /* fp->_Buf <= fp->_Next <= fp->_Wend */
  109. fp->_Next = fp->_Buf;
  110. else
  111. /* fp->_Buf <= fp->_Next <= fp->_Rend */
  112. fp->_Rend = fp->_Next;
  113. return 0;
  114. # elif defined __MINT__ /* Atari FreeMiNT */
  115. if (fp->__pushed_back)
  116. {
  117. fp->__bufp = fp->__pushback_bufp;
  118. fp->__pushed_back = 0;
  119. }
  120. /* Preserve the current file position. */
  121. if (fp->__target != -1)
  122. fp->__target += fp->__bufp - fp->__buffer;
  123. fp->__bufp = fp->__buffer;
  124. /* Nothing in the buffer, next getc is nontrivial. */
  125. fp->__get_limit = fp->__bufp;
  126. /* Nothing in the buffer, next putc is nontrivial. */
  127. fp->__put_limit = fp->__buffer;
  128. return 0;
  129. # elif defined EPLAN9 /* Plan9 */
  130. fp->rp = fp->wp = fp->lp = fp->buf;
  131. return 0;
  132. # else
  133. # error "Please port gnulib fpurge.c to your platform! Look at the definitions of fflush, setvbuf and ungetc on your system, then report this to bug-gnulib."
  134. # endif
  135. #endif
  136. }