]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/gnu-sort/lib/closeout.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / gnu-sort / lib / closeout.c
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "closeout.h"
25
26 #include <stdio.h>
27 #include <errno.h>
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32 #include "error.h"
33 #include "exitfail.h"
34 #include "quotearg.h"
35 #include "unlocked-io.h"
36 #include "__fpending.h"
37
38 static const char *file_name;
39
40 /* Set the file name to be reported in the event an error is detected
41    by close_stdout.  */
42 void
43 close_stdout_set_file_name (const char *file)
44 {
45   file_name = file;
46 }
47
48 /* Close standard output, exiting with status 'exit_failure' on failure.
49    If a program writes *anything* to stdout, that program should `fflush'
50    stdout and make sure that it succeeds before exiting.  Otherwise,
51    suppose that you go to the extreme of checking the return status
52    of every function that does an explicit write to stdout.  The last
53    printf can succeed in writing to the internal stream buffer, and yet
54    the fclose(stdout) could still fail (due e.g., to a disk full error)
55    when it tries to write out that buffered data.  Thus, you would be
56    left with an incomplete output file and the offending program would
57    exit successfully.
58
59    FIXME: note the fflush suggested above is implicit in the fclose
60    we actually do below.  Consider doing only the fflush and/or using
61    setvbuf to inhibit buffering.
62
63    Besides, it's wasteful to check the return value from every call
64    that writes to stdout -- just let the internal stream state record
65    the failure.  That's what the ferror test is checking below.
66
67    It's important to detect such failures and exit nonzero because many
68    tools (most notably `make' and other build-management systems) depend
69    on being able to detect failure in other tools via their exit status.  */
70
71 void
72 close_stdout (void)
73 {
74   int e = ferror (stdout) ? 0 : -1;
75
76   /* If the stream's error bit is clear and there is nothing to flush,
77      then return right away.  */
78   if (e && __fpending (stdout) == 0)
79     return;
80
81   if (fclose (stdout) != 0)
82     e = errno;
83
84   if (0 <= e)
85     {
86       char const *write_error = _("write error");
87       if (file_name)
88         error (exit_failure, e, "%s: %s", quotearg_colon (file_name),
89                write_error);
90       else
91         error (exit_failure, e, "%s", write_error);
92     }
93 }