X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Ffflush.c;h=22d7d02f34d227d59cc99e99b590709cb07c80b6;hb=1e60b920564d9b30c65c53553a4293b1c76863d8;hp=9361c09f3fa15d5855aa9caf366168e14a53679c;hpb=9b87488be4a30247c1eabd71408e1e953b797c22;p=gnulib.git
diff --git a/lib/fflush.c b/lib/fflush.c
old mode 100755
new mode 100644
index 9361c09f3..22d7d02f3
--- a/lib/fflush.c
+++ b/lib/fflush.c
@@ -1,10 +1,10 @@
/* fflush.c -- allow flushing input streams
- Copyright (C) 2007 Free Software Foundation, Inc.
+ Copyright (C) 2007-2008 Free Software Foundation, Inc.
- This program is free software; you can redistribute it and/or modify
+ This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -12,8 +12,7 @@
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software Foundation,
- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+ along with this program. If not, see . */
/* Written by Eric Blake. */
@@ -28,8 +27,49 @@
#include "freading.h"
#include "fpurge.h"
+#include "stdio-impl.h"
+
#undef fflush
+static inline void
+clear_ungetc_buffer (FILE *fp)
+{
+#if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+ if (HASUB (fp))
+ {
+ fp_->_p += fp_->_r;
+ fp_->_r = 0;
+ }
+#endif
+}
+
+#if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+
+static inline int
+disable_seek_optimization (FILE *fp)
+{
+ int saved_flags = fp_->_flags & (__SOPT | __SNPT);
+ fp_->_flags = (fp_->_flags & ~__SOPT) | __SNPT;
+ return saved_flags;
+}
+
+static inline void
+restore_seek_optimization (FILE *fp, int saved_flags)
+{
+ fp_->_flags = (fp_->_flags & ~(__SOPT | __SNPT)) | saved_flags;
+}
+
+#endif
+
+static inline void
+update_fpos_cache (FILE *fp, off_t pos)
+{
+#if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+ fp_->_offset = pos;
+ fp_->_flags |= __SOFF;
+#endif
+}
+
/* Flush all pending data on STREAM according to POSIX rules. Both
output and seekable input streams are supported. */
int
@@ -38,15 +78,54 @@ rpl_fflush (FILE *stream)
int result;
off_t pos;
- /* When stream is NULL, POSIX only requires flushing of output
- streams. C89 guarantees behavior of output streams, and fflush
- should be safe on read-write streams that are not currently
- reading. */
- if (! stream || ! freading (stream))
+ /* When stream is NULL, POSIX and C99 only require flushing of "output
+ streams and update streams in which the most recent operation was not
+ input", and all implementations do this.
+
+ When stream is "an output stream or an update stream in which the most
+ recent operation was not input", POSIX and C99 requires that fflush
+ writes out any buffered data, and all implementations do this.
+
+ When stream is, however, an input stream or an update stream in
+ which the most recent operation was input, C99 specifies nothing,
+ and POSIX only specifies behavior if the stream is seekable.
+ mingw, in particular, drops the input buffer, leaving the file
+ descriptor positioned at the end of the input buffer. I.e. ftell
+ (stream) is lost. We don't want to call the implementation's
+ fflush in this case.
+
+ We test ! freading (stream) here, rather than fwriting (stream), because
+ what we need to know is whether the stream holds a "read buffer", and on
+ mingw this is indicated by _IOREAD, regardless of _IOWRT. */
+ if (stream == NULL || ! freading (stream))
return fflush (stream);
+ /* Clear the ungetc buffer.
+
+ This is needed before fetching the file-position indicator, because
+ 1) The file position indicator is incremented by fgetc() and decremented
+ by ungetc():
+
+ "... the fgetc() function shall ... advance the associated file
+ position indicator for the stream ..."
+
+ "The file-position indicator is decremented by each successful
+ call to ungetc()..."
+ 2) says:
+ "The value of the file-position indicator for the stream after
+ reading or discarding all pushed-back bytes shall be the same
+ as it was before the bytes were pushed back."
+ 3) Here we are discarding all pushed-back bytes.
+
+ Unfortunately it is impossible to implement this on platforms with
+ _IOERR, because an ungetc() on this platform prepends the pushed-back
+ bytes to the buffer without an indication of the limit between the
+ pushed-back bytes and the read-ahead bytes. */
+ clear_ungetc_buffer (stream);
+
/* POSIX does not specify fflush behavior for non-seekable input
- streams. */
+ streams. Some implementations purge unread data, some return
+ EBADF, some do nothing. */
pos = ftello (stream);
if (pos == -1)
{
@@ -60,12 +139,31 @@ rpl_fflush (FILE *stream)
result = fpurge (stream);
if (result != 0)
return result;
+
+#if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+
+ {
+ /* Disable seek optimization for the next fseeko call. This tells the
+ following fseeko call to seek to the desired position directly, rather
+ than to seek to a block-aligned boundary. */
+ int saved_flags = disable_seek_optimization (stream);
+
+ result = fseeko (stream, pos, SEEK_SET);
+
+ restore_seek_optimization (stream, saved_flags);
+ }
+ return result;
+
+#else
+
pos = lseek (fileno (stream), pos, SEEK_SET);
if (pos == -1)
return EOF;
-#if defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
- stream->_offset = pos;
- stream->_flags |= __SOFF;
-#endif
+ /* After a successful lseek, update the file descriptor's position cache
+ in the stream. */
+ update_fpos_cache (stream, pos);
+
return 0;
+
+#endif
}