1 /* closeout.c - close standard output and standard error
2    Copyright (C) 1998-2007, 2012 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 3 of the License, or
7    (at your option) 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, see <https://www.gnu.org/licenses/>.  */
16 
17 #include <config.h>
18 
19 /* Specification.  */
20 #include "closeout.h"
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 
26 #include "error.h"
27 #include "fwriteerror.h"
28 #include "gettext.h"
29 
30 #define _(msgid) gettext (msgid)
31 
32 /* Close standard output, exiting with status STATUS on failure.
33    If a program writes *anything* to stdout, that program should close
34    stdout and make sure that it succeeds before exiting.  Otherwise,
35    suppose that you go to the extreme of checking the return status
36    of every function that does an explicit write to stdout.  The last
37    printf can succeed in writing to the internal stream buffer, and yet
38    the fclose(stdout) could still fail (due e.g., to a disk full error)
39    when it tries to write out that buffered data.  Thus, you would be
40    left with an incomplete output file and the offending program would
41    exit successfully.  Even calling fflush is not always sufficient,
42    since some file systems (NFS and CODA) buffer written/flushed data
43    until an actual close call.
44 
45    Besides, it's wasteful to check the return value from every call
46    that writes to stdout -- just let the internal stream state record
47    the failure.  That's what the ferror test is checking below.
48 
49    If the stdout file descriptor was initially closed (such as when executing
50    a program through "program 1>&-"), it is a failure if and only if some
51    output was made to stdout.
52 
53    Likewise for standard error.
54 
55    It's important to detect such failures and exit nonzero because many
56    tools (most notably 'make' and other build-management systems) depend
57    on being able to detect failure in other tools via their exit status.  */
58 
59 /* Close standard output and standard error, exiting with status EXIT_FAILURE
60    on failure.  */
61 void
close_stdout(void)62 close_stdout (void)
63 {
64   /* Close standard output.  */
65   if (fwriteerror_no_ebadf (stdout))
66     error (EXIT_FAILURE, errno, "%s", _("write error"));
67 
68   /* Close standard error.  This is simpler than fwriteerror_no_ebadf, because
69      upon failure we don't need an errno - all we can do at this point is to
70      set an exit status.  */
71   errno = 0;
72   if (ferror (stderr) || fflush (stderr))
73     {
74       fclose (stderr);
75       exit (EXIT_FAILURE);
76     }
77   if (fclose (stderr) && errno != EBADF)
78     exit (EXIT_FAILURE);
79 }
80 
81 /* Note: When exit (...) calls the atexit-registered
82               close_stdout (), which calls
83               error (status, ...), which calls
84               exit (status),
85    we have undefined behaviour according to ISO C 99 section 7.20.4.3.(2).
86    But in practice there is no problem: The second exit call is executed
87    at a moment when the atexit handlers are no longer active.  */
88