exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / closein.c
blob74675f6c2b5fe7122c426e81d355c19ed9ddb06d
1 /* Close standard input, rewinding seekable stdin if necessary.
3 Copyright (C) 2007, 2009-2024 Free Software Foundation, Inc.
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 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include "closein.h"
22 #include <errno.h>
23 #include <stdio.h>
24 #include <unistd.h>
26 #include "gettext.h"
27 #define _(msgid) gettext (msgid)
29 #include "close-stream.h"
30 #include "closeout.h"
31 #include <error.h>
32 #include "exitfail.h"
33 #include "freadahead.h"
34 #include "quotearg.h"
36 static const char *file_name;
38 /* Set the file name to be reported in the event an error is detected
39 on stdin by close_stdin. See also close_stdout_set_file_name, if
40 an error is detected when closing stdout. */
41 void
42 close_stdin_set_file_name (const char *file)
44 file_name = file;
47 /* Close standard input, rewinding any unused input if stdin is
48 seekable. On error, issue a diagnostic and _exit with status
49 'exit_failure'. Then call close_stdout.
51 Most programs can get by with close_stdout. close_stdin is only
52 needed when a program wants to guarantee that partially read input
53 from seekable stdin is not consumed, for any subsequent clients.
54 For example, POSIX requires that these two commands behave alike:
56 (sed -ne 1q; cat) < file
57 tail -n +2 file
59 Since close_stdin is commonly registered via 'atexit', POSIX
60 and the C standard both say that it should not call 'exit',
61 because the behavior is undefined if 'exit' is called more than
62 once. So it calls '_exit' instead of 'exit'. If close_stdin
63 is registered via atexit before other functions are registered,
64 the other functions can act before this _exit is invoked.
66 Applications that use close_stdout should flush any streams other
67 than stdin, stdout, and stderr before exiting, since the call to
68 _exit will bypass other buffer flushing. Applications should be
69 flushing and closing other streams anyway, to check for I/O errors.
70 Also, applications should not use tmpfile, since _exit can bypass
71 the removal of these files.
73 It's important to detect such failures and exit nonzero because many
74 tools (most notably 'make' and other build-management systems) depend
75 on being able to detect failure in other tools via their exit status. */
77 void
78 close_stdin (void)
80 bool fail = false;
82 /* There is no need to flush stdin if we can determine quickly that stdin's
83 input buffer is empty; in this case we know that if stdin is seekable,
84 (fseeko (stdin, 0, SEEK_CUR), ftello (stdin))
85 == lseek (0, 0, SEEK_CUR). */
86 if (freadahead (stdin) > 0)
88 /* Only attempt flush if stdin is seekable, as fflush is entitled to
89 fail on non-seekable streams. */
90 if (fseeko (stdin, 0, SEEK_CUR) == 0 && fflush (stdin) != 0)
91 fail = true;
93 if (close_stream (stdin) != 0)
94 fail = true;
95 if (fail)
97 /* Report failure, but defer exit until after closing stdout,
98 since the failure report should still be flushed. */
99 char const *close_error = _("error closing file");
100 if (file_name)
101 error (0, errno, "%s: %s", quotearg_colon (file_name),
102 close_error);
103 else
104 error (0, errno, "%s", close_error);
107 close_stdout ();
109 if (fail)
110 _exit (exit_failure);