Optimization: no need to flush stdin if we can determine quickly that stdin's
[gnulib.git] / lib / closein.c
1 /* Close standard input, rewinding seekable stdin if necessary.
2
3    Copyright (C) 2007 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #include <config.h>
20
21 #include "closein.h"
22
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <unistd.h>
27
28 #include "gettext.h"
29 #define _(msgid) gettext (msgid)
30
31 #include "close-stream.h"
32 #include "closeout.h"
33 #include "error.h"
34 #include "exitfail.h"
35 #include "freadahead.h"
36 #include "quotearg.h"
37
38 static const char *file_name;
39
40 /* Set the file name to be reported in the event an error is detected
41    on stdin by close_stdin.  See also close_stdout_set_file_name, if
42    an error is detected when closing stdout.  */
43 void
44 close_stdin_set_file_name (const char *file)
45 {
46   file_name = file;
47 }
48
49 /* Close standard input, rewinding any unused input if stdin is
50    seekable.  On error, issue a diagnostic and _exit with status
51    'exit_failure'.  Then call close_stdout.
52
53    Most programs can get by with close_stdout.  close_stdin is only
54    needed when a program wants to guarantee that partially read input
55    from seekable stdin is not consumed, for any subsequent clients.
56    For example, POSIX requires that these two commands behave alike:
57
58      (sed -ne 1q; cat) < file
59      tail -n 1 file
60
61    Since close_stdin is commonly registered via 'atexit', POSIX
62    and the C standard both say that it should not call 'exit',
63    because the behavior is undefined if 'exit' is called more than
64    once.  So it calls '_exit' instead of 'exit'.  If close_stdin
65    is registered via atexit before other functions are registered,
66    the other functions can act before this _exit is invoked.
67
68    Applications that use close_stdout should flush any streams other
69    than stdin, stdout, and stderr before exiting, since the call to
70    _exit will bypass other buffer flushing.  Applications should be
71    flushing and closing other streams anyway, to check for I/O errors.
72    Also, applications should not use tmpfile, since _exit can bypass
73    the removal of these files.
74
75    It's important to detect such failures and exit nonzero because many
76    tools (most notably `make' and other build-management systems) depend
77    on being able to detect failure in other tools via their exit status.  */
78
79 void
80 close_stdin (void)
81 {
82   bool fail = false;
83
84   /* There is no need to flush stdin if we can determine quickly that stdin's
85      input buffer is empty; in this case we know that if stdin is seekable,
86      fseeko (stdin, 0, SEEK_CUR) == lseek (0, 0, SEEK_CUR).  */
87   if (freadahead (stdin) > 0)
88     {
89       /* Only attempt flush if stdin is seekable, as fflush is entitled to
90          fail on non-seekable streams.  */
91       if (fseeko (stdin, 0, SEEK_CUR) == 0 && fflush (stdin) != 0)
92         fail = true;
93     }
94   if (close_stream (stdin) != 0)
95     fail = true;
96   if (fail)
97     {
98       /* Report failure, but defer exit until after closing stdout,
99          since the failure report should still be flushed.  */
100       char const *close_error = _("error closing file");
101       if (file_name)
102         error (0, errno, "%s: %s", quotearg_colon (file_name),
103                close_error);
104       else
105         error (0, errno, "%s", close_error);
106     }
107
108   close_stdout ();
109
110   if (fail)
111     _exit (exit_failure);
112 }