Rename xclone to xmemdup. Remove obsolete xalloc macros.
[gnulib.git] / 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 "__fpending.h"
36
37 #if USE_UNLOCKED_IO
38 # include "unlocked-io.h"
39 #endif
40
41 static const char *file_name;
42
43 /* Set the file name to be reported in the event an error is detected
44    by close_stdout.  */
45 void
46 close_stdout_set_file_name (const char *file)
47 {
48   file_name = file;
49 }
50
51 /* Close standard output, exiting with status 'exit_failure' on failure.
52    If a program writes *anything* to stdout, that program should `fflush'
53    stdout and make sure that it succeeds before exiting.  Otherwise,
54    suppose that you go to the extreme of checking the return status
55    of every function that does an explicit write to stdout.  The last
56    printf can succeed in writing to the internal stream buffer, and yet
57    the fclose(stdout) could still fail (due e.g., to a disk full error)
58    when it tries to write out that buffered data.  Thus, you would be
59    left with an incomplete output file and the offending program would
60    exit successfully.
61
62    FIXME: note the fflush suggested above is implicit in the fclose
63    we actually do below.  Consider doing only the fflush and/or using
64    setvbuf to inhibit buffering.
65
66    Besides, it's wasteful to check the return value from every call
67    that writes to stdout -- just let the internal stream state record
68    the failure.  That's what the ferror test is checking below.
69
70    It's important to detect such failures and exit nonzero because many
71    tools (most notably `make' and other build-management systems) depend
72    on being able to detect failure in other tools via their exit status.  */
73
74 void
75 close_stdout (void)
76 {
77   int e = ferror (stdout) ? 0 : -1;
78
79   /* If the stream's error bit is clear and there is nothing to flush,
80      then return right away.  */
81   if (e && __fpending (stdout) == 0)
82     return;
83
84   if (fclose (stdout) != 0)
85     e = errno;
86
87   if (0 <= e)
88     {
89       char const *write_error = _("write error");
90       if (file_name)
91         error (exit_failure, e, "%s: %s", quotearg_colon (file_name),
92                write_error);
93       else
94         error (exit_failure, e, "%s", write_error);
95     }
96 }