diff options
author | Bruno Haible <bruno@clisp.org> | 2007-08-20 22:09:08 +0000 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2007-08-20 22:09:08 +0000 |
commit | 7042775894700f8be0e6bc2a5ff3c49fc77296bc (patch) | |
tree | 364d53ec1db89686ee960be99d6dcd1f6994b8ff | |
parent | 5bca97aedcd1e07c1bd80c242239b83c7df9194d (diff) | |
download | gnulib-7042775894700f8be0e6bc2a5ff3c49fc77296bc.tar.gz |
Optimization: no need to flush stdin if we can determine quickly that stdin's
input buffer is empty.
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | lib/closein.c | 15 | ||||
-rw-r--r-- | modules/closein | 1 |
3 files changed, 19 insertions, 4 deletions
@@ -1,5 +1,12 @@ 2007-08-19 Bruno Haible <bruno@clisp.org> + * 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. + +2007-08-19 Bruno Haible <bruno@clisp.org> + * modules/freadahead-tests: New file. * tests/test-freadahead.sh: New file. * tests/test-freadahead.c: New file. diff --git a/lib/closein.c b/lib/closein.c index 4450d5bd8d..bca7711bae 100644 --- a/lib/closein.c +++ b/lib/closein.c @@ -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 @@ close_stdin (void) { 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) diff --git a/modules/closein b/modules/closein index fefeae3648..522a383d3e 100644 --- a/modules/closein +++ b/modules/closein @@ -8,6 +8,7 @@ m4/closein.m4 Depends-on: closeout +freadahead fflush stdbool |