1 /* fflush.c -- allow flushing input streams
2 Copyright (C) 2007-2009 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 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
37 /* Clear the stream's ungetc buffer, preserving the value of ftello (fp). */
39 clear_ungetc_buffer_preserving_position (FILE *fp)
41 if (fp->_flags & _IO_IN_BACKUP)
42 /* _IO_free_backup_area is a bit complicated. Simply call fseek. */
43 fseek (fp, 0, SEEK_CUR);
48 /* Clear the stream's ungetc buffer. May modify the value of ftello (fp). */
50 clear_ungetc_buffer (FILE *fp)
52 # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
58 # elif defined __EMX__ /* emx+gcc */
59 if (fp->_ungetc_count > 0)
61 fp->_ungetc_count = 0;
62 fp->_rcount = - fp->_rcount;
64 # elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
66 # else /* other implementations */
67 fseek (fp, 0, SEEK_CUR);
73 #if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
76 disable_seek_optimization (FILE *fp)
78 int saved_flags = fp_->_flags & (__SOPT | __SNPT);
79 fp_->_flags = (fp_->_flags & ~__SOPT) | __SNPT;
84 restore_seek_optimization (FILE *fp, int saved_flags)
86 fp_->_flags = (fp_->_flags & ~(__SOPT | __SNPT)) | saved_flags;
92 update_fpos_cache (FILE *fp, off_t pos)
94 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
96 fp_->_flags |= __SOFF;
100 /* Flush all pending data on STREAM according to POSIX rules. Both
101 output and seekable input streams are supported. */
103 rpl_fflush (FILE *stream)
105 /* When stream is NULL, POSIX and C99 only require flushing of "output
106 streams and update streams in which the most recent operation was not
107 input", and all implementations do this.
109 When stream is "an output stream or an update stream in which the most
110 recent operation was not input", POSIX and C99 requires that fflush
111 writes out any buffered data, and all implementations do this.
113 When stream is, however, an input stream or an update stream in
114 which the most recent operation was input, C99 specifies nothing,
115 and POSIX only specifies behavior if the stream is seekable.
116 mingw, in particular, drops the input buffer, leaving the file
117 descriptor positioned at the end of the input buffer. I.e. ftell
118 (stream) is lost. We don't want to call the implementation's
121 We test ! freading (stream) here, rather than fwriting (stream), because
122 what we need to know is whether the stream holds a "read buffer", and on
123 mingw this is indicated by _IOREAD, regardless of _IOWRT. */
124 if (stream == NULL || ! freading (stream))
125 return fflush (stream);
127 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
129 clear_ungetc_buffer_preserving_position (stream);
131 return fflush (stream);
135 /* Notes about the file-position indicator:
136 1) The file position indicator is incremented by fgetc() and decremented
138 <http://www.opengroup.org/susv3/functions/fgetc.html>
139 "... the fgetc() function shall ... advance the associated file
140 position indicator for the stream ..."
141 <http://www.opengroup.org/susv3/functions/ungetc.html>
142 "The file-position indicator is decremented by each successful
144 2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
145 "The value of the file-position indicator for the stream after
146 reading or discarding all pushed-back bytes shall be the same
147 as it was before the bytes were pushed back."
148 Here we are discarding all pushed-back bytes. But more specifically,
149 3) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> says:
150 "[After fflush(),] the file offset of the underlying open file
151 description shall be set to the file position of the stream, and
152 any characters pushed back onto the stream by ungetc() ... shall
155 /* POSIX does not specify fflush behavior for non-seekable input
156 streams. Some implementations purge unread data, some return
157 EBADF, some do nothing. */
158 off_t pos = ftello (stream);
165 /* Clear the ungetc buffer. */
166 clear_ungetc_buffer (stream);
168 /* To get here, we must be flushing a seekable input stream, so the
169 semantics of fpurge are now appropriate to clear the buffer. To
170 avoid losing data, the lseek is also necessary. */
172 int result = fpurge (stream);
177 # if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
180 /* Disable seek optimization for the next fseeko call. This tells the
181 following fseeko call to seek to the desired position directly, rather
182 than to seek to a block-aligned boundary. */
183 int saved_flags = disable_seek_optimization (stream);
184 int result = fseeko (stream, pos, SEEK_SET);
186 restore_seek_optimization (stream, saved_flags);
192 pos = lseek (fileno (stream), pos, SEEK_SET);
195 /* After a successful lseek, update the file descriptor's position cache
197 update_fpos_cache (stream, pos);