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. */
29 #include "stdio-impl.h"
34 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
36 /* Clear the stream's ungetc buffer, preserving the value of ftello (fp). */
38 clear_ungetc_buffer_preserving_position (FILE *fp)
40 if (fp->_flags & _IO_IN_BACKUP)
41 /* _IO_free_backup_area is a bit complicated. Simply call fseek. */
42 fseeko (fp, 0, SEEK_CUR);
47 /* Clear the stream's ungetc buffer. May modify the value of ftello (fp). */
49 clear_ungetc_buffer (FILE *fp)
51 # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
57 # elif defined __EMX__ /* emx+gcc */
58 if (fp->_ungetc_count > 0)
60 fp->_ungetc_count = 0;
61 fp->_rcount = - fp->_rcount;
63 # elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
65 # else /* other implementations */
66 fseeko (fp, 0, SEEK_CUR);
72 #if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
75 disable_seek_optimization (FILE *fp)
77 int saved_flags = fp_->_flags & (__SOPT | __SNPT);
78 fp_->_flags = (fp_->_flags & ~__SOPT) | __SNPT;
83 restore_seek_optimization (FILE *fp, int saved_flags)
85 fp_->_flags = (fp_->_flags & ~(__SOPT | __SNPT)) | saved_flags;
91 update_fpos_cache (FILE *fp, off_t pos)
93 #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
94 /* Use a union, since on NetBSD, the compilation flags determine
95 whether fpos_t is typedef'd to off_t or a struct containing a
96 single off_t member. */
104 fp_->_flags |= __SOFF;
108 /* Flush all pending data on STREAM according to POSIX rules. Both
109 output and seekable input streams are supported. */
111 rpl_fflush (FILE *stream)
113 /* When stream is NULL, POSIX and C99 only require flushing of "output
114 streams and update streams in which the most recent operation was not
115 input", and all implementations do this.
117 When stream is "an output stream or an update stream in which the most
118 recent operation was not input", POSIX and C99 requires that fflush
119 writes out any buffered data, and all implementations do this.
121 When stream is, however, an input stream or an update stream in
122 which the most recent operation was input, C99 specifies nothing,
123 and POSIX only specifies behavior if the stream is seekable.
124 mingw, in particular, drops the input buffer, leaving the file
125 descriptor positioned at the end of the input buffer. I.e. ftell
126 (stream) is lost. We don't want to call the implementation's
129 We test ! freading (stream) here, rather than fwriting (stream), because
130 what we need to know is whether the stream holds a "read buffer", and on
131 mingw this is indicated by _IOREAD, regardless of _IOWRT. */
132 if (stream == NULL || ! freading (stream))
133 return fflush (stream);
135 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
137 clear_ungetc_buffer_preserving_position (stream);
139 return fflush (stream);
143 /* Notes about the file-position indicator:
144 1) The file position indicator is incremented by fgetc() and decremented
146 <http://www.opengroup.org/susv3/functions/fgetc.html>
147 "... the fgetc() function shall ... advance the associated file
148 position indicator for the stream ..."
149 <http://www.opengroup.org/susv3/functions/ungetc.html>
150 "The file-position indicator is decremented by each successful
152 2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
153 "The value of the file-position indicator for the stream after
154 reading or discarding all pushed-back bytes shall be the same
155 as it was before the bytes were pushed back."
156 Here we are discarding all pushed-back bytes. But more specifically,
157 3) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> says:
158 "[After fflush(),] the file offset of the underlying open file
159 description shall be set to the file position of the stream, and
160 any characters pushed back onto the stream by ungetc() ... shall
163 /* POSIX does not specify fflush behavior for non-seekable input
164 streams. Some implementations purge unread data, some return
165 EBADF, some do nothing. */
166 off_t pos = ftello (stream);
173 /* Clear the ungetc buffer. */
174 clear_ungetc_buffer (stream);
176 /* To get here, we must be flushing a seekable input stream, so the
177 semantics of fpurge are now appropriate to clear the buffer. To
178 avoid losing data, the lseek is also necessary. */
180 int result = fpurge (stream);
185 # if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
188 /* Disable seek optimization for the next fseeko call. This tells the
189 following fseeko call to seek to the desired position directly, rather
190 than to seek to a block-aligned boundary. */
191 int saved_flags = disable_seek_optimization (stream);
192 int result = fseeko (stream, pos, SEEK_SET);
194 restore_seek_optimization (stream, saved_flags);
200 pos = lseek (fileno (stream), pos, SEEK_SET);
203 /* After a successful lseek, update the file descriptor's position cache
205 update_fpos_cache (stream, pos);