summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2009-01-16 00:24:35 +0100
committerBruno Haible <bruno@clisp.org>2009-01-16 00:24:35 +0100
commit1074f45959f35f8730a097fdc3d142039071ade7 (patch)
treec41f135cc44df985afb41a453c71976e28a4ba85 /lib
parent73a15056d9347aba42002778308ee398a20584c2 (diff)
downloadgnulib-1074f45959f35f8730a097fdc3d142039071ade7.tar.gz
Make fflush-after-ungetc POSIX compliant on glibc systems.
Diffstat (limited to 'lib')
-rw-r--r--lib/fflush.c19
-rw-r--r--lib/fseeko.c37
2 files changed, 38 insertions, 18 deletions
diff --git a/lib/fflush.c b/lib/fflush.c
index 22d7d02f34..977278991c 100644
--- a/lib/fflush.c
+++ b/lib/fflush.c
@@ -1,5 +1,5 @@
/* fflush.c -- allow flushing input streams
- Copyright (C) 2007-2008 Free Software Foundation, Inc.
+ Copyright (C) 2007-2009 Free Software Foundation, Inc.
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
@@ -34,7 +34,11 @@
static inline void
clear_ungetc_buffer (FILE *fp)
{
-#if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
+ if (fp->_flags & _IO_IN_BACKUP)
+ /* _IO_free_backup_area is a bit complicated. Simply call fseek. */
+ fseek (fp, 0, SEEK_CUR);
+#elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
if (HASUB (fp))
{
fp_->_p += fp_->_r;
@@ -123,6 +127,12 @@ rpl_fflush (FILE *stream)
pushed-back bytes and the read-ahead bytes. */
clear_ungetc_buffer (stream);
+#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
+
+ return fflush (stream);
+
+#else
+
/* POSIX does not specify fflush behavior for non-seekable input
streams. Some implementations purge unread data, some return
EBADF, some do nothing. */
@@ -140,7 +150,7 @@ rpl_fflush (FILE *stream)
if (result != 0)
return result;
-#if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+# 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
@@ -154,7 +164,7 @@ rpl_fflush (FILE *stream)
}
return result;
-#else
+# else
pos = lseek (fileno (stream), pos, SEEK_SET);
if (pos == -1)
@@ -165,5 +175,6 @@ rpl_fflush (FILE *stream)
return 0;
+# endif
#endif
}
diff --git a/lib/fseeko.c b/lib/fseeko.c
index ac0af5785e..47beac42e6 100644
--- a/lib/fseeko.c
+++ b/lib/fseeko.c
@@ -1,5 +1,5 @@
/* An fseeko() function that, together with fflush(), is POSIX compliant.
- Copyright (C) 2007-2008 Free Software Foundation, Inc.
+ Copyright (C) 2007-2009 Free Software Foundation, Inc.
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
@@ -86,7 +86,14 @@ rpl_fseeko (FILE *fp, off_t offset, int whence)
#error "Please port gnulib fseeko.c to your platform! Look at the code in fpurge.c, then report this to bug-gnulib."
#endif
{
- off_t pos = lseek (fileno (fp), offset, whence);
+ /* We get here when an fflush() call immediately preceded this one. We
+ know there are no buffers.
+ POSIX requires us to modify the file descriptor's position.
+ But we cannot position beyond end of file here. */
+ off_t pos =
+ lseek (fileno (fp),
+ whence == SEEK_END && offset > 0 ? 0 : offset,
+ whence);
if (pos == -1)
{
#if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
@@ -94,20 +101,22 @@ rpl_fseeko (FILE *fp, off_t offset, int whence)
#endif
return -1;
}
- else
- {
-#if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
- fp_->_offset = pos;
- fp_->_flags |= __SOFF;
- fp_->_flags &= ~__SEOF;
+
+#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
+ fp->_flags &= ~_IO_EOF_SEEN;
+#elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
+ fp_->_offset = pos;
+ fp_->_flags |= __SOFF;
+ fp_->_flags &= ~__SEOF;
#elif defined __EMX__ /* emx+gcc */
- fp->_flags &= ~_IOEOF;
+ fp->_flags &= ~_IOEOF;
#elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
- fp->_flag &= ~_IOEOF;
+ fp->_flag &= ~_IOEOF;
#endif
- return 0;
- }
+ /* If we were not requested to position beyond end of file, we're
+ done. */
+ if (!(whence == SEEK_END && offset > 0))
+ return 0;
}
- else
- return fseeko (fp, offset, whence);
+ return fseeko (fp, offset, whence);
}