* _fpending.c: Include <config.h> unconditionally, since we no
[gnulib.git] / lib / closeout.c
1 /* Close standard output, 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 2, or (at your option)
9    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, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 #include <config.h>
21
22 #include "closeout.h"
23
24 #include <errno.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 "error.h"
33 #include "exitfail.h"
34 #include "quotearg.h"
35
36 static const char *file_name;
37
38 /* Set the file name to be reported in the event an error is detected
39    by close_stdout.  */
40 void
41 close_stdout_set_file_name (const char *file)
42 {
43   file_name = file;
44 }
45
46 /* Close standard output.  On error, issue a diagnostic and _exit
47    with status 'exit_failure'.
48
49    Since close_stdout is commonly registered via 'atexit', POSIX
50    and the C standard both say that it should not call 'exit',
51    because the behavior is undefined if 'exit' is called more than
52    once.  So it calls '_exit' instead of 'exit'.  If close_stdout
53    is registered via atexit before other functions are registered,
54    the other functions can act before this _exit is invoked.
55
56    Applications that use close_stdout should flush any streams
57    other than stdout and stderr before exiting, since the call to
58    _exit will bypass other buffer flushing.  Applications should
59    be flushing and closing other streams anyway, to check for I/O
60    errors.  Also, applications should not use tmpfile, since _exit
61    can bypass the removal of these files.
62
63    It's important to detect such failures and exit nonzero because many
64    tools (most notably `make' and other build-management systems) depend
65    on being able to detect failure in other tools via their exit status.  */
66
67 void
68 close_stdout (void)
69 {
70   if (close_stream (stdout) != 0)
71     {
72       char const *write_error = _("write error");
73       if (file_name)
74         error (0, errno, "%s: %s", quotearg_colon (file_name),
75                write_error);
76       else
77         error (0, errno, "%s", write_error);
78
79       _exit (exit_failure);
80     }
81 }