X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Ffflush.c;h=ead4875498e760bc572310d49d02131a1d902573;hb=ab43907148e80f3895280928c05d4be93875580f;hp=1292a1b37919862217121730370e5272bf107d37;hpb=557a006f5a5292d9065640d801293b0b6837cd01;p=gnulib.git diff --git a/lib/fflush.c b/lib/fflush.c index 1292a1b37..ead487549 100644 --- a/lib/fflush.c +++ b/lib/fflush.c @@ -1,5 +1,5 @@ /* fflush.c -- allow flushing input streams - Copyright (C) 2007-2009 Free Software Foundation, Inc. + Copyright (C) 2007-2010 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 @@ -25,7 +25,6 @@ #include #include "freading.h" -#include "fpurge.h" #include "stdio-impl.h" @@ -40,7 +39,7 @@ clear_ungetc_buffer_preserving_position (FILE *fp) { if (fp->_flags & _IO_IN_BACKUP) /* _IO_free_backup_area is a bit complicated. Simply call fseek. */ - fseek (fp, 0, SEEK_CUR); + fseeko (fp, 0, SEEK_CUR); } #else @@ -64,7 +63,7 @@ clear_ungetc_buffer (FILE *fp) # elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */ /* Nothing to do. */ # else /* other implementations */ - fseek (fp, 0, SEEK_CUR); + fseeko (fp, 0, SEEK_CUR); # endif } @@ -92,7 +91,22 @@ static inline void update_fpos_cache (FILE *fp, off_t pos) { #if defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */ +# if defined __CYGWIN__ + /* fp_->_offset is typed as an integer. */ fp_->_offset = pos; +# else + /* fp_->_offset is an fpos_t. */ + /* Use a union, since on NetBSD, the compilation flags determine + whether fpos_t is typedef'd to off_t or a struct containing a + single off_t member. */ + union + { + fpos_t f; + off_t o; + } u; + u.o = pos; + fp_->_offset = u.f; +# endif fp_->_flags |= __SOFF; #endif } @@ -102,9 +116,6 @@ update_fpos_cache (FILE *fp, off_t pos) int rpl_fflush (FILE *stream) { - int result; - off_t pos; - /* 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. @@ -134,72 +145,74 @@ rpl_fflush (FILE *stream) return fflush (stream); #else - - /* Notes about the file-position indicator: - 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." - Here we are discarding all pushed-back bytes. But more specifically, - 3) says: - "[After fflush(),] the file offset of the underlying open file - description shall be set to the file position of the stream, and - any characters pushed back onto the stream by ungetc() ... shall - be discarded." */ - - /* POSIX does not specify fflush behavior for non-seekable input - streams. Some implementations purge unread data, some return - EBADF, some do nothing. */ - pos = ftello (stream); - if (pos == -1) + { + /* Notes about the file-position indicator: + 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." + Here we are discarding all pushed-back bytes. But more specifically, + 3) says: + "[After fflush(),] the file offset of the underlying open file + description shall be set to the file position of the stream, and + any characters pushed back onto the stream by ungetc() ... shall + be discarded." */ + + /* POSIX does not specify fflush behavior for non-seekable input + streams. Some implementations purge unread data, some return + EBADF, some do nothing. */ + off_t pos = ftello (stream); + if (pos == -1) + { + errno = EBADF; + return EOF; + } + + /* Clear the ungetc buffer. */ + clear_ungetc_buffer (stream); + + /* To get here, we must be flushing a seekable input stream, so the + semantics of fpurge are now appropriate to clear the buffer. To + avoid losing data, the lseek is also necessary. */ { - errno = EBADF; - return EOF; + int result = fpurge (stream); + if (result != 0) + return result; } - /* Clear the ungetc buffer. */ - clear_ungetc_buffer (stream); - - /* To get here, we must be flushing a seekable input stream, so the - semantics of fpurge are now appropriate to clear the buffer. To - avoid losing data, the lseek is also necessary. */ - 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; + { + /* 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); + int 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; - /* After a successful lseek, update the file descriptor's position cache - in the stream. */ - update_fpos_cache (stream, pos); + pos = lseek (fileno (stream), pos, SEEK_SET); + if (pos == -1) + return EOF; + /* After a successful lseek, update the file descriptor's position cache + in the stream. */ + update_fpos_cache (stream, pos); - return 0; + return 0; # endif + } #endif }