1 /* Flushing buffers of a FILE stream.
2 Copyright (C) 2007 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, Solaris >= 7 */
23 # include <stdio_ext.h>
30 #if HAVE___FPURGE /* glibc >= 2.2, Solaris >= 7 */
33 /* The __fpurge function does not have a return value. */
36 #elif HAVE_FPURGE /* FreeBSD, NetBSD, OpenBSD, MacOS X */
38 /* Call the system's fpurge function. */
40 # if !HAVE_DECL_FPURGE
41 extern int fpurge (FILE *);
43 int result = fpurge (fp);
44 # if defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
46 /* Correct the invariants that fpurge broke.
47 <stdio.h> on BSD systems says:
48 "The following always hold: if _flags & __SRD, _w is 0."
49 If this invariant is not fulfilled and the stream is read-write but
50 currently writing, subsequent putc or fputc calls will write directly
51 into the buffer, although they shouldn't be allowed to. */
52 if ((fp->_flags & __SRD) != 0)
59 /* Most systems provide FILE as a struct and the necessary bitmask in
60 <stdio.h>, because they need it for implementing getc() and putc() as
62 # if defined _IO_ferror_unlocked /* GNU libc, BeOS */
63 fp->_IO_read_end = fp->_IO_read_ptr;
64 fp->_IO_write_ptr = fp->_IO_write_base;
65 /* Avoid memory leak when there is an active ungetc buffer. */
66 if (fp->_IO_save_base != NULL)
68 free (fp->_IO_save_base);
69 fp->_IO_save_base = NULL;
72 # elif defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
73 fp->_p = fp->_bf._base;
75 fp->_w = ((fp->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
78 /* Avoid memory leak when there is an active ungetc buffer. */
79 # if defined __NetBSD__ || defined __OpenBSD__ /* NetBSD, OpenBSD */
80 /* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
81 and <http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> */
82 # define fp_ub ((struct { struct __sbuf _ub; } *) fp->_ext._base)->_ub
83 # else /* FreeBSD, MacOS X, Cygwin */
84 # define fp_ub fp->_ub
86 if (fp_ub._base != NULL)
88 if (fp_ub._base != fp->_ubuf)
93 # elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
98 # elif defined __UCLIBC__ /* uClibc */
99 # ifdef __STDIO_BUFFERS
100 if (fp->__modeflags & __FLAG_WRITING)
101 fp->__bufpos = fp->__bufstart;
102 else if (fp->__modeflags & (__FLAG_READONLY | __FLAG_READING))
103 fp->__bufpos = fp->__bufread;
106 # elif defined __QNX__ /* QNX */
107 fp->_Rback = fp->_Back + sizeof (fp->_Back);
109 if (fp->_Mode & 0x2000 /* _MWRITE */)
110 /* fp->_Buf <= fp->_Next <= fp->_Wend */
111 fp->_Next = fp->_Buf;
113 /* fp->_Buf <= fp->_Next <= fp->_Rend */
114 fp->_Rend = fp->_Next;
117 #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."