1 /* fflush.c -- allow flushing input streams
2 Copyright (C) 2007-2008 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/>. */
17 /* Written by Eric Blake. */
30 #include "stdio-impl.h"
35 clear_ungetc_buffer (FILE *fp)
37 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
46 #if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
49 disable_seek_optimization (FILE *fp)
51 int saved_flags = fp_->_flags & (__SOPT | __SNPT);
52 fp_->_flags = (fp_->_flags & ~__SOPT) | __SNPT;
57 restore_seek_optimization (FILE *fp, int saved_flags)
59 fp_->_flags = (fp_->_flags & ~(__SOPT | __SNPT)) | saved_flags;
65 update_fpos_cache (FILE *fp, off_t pos)
67 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
69 fp_->_flags |= __SOFF;
73 /* Flush all pending data on STREAM according to POSIX rules. Both
74 output and seekable input streams are supported. */
76 rpl_fflush (FILE *stream)
81 /* When stream is NULL, POSIX and C99 only require flushing of "output
82 streams and update streams in which the most recent operation was not
83 input", and all implementations do this.
85 When stream is "an output stream or an update stream in which the most
86 recent operation was not input", POSIX and C99 requires that fflush
87 writes out any buffered data, and all implementations do this.
89 When stream is, however, an input stream or an update stream in
90 which the most recent operation was input, C99 specifies nothing,
91 and POSIX only specifies behavior if the stream is seekable.
92 mingw, in particular, drops the input buffer, leaving the file
93 descriptor positioned at the end of the input buffer. I.e. ftell
94 (stream) is lost. We don't want to call the implementation's
97 We test ! freading (stream) here, rather than fwriting (stream), because
98 what we need to know is whether the stream holds a "read buffer", and on
99 mingw this is indicated by _IOREAD, regardless of _IOWRT. */
100 if (stream == NULL || ! freading (stream))
101 return fflush (stream);
103 /* Clear the ungetc buffer.
105 This is needed before fetching the file-position indicator, because
106 1) The file position indicator is incremented by fgetc() and decremented
108 <http://www.opengroup.org/susv3/functions/fgetc.html>
109 "... the fgetc() function shall ... advance the associated file
110 position indicator for the stream ..."
111 <http://www.opengroup.org/susv3/functions/ungetc.html>
112 "The file-position indicator is decremented by each successful
114 2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
115 "The value of the file-position indicator for the stream after
116 reading or discarding all pushed-back bytes shall be the same
117 as it was before the bytes were pushed back."
118 3) Here we are discarding all pushed-back bytes.
120 Unfortunately it is impossible to implement this on platforms with
121 _IOERR, because an ungetc() on this platform prepends the pushed-back
122 bytes to the buffer without an indication of the limit between the
123 pushed-back bytes and the read-ahead bytes. */
124 clear_ungetc_buffer (stream);
126 /* POSIX does not specify fflush behavior for non-seekable input
127 streams. Some implementations purge unread data, some return
128 EBADF, some do nothing. */
129 pos = ftello (stream);
136 /* To get here, we must be flushing a seekable input stream, so the
137 semantics of fpurge are now appropriate to clear the buffer. To
138 avoid losing data, the lseek is also necessary. */
139 result = fpurge (stream);
143 #if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
146 /* Disable seek optimization for the next fseeko call. This tells the
147 following fseeko call to seek to the desired position directly, rather
148 than to seek to a block-aligned boundary. */
149 int saved_flags = disable_seek_optimization (stream);
151 result = fseeko (stream, pos, SEEK_SET);
153 restore_seek_optimization (stream, saved_flags);
159 pos = lseek (fileno (stream), pos, SEEK_SET);
162 /* After a successful lseek, update the file descriptor's position cache
164 update_fpos_cache (stream, pos);