xref: /netbsd-src/external/gpl2/xcvs/dist/lib/closeout.c (revision 5a6c14c844c4c665da5632061aebde7bb2cb5766)
1 /* closeout.c - close standard output
2 
3    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
4    Foundation, Inc.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19 #include <sys/cdefs.h>
20 __RCSID("$NetBSD: closeout.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
21 
22 
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26 
27 #include "closeout.h"
28 
29 #include <stdio.h>
30 #include <stdbool.h>
31 #include <errno.h>
32 
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35 
36 #include "error.h"
37 #include "exitfail.h"
38 #include "quotearg.h"
39 #include "__fpending.h"
40 
41 #if USE_UNLOCKED_IO
42 # include "unlocked-io.h"
43 #endif
44 
45 static const char *file_name;
46 
47 /* Set the file name to be reported in the event an error is detected
48    by close_stdout.  */
49 void
close_stdout_set_file_name(const char * file)50 close_stdout_set_file_name (const char *file)
51 {
52   file_name = file;
53 }
54 
55 /* Close standard output, exiting with status 'exit_failure' on failure.
56    If a program writes *anything* to stdout, that program should close
57    stdout and make sure that it succeeds before exiting.  Otherwise,
58    suppose that you go to the extreme of checking the return status
59    of every function that does an explicit write to stdout.  The last
60    printf can succeed in writing to the internal stream buffer, and yet
61    the fclose(stdout) could still fail (due e.g., to a disk full error)
62    when it tries to write out that buffered data.  Thus, you would be
63    left with an incomplete output file and the offending program would
64    exit successfully.  Even calling fflush is not always sufficient,
65    since some file systems (NFS and CODA) buffer written/flushed data
66    until an actual close call.
67 
68    Besides, it's wasteful to check the return value from every call
69    that writes to stdout -- just let the internal stream state record
70    the failure.  That's what the ferror test is checking below.
71 
72    It's important to detect such failures and exit nonzero because many
73    tools (most notably `make' and other build-management systems) depend
74    on being able to detect failure in other tools via their exit status.  */
75 
76 void
close_stdout(void)77 close_stdout (void)
78 {
79   bool prev_fail = ferror (stdout);
80   bool none_pending = (0 == __fpending (stdout));
81   bool fclose_fail = fclose (stdout);
82 
83   if (prev_fail || fclose_fail)
84     {
85       int e = fclose_fail ? errno : 0;
86       char const *write_error;
87 
88       /* If ferror returned zero, no data remains to be flushed, and we'd
89 	 otherwise fail with EBADF due to a failed fclose, then assume that
90 	 it's ok to ignore the fclose failure.  That can happen when a
91 	 program like cp is invoked like this `cp a b >&-' (i.e., with
92 	 stdout closed) and doesn't generate any output (hence no previous
93 	 error and nothing to be flushed).  */
94       if (e == EBADF && !prev_fail && none_pending)
95 	return;
96 
97       write_error = _("write error");
98       if (file_name)
99 	error (exit_failure, e, "%s: %s", quotearg_colon (file_name),
100 	       write_error);
101       else
102 	error (exit_failure, e, "%s", write_error);
103     }
104 }
105