1*63eb84d1Schristos /* closeout.c - close standard output and standard error
2*63eb84d1Schristos    Copyright (C) 1998-2006 Free Software Foundation, Inc.
3*63eb84d1Schristos 
4*63eb84d1Schristos    This program is free software; you can redistribute it and/or modify
5*63eb84d1Schristos    it under the terms of the GNU General Public License as published by
6*63eb84d1Schristos    the Free Software Foundation; either version 2, or (at your option)
7*63eb84d1Schristos    any later version.
8*63eb84d1Schristos 
9*63eb84d1Schristos    This program is distributed in the hope that it will be useful,
10*63eb84d1Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*63eb84d1Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*63eb84d1Schristos    GNU General Public License for more details.
13*63eb84d1Schristos 
14*63eb84d1Schristos    You should have received a copy of the GNU General Public License
15*63eb84d1Schristos    along with this program; if not, write to the Free Software Foundation,
16*63eb84d1Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*63eb84d1Schristos 
18*63eb84d1Schristos #include <config.h>
19*63eb84d1Schristos 
20*63eb84d1Schristos /* Specification.  */
21*63eb84d1Schristos #include "closeout.h"
22*63eb84d1Schristos 
23*63eb84d1Schristos #include <stdio.h>
24*63eb84d1Schristos #include <errno.h>
25*63eb84d1Schristos 
26*63eb84d1Schristos #include "error.h"
27*63eb84d1Schristos #include "fwriteerror.h"
28*63eb84d1Schristos #include "exit.h"
29*63eb84d1Schristos #include "gettext.h"
30*63eb84d1Schristos 
31*63eb84d1Schristos #define _(msgid) gettext (msgid)
32*63eb84d1Schristos 
33*63eb84d1Schristos /* Close standard output, exiting with status STATUS on failure.
34*63eb84d1Schristos    If a program writes *anything* to stdout, that program should close
35*63eb84d1Schristos    stdout and make sure that it succeeds before exiting.  Otherwise,
36*63eb84d1Schristos    suppose that you go to the extreme of checking the return status
37*63eb84d1Schristos    of every function that does an explicit write to stdout.  The last
38*63eb84d1Schristos    printf can succeed in writing to the internal stream buffer, and yet
39*63eb84d1Schristos    the fclose(stdout) could still fail (due e.g., to a disk full error)
40*63eb84d1Schristos    when it tries to write out that buffered data.  Thus, you would be
41*63eb84d1Schristos    left with an incomplete output file and the offending program would
42*63eb84d1Schristos    exit successfully.  Even calling fflush is not always sufficient,
43*63eb84d1Schristos    since some file systems (NFS and CODA) buffer written/flushed data
44*63eb84d1Schristos    until an actual close call.
45*63eb84d1Schristos 
46*63eb84d1Schristos    Besides, it's wasteful to check the return value from every call
47*63eb84d1Schristos    that writes to stdout -- just let the internal stream state record
48*63eb84d1Schristos    the failure.  That's what the ferror test is checking below.
49*63eb84d1Schristos 
50*63eb84d1Schristos    If the stdout file descriptor was initially closed (such as when executing
51*63eb84d1Schristos    a program through "program 1>&-"), it is a failure if and only if some
52*63eb84d1Schristos    output was made to stdout.
53*63eb84d1Schristos 
54*63eb84d1Schristos    Likewise for standard error.
55*63eb84d1Schristos 
56*63eb84d1Schristos    It's important to detect such failures and exit nonzero because many
57*63eb84d1Schristos    tools (most notably `make' and other build-management systems) depend
58*63eb84d1Schristos    on being able to detect failure in other tools via their exit status.  */
59*63eb84d1Schristos 
60*63eb84d1Schristos /* Close standard output and standard error, exiting with status EXIT_FAILURE
61*63eb84d1Schristos    on failure.  */
62*63eb84d1Schristos void
close_stdout(void)63*63eb84d1Schristos close_stdout (void)
64*63eb84d1Schristos {
65*63eb84d1Schristos   /* Close standard output.  */
66*63eb84d1Schristos   if (fwriteerror_no_ebadf (stdout))
67*63eb84d1Schristos     error (EXIT_FAILURE, errno, "%s", _("write error"));
68*63eb84d1Schristos 
69*63eb84d1Schristos   /* Close standard error.  This is simpler than fwriteerror_no_ebadf, because
70*63eb84d1Schristos      upon failure we don't need an errno - all we can do at this point is to
71*63eb84d1Schristos      set an exit status.  */
72*63eb84d1Schristos   errno = 0;
73*63eb84d1Schristos   if (ferror (stderr) || fflush (stderr))
74*63eb84d1Schristos     {
75*63eb84d1Schristos       fclose (stderr);
76*63eb84d1Schristos       exit (EXIT_FAILURE);
77*63eb84d1Schristos     }
78*63eb84d1Schristos   if (fclose (stderr) && errno != EBADF)
79*63eb84d1Schristos     exit (EXIT_FAILURE);
80*63eb84d1Schristos }
81*63eb84d1Schristos 
82*63eb84d1Schristos /* Note: When exit (...) calls the atexit-registered
83*63eb84d1Schristos               close_stdout (), which calls
84*63eb84d1Schristos               error (status, ...), which calls
85*63eb84d1Schristos               exit (status),
86*63eb84d1Schristos    we have undefined behaviour according to ISO C 99 section 7.20.4.3.(2).
87*63eb84d1Schristos    But in practice there is no problem: The second exit call is executed
88*63eb84d1Schristos    at a moment when the atexit handlers are no longer active.  */
89