Bug 91931 - fflush(3) is misleading about input streams
Summary: fflush(3) is misleading about input streams
Status: RESOLVED CODE_FIX
Alias: None
Product: Documentation
Classification: Unclassified
Component: man-pages (show other bugs)
Hardware: All Linux
: P1 normal
Assignee: documentation_man-pages@kernel-bugs.osdl.org
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-01-23 16:15 UTC by cubbi
Modified: 2015-01-28 08:33 UTC (History)
1 user (show)

See Also:
Kernel Version:
Subsystem:
Regression: No
Bisected commit-id:


Attachments

Description cubbi 2015-01-23 16:15:25 UTC
Current fflush(3) claims that

"For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application."

While technically true (assuming 'file' implies seekable device), this is incomplete: flushing an input stream associated with a non-seekable device, such as the terminal input, does nothing (the update to fp->_IO_read_end at libio/fileops.c:872 does not happen due to ESPIPE from lseek)

There have been enough confused beginner posts to various internet forums asking why flushing stdin doesn't work on Linux. I believe it is worth making the man page more explicit. At the very least, it should begin that sentence with "For input streams associated with seekable devices", or perhaps it should mention the unfortunately popular use case fflush(stdin) explicitly.

On a very minor note, fflush(3) also claims that
"The standards do not specify the behavior for input streams"

This is no longer precisely correct; POSIX specifies behavior for seekable input streams to move fileno's offset to match FILE's offset and undo all ungetc's: http://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html and glibc conforms to that (or tries to, https://sourceware.org/bugzilla/show_bug.cgi?id=5994 is still open)
Comment 1 Michael Kerrisk 2015-01-25 19:53:00 UTC
Thanks for the report. I think you are more or less right. One question though: should the text not be in terms of "block buffered stream" rather than "seekable device"?

Thanks,

Michael
Comment 2 cubbi 2015-01-27 04:35:40 UTC
I don't see how buffering strategy would be relevant: it calls lseek(2) the same way whether the streams are setvbuf'd line-buffered or block-buffered.

POSIX wording is simply "the file is one capable of seeking"
Comment 3 Michael Kerrisk 2015-01-28 08:31:33 UTC
(In reply to cubbi from comment #0)

> On a very minor note, fflush(3) also claims that
> "The standards do not specify the behavior for input streams"
> 
> This is no longer precisely correct; POSIX specifies behavior for seekable
> input streams to move fileno's offset to match FILE's offset and undo all
> ungetc's:
> http://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html and
> glibc conforms to that (or tries to,
> https://sourceware.org/bugzilla/show_bug.cgi?id=5994 is still open)

Thanks. I modified the text to note that POSIX.1-2008 does add specification for input streams.

Note You need to log in before you can comment on or make changes to this bug.