Eric Blake wrote:
> the current implementation is correct even when stdin is still in the
> initial state. However, if you want to optimize it further, such patches
> are worth considering.
Here is such a proposed patch.
2007-08-19 Bruno Haible <[EMAIL PROTECTED]>
* modules/closein (Depends-on): Add freadahead.
* lib/closein.c: Include freadahead.h.
(close_stdin): Skip the fseeko and fflush calls if freadahead (stdin)
is zero.
--- modules/closein 12 Apr 2007 16:11:40 -0000 1.1
+++ modules/closein 19 Aug 2007 17:48:44 -0000
@@ -8,6 +8,7 @@
Depends-on:
closeout
+freadahead
fflush
stdbool
--- lib/closein.c 27 Apr 2007 17:14:40 -0000 1.3
+++ lib/closein.c 19 Aug 2007 17:48:44 -0000
@@ -32,6 +32,7 @@
#include "closeout.h"
#include "error.h"
#include "exitfail.h"
+#include "freadahead.h"
#include "quotearg.h"
static const char *file_name;
@@ -80,10 +81,16 @@
{
bool fail = false;
- /* Only attempt flush if stdin is seekable, as fflush is entitled to
- fail on non-seekable streams. */
- if (fseeko (stdin, 0, SEEK_CUR) == 0 && fflush (stdin) != 0)
- fail = true;
+ /* There is no need to flush stdin if we can determine quickly that stdin's
+ input buffer is empty; in this case we know that if stdin is seekable,
+ fseeko (stdin, 0, SEEK_CUR) == lseek (0, 0, SEEK_CUR). */
+ if (freadahead (stdin) > 0)
+ {
+ /* Only attempt flush if stdin is seekable, as fflush is entitled to
+ fail on non-seekable streams. */
+ if (fseeko (stdin, 0, SEEK_CUR) == 0 && fflush (stdin) != 0)
+ fail = true;
+ }
if (close_stream (stdin) != 0)
fail = true;
if (fail)