1 /* fflush.c -- allow flushing input streams
2 Copyright (C) 2007-2013 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"
31 #include "unused-parameter.h"
36 #if defined _IO_EOF_SEEN || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
38 /* Clear the stream's ungetc buffer, preserving the value of ftello (fp). */
40 clear_ungetc_buffer_preserving_position (FILE *fp)
42 if (fp->_flags & _IO_IN_BACKUP)
43 /* _IO_free_backup_area is a bit complicated. Simply call fseek. */
44 fseeko (fp, 0, SEEK_CUR);
49 /* Clear the stream's ungetc buffer. May modify the value of ftello (fp). */
51 clear_ungetc_buffer (FILE *fp)
53 # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
59 # elif defined __EMX__ /* emx+gcc */
60 if (fp->_ungetc_count > 0)
62 fp->_ungetc_count = 0;
63 fp->_rcount = - fp->_rcount;
65 # elif defined _IOERR /* Minix, AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
67 # else /* other implementations */
68 fseeko (fp, 0, SEEK_CUR);
74 #if ! (defined _IO_EOF_SEEN || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */)
76 # if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
79 disable_seek_optimization (FILE *fp)
81 int saved_flags = fp_->_flags & (__SOPT | __SNPT);
82 fp_->_flags = (fp_->_flags & ~__SOPT) | __SNPT;
87 restore_seek_optimization (FILE *fp, int saved_flags)
89 fp_->_flags = (fp_->_flags & ~(__SOPT | __SNPT)) | saved_flags;
95 update_fpos_cache (FILE *fp _GL_UNUSED_PARAMETER,
96 off_t pos _GL_UNUSED_PARAMETER)
98 # if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
99 # if defined __CYGWIN__
100 /* fp_->_offset is typed as an integer. */
103 /* fp_->_offset is an fpos_t. */
104 /* Use a union, since on NetBSD, the compilation flags determine
105 whether fpos_t is typedef'd to off_t or a struct containing a
106 single off_t member. */
115 fp_->_flags |= __SOFF;
121 /* Flush all pending data on STREAM according to POSIX rules. Both
122 output and seekable input streams are supported. */
124 rpl_fflush (FILE *stream)
126 /* When stream is NULL, POSIX and C99 only require flushing of "output
127 streams and update streams in which the most recent operation was not
128 input", and all implementations do this.
130 When stream is "an output stream or an update stream in which the most
131 recent operation was not input", POSIX and C99 requires that fflush
132 writes out any buffered data, and all implementations do this.
134 When stream is, however, an input stream or an update stream in
135 which the most recent operation was input, C99 specifies nothing,
136 and POSIX only specifies behavior if the stream is seekable.
137 mingw, in particular, drops the input buffer, leaving the file
138 descriptor positioned at the end of the input buffer. I.e. ftell
139 (stream) is lost. We don't want to call the implementation's
142 We test ! freading (stream) here, rather than fwriting (stream), because
143 what we need to know is whether the stream holds a "read buffer", and on
144 mingw this is indicated by _IOREAD, regardless of _IOWRT. */
145 if (stream == NULL || ! freading (stream))
146 return fflush (stream);
148 #if defined _IO_EOF_SEEN || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
150 clear_ungetc_buffer_preserving_position (stream);
152 return fflush (stream);
156 /* Notes about the file-position indicator:
157 1) The file position indicator is incremented by fgetc() and decremented
159 <http://www.opengroup.org/susv3/functions/fgetc.html>
160 "... the fgetc() function shall ... advance the associated file
161 position indicator for the stream ..."
162 <http://www.opengroup.org/susv3/functions/ungetc.html>
163 "The file-position indicator is decremented by each successful
165 2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
166 "The value of the file-position indicator for the stream after
167 reading or discarding all pushed-back bytes shall be the same
168 as it was before the bytes were pushed back."
169 Here we are discarding all pushed-back bytes. But more specifically,
170 3) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> says:
171 "[After fflush(),] the file offset of the underlying open file
172 description shall be set to the file position of the stream, and
173 any characters pushed back onto the stream by ungetc() ... shall
176 /* POSIX does not specify fflush behavior for non-seekable input
177 streams. Some implementations purge unread data, some return
178 EBADF, some do nothing. */
179 off_t pos = ftello (stream);
186 /* Clear the ungetc buffer. */
187 clear_ungetc_buffer (stream);
189 /* To get here, we must be flushing a seekable input stream, so the
190 semantics of fpurge are now appropriate to clear the buffer. To
191 avoid losing data, the lseek is also necessary. */
193 int result = fpurge (stream);
198 # if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
201 /* Disable seek optimization for the next fseeko call. This tells the
202 following fseeko call to seek to the desired position directly, rather
203 than to seek to a block-aligned boundary. */
204 int saved_flags = disable_seek_optimization (stream);
205 int result = fseeko (stream, pos, SEEK_SET);
207 restore_seek_optimization (stream, saved_flags);
213 pos = lseek (fileno (stream), pos, SEEK_SET);
216 /* After a successful lseek, update the file descriptor's position cache
218 update_fpos_cache (stream, pos);