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