-
- /* Notes about the file-position indicator:
- 1) The file position indicator is incremented by fgetc() and decremented
- by ungetc():
- <http://www.opengroup.org/susv3/functions/fgetc.html>
- "... the fgetc() function shall ... advance the associated file
- position indicator for the stream ..."
- <http://www.opengroup.org/susv3/functions/ungetc.html>
- "The file-position indicator is decremented by each successful
- call to ungetc()..."
- 2) <http://www.opengroup.org/susv3/functions/ungetc.html> 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) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> 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():
+ <http://www.opengroup.org/susv3/functions/fgetc.html>
+ "... the fgetc() function shall ... advance the associated file
+ position indicator for the stream ..."
+ <http://www.opengroup.org/susv3/functions/ungetc.html>
+ "The file-position indicator is decremented by each successful
+ call to ungetc()..."
+ 2) <http://www.opengroup.org/susv3/functions/ungetc.html> 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) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> 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. */