(close_stdout_set_status, close_stdout_status): Remove.
[gnulib.git] / lib / closeout.c
1 /* closeout.c - close standard output
2    Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include "closeout.h"
23
24 #include <stdio.h>
25
26 #include <errno.h>
27 #ifndef errno
28 extern int errno;
29 #endif
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 #include "error.h"
35 #include "exitfail.h"
36 #include "quotearg.h"
37 #include "unlocked-io.h"
38 #include "__fpending.h"
39
40 static const char *file_name;
41
42 /* Set the file name to be reported in the event an error is detected
43    by close_stdout.  */
44 void
45 close_stdout_set_file_name (const char *file)
46 {
47   file_name = file;
48 }
49
50 /* Close standard output, exiting with status 'exit_failure' on failure.
51    If a program writes *anything* to stdout, that program should `fflush'
52    stdout and make sure that it succeeds before exiting.  Otherwise,
53    suppose that you go to the extreme of checking the return status
54    of every function that does an explicit write to stdout.  The last
55    printf can succeed in writing to the internal stream buffer, and yet
56    the fclose(stdout) could still fail (due e.g., to a disk full error)
57    when it tries to write out that buffered data.  Thus, you would be
58    left with an incomplete output file and the offending program would
59    exit successfully.
60
61    FIXME: note the fflush suggested above is implicit in the fclose
62    we actually do below.  Consider doing only the fflush and/or using
63    setvbuf to inhibit buffering.
64
65    Besides, it's wasteful to check the return value from every call
66    that writes to stdout -- just let the internal stream state record
67    the failure.  That's what the ferror test is checking below.
68
69    It's important to detect such failures and exit nonzero because many
70    tools (most notably `make' and other build-management systems) depend
71    on being able to detect failure in other tools via their exit status.  */
72
73 void
74 close_stdout (void)
75 {
76   int e = ferror (stdout) ? 0 : -1;
77
78   /* If the stream's error bit is clear and there is nothing to flush,
79      then return right away.  */
80   if (e && __fpending (stdout) == 0)
81     return;
82
83   if (fclose (stdout) != 0)
84     e = errno;
85
86   if (0 <= e)
87     {
88       char const *write_error = _("write error");
89       if (file_name)
90         error (exit_failure, e, "%s: %s", quotearg_colon (file_name),
91                write_error);
92       else
93         error (exit_failure, e, "%s", write_error);
94     }
95 }