1 /* Flushing buffers of a FILE stream.
2 Copyright (C) 2007-2016 Free Software Foundation, Inc.
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.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #if HAVE___FPURGE /* glibc >= 2.2, Haiku, Solaris >= 7 */
23 # include <stdio_ext.h>
27 #include "stdio-impl.h"
32 #if HAVE___FPURGE /* glibc >= 2.2, Haiku, Solaris >= 7, musl libc */
35 /* The __fpurge function does not have a return value. */
38 #elif HAVE_FPURGE /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin 1.7 */
40 /* Call the system's fpurge function. */
42 # if !HAVE_DECL_FPURGE
43 extern int fpurge (FILE *);
45 int result = fpurge (fp);
46 # if defined __sferror || defined __DragonFly__ || defined __ANDROID__
47 /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
49 /* Correct the invariants that fpurge broke.
50 <stdio.h> on BSD systems says:
51 "The following always hold: if _flags & __SRD, _w is 0."
52 If this invariant is not fulfilled and the stream is read-write but
53 currently reading, subsequent putc or fputc calls will write directly
54 into the buffer, although they shouldn't be allowed to. */
55 if ((fp_->_flags & __SRD) != 0)
62 /* Most systems provide FILE as a struct and the necessary bitmask in
63 <stdio.h>, because they need it for implementing getc() and putc() as
65 # if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
66 fp->_IO_read_end = fp->_IO_read_ptr;
67 fp->_IO_write_ptr = fp->_IO_write_base;
68 /* Avoid memory leak when there is an active ungetc buffer. */
69 if (fp->_IO_save_base != NULL)
71 free (fp->_IO_save_base);
72 fp->_IO_save_base = NULL;
75 # elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
76 /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
77 fp_->_p = fp_->_bf._base;
79 fp_->_w = ((fp_->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
82 /* Avoid memory leak when there is an active ungetc buffer. */
83 if (fp_ub._base != NULL)
85 if (fp_ub._base != fp_->_ubuf)
90 # elif defined __EMX__ /* emx+gcc */
91 fp->_ptr = fp->_buffer;
94 fp->_ungetc_count = 0;
96 # elif defined __minix /* Minix */
101 # elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
102 fp->_ptr = fp->_base;
103 if (fp->_ptr != NULL)
106 # elif defined __UCLIBC__ /* uClibc */
107 # ifdef __STDIO_BUFFERS
108 if (fp->__modeflags & __FLAG_WRITING)
109 fp->__bufpos = fp->__bufstart;
110 else if (fp->__modeflags & (__FLAG_READONLY | __FLAG_READING))
111 fp->__bufpos = fp->__bufread;
114 # elif defined __QNX__ /* QNX */
115 fp->_Rback = fp->_Back + sizeof (fp->_Back);
117 if (fp->_Mode & 0x2000 /* _MWRITE */)
118 /* fp->_Buf <= fp->_Next <= fp->_Wend */
119 fp->_Next = fp->_Buf;
121 /* fp->_Buf <= fp->_Next <= fp->_Rend */
122 fp->_Rend = fp->_Next;
124 # elif defined __MINT__ /* Atari FreeMiNT */
125 if (fp->__pushed_back)
127 fp->__bufp = fp->__pushback_bufp;
128 fp->__pushed_back = 0;
130 /* Preserve the current file position. */
131 if (fp->__target != -1)
132 fp->__target += fp->__bufp - fp->__buffer;
133 fp->__bufp = fp->__buffer;
134 /* Nothing in the buffer, next getc is nontrivial. */
135 fp->__get_limit = fp->__bufp;
136 /* Nothing in the buffer, next putc is nontrivial. */
137 fp->__put_limit = fp->__buffer;
139 # elif defined EPLAN9 /* Plan9 */
140 fp->rp = fp->wp = fp->lp = fp->buf;
143 # 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."