Line data Source code
1 : /* Close standard output and standard error, exiting with a diagnostic on error.
2 :
3 : Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2006 Free
4 : Software Foundation, Inc.
5 :
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.
10 :
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.
15 :
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/>. */
18 :
19 : #include <config.h>
20 :
21 : #include "closeout.h"
22 :
23 : #include <errno.h>
24 : #include <stdio.h>
25 : #include <unistd.h>
26 :
27 : #include "gettext.h"
28 : #define _(msgid) gettext (msgid)
29 :
30 : #include "close-stream.h"
31 : #include "error.h"
32 : #include "exitfail.h"
33 : #include "quotearg.h"
34 :
35 : static const char *file_name;
36 :
37 : /* Set the file name to be reported in the event an error is detected
38 : by close_stdout. */
39 : void
40 0 : close_stdout_set_file_name (const char *file)
41 : {
42 0 : file_name = file;
43 0 : }
44 :
45 : /* Close standard output. On error, issue a diagnostic and _exit
46 : with status 'exit_failure'.
47 :
48 : Also close standard error. On error, _exit with status 'exit_failure'.
49 :
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.
56 :
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.
63 :
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. */
67 :
68 : void
69 6210 : close_stdout (void)
70 : {
71 6210 : if (close_stream (stdout) != 0)
72 : {
73 0 : char const *write_error = _("write error");
74 0 : if (file_name)
75 0 : error (0, errno, "%s: %s", quotearg_colon (file_name),
76 : write_error);
77 : else
78 0 : error (0, errno, "%s", write_error);
79 :
80 0 : _exit (exit_failure);
81 : }
82 :
83 6210 : if (close_stream (stderr) != 0)
84 0 : _exit (exit_failure);
85 6210 : }
|