1 /* fflush.c -- allow flushing input streams
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 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. */
32 /* Flush all pending data on STREAM according to POSIX rules. Both
33 output and seekable input streams are supported. */
35 rpl_fflush (FILE *stream)
40 /* When stream is NULL, POSIX and C99 only require flushing of "output
41 streams and update streams in which the most recent operation was not
42 input", and all implementations do this.
44 When stream is "an output stream or an update stream in which the most
45 recent operation was not input", POSIX and C99 requires that fflush
46 writes out any buffered data, and all implementations do this.
48 When stream is, however, an input stream or an update stream in
49 which the most recent operation was input, C99 specifies nothing,
50 and POSIX only specifies behavior if the stream is seekable.
51 mingw, in particular, drops the input buffer, leaving the file
52 descriptor positioned at the end of the input buffer. I.e. ftell
53 (stream) is lost. We don't want to call the implementation's
56 We test ! freading (stream) here, rather than fwriting (stream), because
57 what we need to know is whether the stream holds a "read buffer", and on
58 mingw this is indicated by _IOREAD, regardless of _IOWRT. */
59 if (stream == NULL || ! freading (stream))
60 return fflush (stream);
62 /* POSIX does not specify fflush behavior for non-seekable input
63 streams. Some implementations purge unread data, some return
64 EBADF, some do nothing. */
65 pos = ftello (stream);
72 /* To get here, we must be flushing a seekable input stream, so the
73 semantics of fpurge are now appropriate to clear the buffer. To
74 avoid losing data, the lseek is also necessary. */
75 result = fpurge (stream);
79 #if defined __sferror && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
82 /* Disable seek optimization for the next fseeko call. This tells the
83 following fseeko call to seek to the desired position directly, rather
84 than to seek to a block-aligned boundary. */
85 int saved_flags = stream->_flags & (__SOPT | __SNPT);
86 stream->_flags = (stream->_flags & ~__SOPT) | __SNPT;
88 result = fseeko (stream, pos, SEEK_SET);
90 stream->_flags = (stream->_flags & ~(__SOPT | __SNPT)) | saved_flags;
96 pos = lseek (fileno (stream), pos, SEEK_SET);
99 /* After a successful lseek, update the file descriptor's position cache
101 # if defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
102 stream->_offset = pos;
103 stream->_flags |= __SOFF;