1 /* closeout.c - close standard output and standard error
2 Copyright (C) 1998-2007, 2012, 2015 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "fwriteerror.h"
30 #define _(msgid) gettext (msgid)
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.
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.
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.
53 Likewise for standard error.
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. */
59 /* Close standard output and standard error, exiting with status EXIT_FAILURE
64 /* Close standard output. */
65 if (fwriteerror_no_ebadf (stdout))
66 error (EXIT_FAILURE, errno, "%s", _("write error"));
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. */
72 if (ferror (stderr) || fflush (stderr))
77 if (fclose (stderr) && errno != EBADF)
81 /* Note: When exit (...) calls the atexit-registered
82 close_stdout (), which calls
83 error (status, ...), which calls
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. */