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 2, or (at your option)
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 along
15
with this program; if not, write to the Free Software Foundation,
16
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23
#if HAVE___FPURGE /* glibc >= 2.2, Solaris >= 7 */
24
# include <stdio_ext.h>
31
#if HAVE___FPURGE /* glibc >= 2.2, Solaris >= 7 */
34
/* The __fpurge function does not have a return value. */
37
#elif HAVE_FPURGE /* FreeBSD, NetBSD, OpenBSD, MacOS X */
39
/* Call the system's fpurge function. */
41
# if !HAVE_DECL_FPURGE
42
extern int fpurge (FILE *);
44
int result = fpurge (fp);
45
# if defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
47
/* Correct the invariants that fpurge broke.
48
<stdio.h> on BSD systems says:
49
"The following always hold: if _flags & __SRD, _w is 0."
50
If this invariant is not fulfilled and the stream is read-write but
51
currently writing, subsequent putc or fputc calls will write directly
52
into the buffer, although they shouldn't be allowed to. */
53
if ((fp->_flags & __SRD) != 0)
60
/* Most systems provide FILE as a struct and the necessary bitmask in
61
<stdio.h>, because they need it for implementing getc() and putc() as
63
# if defined _IO_ferror_unlocked /* GNU libc, BeOS */
64
fp->_IO_read_end = fp->_IO_read_ptr;
65
fp->_IO_write_ptr = fp->_IO_write_base;
66
/* Avoid memory leak when there is an active ungetc buffer. */
67
if (fp->_IO_save_base != NULL)
69
free (fp->_IO_save_base);
70
fp->_IO_save_base = NULL;
73
# elif defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
74
fp->_p = fp->_bf._base;
76
fp->_w = ((fp->_flags & (__SLBF | __SNBF | __SRD)) == 0 /* fully buffered and not currently reading? */
79
/* Avoid memory leak when there is an active ungetc buffer. */
80
# if defined __NetBSD__ || defined __OpenBSD__ /* NetBSD, OpenBSD */
81
/* See <http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup>
82
and <http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdio/fileext.h?rev=HEAD&content-type=text/x-cvsweb-markup> */
83
# define fp_ub ((struct { struct __sbuf _ub; } *) fp->_ext._base)->_ub
84
# else /* FreeBSD, MacOS X, Cygwin */
85
# define fp_ub fp->_ub
87
if (fp_ub._base != NULL)
89
if (fp_ub._base != fp->_ubuf)
94
# elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
99
# elif defined __UCLIBC__ /* uClibc */
100
# ifdef __STDIO_BUFFERS
101
if (fp->__modeflags & __FLAG_WRITING)
102
fp->__bufpos = fp->__bufstart;
103
else if (fp->__modeflags & (__FLAG_READONLY | __FLAG_READING))
104
fp->__bufpos = fp->__bufread;
108
#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."