1 /* Close standard output and standard error, exiting with a diagnostic on error.
3 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2006 Free
4 Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #define _(msgid) gettext (msgid)
30 #include "close-stream.h"
35 static const char *file_name;
37 /* Set the file name to be reported in the event an error is detected
40 close_stdout_set_file_name (const char *file)
45 /* Close standard output. On error, issue a diagnostic and _exit
46 with status 'exit_failure'.
48 Also close standard error. On error, _exit with status 'exit_failure'.
50 Since close_stdout is commonly registered via 'atexit', POSIX
51 and the C standard both say that it should not call 'exit',
52 because the behavior is undefined if 'exit' is called more than
53 once. So it calls '_exit' instead of 'exit'. If close_stdout
54 is registered via atexit before other functions are registered,
55 the other functions can act before this _exit is invoked.
57 Applications that use close_stdout should flush any streams
58 other than stdout and stderr before exiting, since the call to
59 _exit will bypass other buffer flushing. Applications should
60 be flushing and closing other streams anyway, to check for I/O
61 errors. Also, applications should not use tmpfile, since _exit
62 can bypass the removal of these files.
64 It's important to detect such failures and exit nonzero because many
65 tools (most notably `make' and other build-management systems) depend
66 on being able to detect failure in other tools via their exit status. */
71 if (close_stream (stdout) != 0)
73 char const *write_error = _("write error");
75 error (0, errno, "%s: %s", quotearg_colon (file_name),
78 error (0, errno, "%s", write_error);
83 if (close_stream (stderr) != 0)