X-Git-Url: https://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fclosein.c;h=c7fb581eaa5af0f7b3e6c0e65468b909638ef2fb;hb=298d8b4a29e66da0b046b64b822f97d1c8fef74b;hp=51534842624451e877783ea56d2974ac55a1b0cb;hpb=88a56a14d9b092c7e88c05f8a18c8e3fb8844ca1;p=gnulib.git diff --git a/lib/closein.c b/lib/closein.c index 515348426..c7fb581ea 100644 --- a/lib/closein.c +++ b/lib/closein.c @@ -2,10 +2,10 @@ Copyright (C) 2007 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify + 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 - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -13,13 +13,11 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see . */ #include #include "closein.h" -#include "closeout.h" #include #include @@ -30,8 +28,10 @@ #define _(msgid) gettext (msgid) #include "close-stream.h" +#include "closeout.h" #include "error.h" #include "exitfail.h" +#include "freadahead.h" #include "quotearg.h" static const char *file_name; @@ -79,18 +79,29 @@ void close_stdin (void) { bool fail = false; - if (fflush (stdin) != 0 || close_stream (stdin) != 0) + + /* 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) + { + /* Report failure, but defer exit until after closing stdout, + since the failure report should still be flushed. */ char const *close_error = _("error closing file"); if (file_name) error (0, errno, "%s: %s", quotearg_colon (file_name), close_error); else error (0, errno, "%s", close_error); - - /* Defer failure until after closing stdout, since the output - can still usefully be flushed. */ - fail = true; } close_stdout ();