1*9663SMark.Logan@Sun.COM /* Close standard output and standard error, exiting with a diagnostic on error.
2*9663SMark.Logan@Sun.COM
3*9663SMark.Logan@Sun.COM Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2006 Free
4*9663SMark.Logan@Sun.COM Software Foundation, Inc.
5*9663SMark.Logan@Sun.COM
6*9663SMark.Logan@Sun.COM This program is free software; you can redistribute it and/or modify
7*9663SMark.Logan@Sun.COM it under the terms of the GNU General Public License as published by
8*9663SMark.Logan@Sun.COM the Free Software Foundation; either version 2, or (at your option)
9*9663SMark.Logan@Sun.COM any later version.
10*9663SMark.Logan@Sun.COM
11*9663SMark.Logan@Sun.COM This program is distributed in the hope that it will be useful,
12*9663SMark.Logan@Sun.COM but WITHOUT ANY WARRANTY; without even the implied warranty of
13*9663SMark.Logan@Sun.COM MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*9663SMark.Logan@Sun.COM GNU General Public License for more details.
15*9663SMark.Logan@Sun.COM
16*9663SMark.Logan@Sun.COM You should have received a copy of the GNU General Public License
17*9663SMark.Logan@Sun.COM along with this program; if not, write to the Free Software Foundation,
18*9663SMark.Logan@Sun.COM Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19*9663SMark.Logan@Sun.COM
20*9663SMark.Logan@Sun.COM #include <config.h>
21*9663SMark.Logan@Sun.COM
22*9663SMark.Logan@Sun.COM #include "closeout.h"
23*9663SMark.Logan@Sun.COM
24*9663SMark.Logan@Sun.COM #include <errno.h>
25*9663SMark.Logan@Sun.COM #include <stdio.h>
26*9663SMark.Logan@Sun.COM #include <unistd.h>
27*9663SMark.Logan@Sun.COM
28*9663SMark.Logan@Sun.COM #include "gettext.h"
29*9663SMark.Logan@Sun.COM #define _(msgid) gettext (msgid)
30*9663SMark.Logan@Sun.COM
31*9663SMark.Logan@Sun.COM #include "close-stream.h"
32*9663SMark.Logan@Sun.COM #include "error.h"
33*9663SMark.Logan@Sun.COM #include "exitfail.h"
34*9663SMark.Logan@Sun.COM #include "quotearg.h"
35*9663SMark.Logan@Sun.COM
36*9663SMark.Logan@Sun.COM static const char *file_name;
37*9663SMark.Logan@Sun.COM
38*9663SMark.Logan@Sun.COM /* Set the file name to be reported in the event an error is detected
39*9663SMark.Logan@Sun.COM by close_stdout. */
40*9663SMark.Logan@Sun.COM void
close_stdout_set_file_name(const char * file)41*9663SMark.Logan@Sun.COM close_stdout_set_file_name (const char *file)
42*9663SMark.Logan@Sun.COM {
43*9663SMark.Logan@Sun.COM file_name = file;
44*9663SMark.Logan@Sun.COM }
45*9663SMark.Logan@Sun.COM
46*9663SMark.Logan@Sun.COM /* Close standard output. On error, issue a diagnostic and _exit
47*9663SMark.Logan@Sun.COM with status 'exit_failure'.
48*9663SMark.Logan@Sun.COM
49*9663SMark.Logan@Sun.COM Also close standard error. On error, _exit with status 'exit_failure'.
50*9663SMark.Logan@Sun.COM
51*9663SMark.Logan@Sun.COM Since close_stdout is commonly registered via 'atexit', POSIX
52*9663SMark.Logan@Sun.COM and the C standard both say that it should not call 'exit',
53*9663SMark.Logan@Sun.COM because the behavior is undefined if 'exit' is called more than
54*9663SMark.Logan@Sun.COM once. So it calls '_exit' instead of 'exit'. If close_stdout
55*9663SMark.Logan@Sun.COM is registered via atexit before other functions are registered,
56*9663SMark.Logan@Sun.COM the other functions can act before this _exit is invoked.
57*9663SMark.Logan@Sun.COM
58*9663SMark.Logan@Sun.COM Applications that use close_stdout should flush any streams
59*9663SMark.Logan@Sun.COM other than stdout and stderr before exiting, since the call to
60*9663SMark.Logan@Sun.COM _exit will bypass other buffer flushing. Applications should
61*9663SMark.Logan@Sun.COM be flushing and closing other streams anyway, to check for I/O
62*9663SMark.Logan@Sun.COM errors. Also, applications should not use tmpfile, since _exit
63*9663SMark.Logan@Sun.COM can bypass the removal of these files.
64*9663SMark.Logan@Sun.COM
65*9663SMark.Logan@Sun.COM It's important to detect such failures and exit nonzero because many
66*9663SMark.Logan@Sun.COM tools (most notably `make' and other build-management systems) depend
67*9663SMark.Logan@Sun.COM on being able to detect failure in other tools via their exit status. */
68*9663SMark.Logan@Sun.COM
69*9663SMark.Logan@Sun.COM void
close_stdout(void)70*9663SMark.Logan@Sun.COM close_stdout (void)
71*9663SMark.Logan@Sun.COM {
72*9663SMark.Logan@Sun.COM if (close_stream (stdout) != 0)
73*9663SMark.Logan@Sun.COM {
74*9663SMark.Logan@Sun.COM char const *write_error = _("write error");
75*9663SMark.Logan@Sun.COM if (file_name)
76*9663SMark.Logan@Sun.COM error (0, errno, "%s: %s", quotearg_colon (file_name),
77*9663SMark.Logan@Sun.COM write_error);
78*9663SMark.Logan@Sun.COM else
79*9663SMark.Logan@Sun.COM error (0, errno, "%s", write_error);
80*9663SMark.Logan@Sun.COM
81*9663SMark.Logan@Sun.COM _exit (exit_failure);
82*9663SMark.Logan@Sun.COM }
83*9663SMark.Logan@Sun.COM
84*9663SMark.Logan@Sun.COM if (close_stream (stderr) != 0)
85*9663SMark.Logan@Sun.COM _exit (exit_failure);
86*9663SMark.Logan@Sun.COM }
87