summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authorPaul Smith <psmith@gnu.org>2005-06-12 22:22:07 +0000
committerPaul Smith <psmith@gnu.org>2005-06-12 22:22:07 +0000
commit467115baae38eba41b3800b4e0e84afb7f3ec99b (patch)
treed9f41f1e367ec952a9ce65dc21c30eebe0a7bcd0 /misc.c
parent974d14ac4fc8dc736a4e2c9876d85d3c295b1196 (diff)
downloadgunmake-467115baae38eba41b3800b4e0e84afb7f3ec99b.tar.gz
Fix Savannah bug # 1328: if stdout is redirected to a full filesystem, we
check for this and exit with an error. The closeout.c version from gnulib pulls in too much other stuff, and gnulib requires an ANSI C 89 compliant compiler, while GNU make (so far) still wants to work on K&R.
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/misc.c b/misc.c
index 63936f7..995fd20 100644
--- a/misc.c
+++ b/misc.c
@@ -823,3 +823,50 @@ get_path_max (void)
return value;
}
#endif
+
+
+/* This code is stolen from gnulib.
+ If/when we abandon the requirement to work with K&R compilers, we can
+ remove this (and perhaps other parts of GNU make!) and migrate to using
+ gnulib directly.
+
+ This is called only through atexit(), which means die() has already been
+ invoked. So, call exit() here directly. Apparently that works...?
+*/
+
+/* Close standard output, exiting with status 'exit_failure' on failure.
+ If a program writes *anything* to stdout, that program should close
+ stdout and make sure that it succeeds before exiting. Otherwise,
+ suppose that you go to the extreme of checking the return status
+ of every function that does an explicit write to stdout. The last
+ printf can succeed in writing to the internal stream buffer, and yet
+ the fclose(stdout) could still fail (due e.g., to a disk full error)
+ when it tries to write out that buffered data. Thus, you would be
+ left with an incomplete output file and the offending program would
+ exit successfully. Even calling fflush is not always sufficient,
+ since some file systems (NFS and CODA) buffer written/flushed data
+ until an actual close call.
+
+ Besides, it's wasteful to check the return value from every call
+ that writes to stdout -- just let the internal stream state record
+ the failure. That's what the ferror test is checking below.
+
+ It's important to detect such failures and exit nonzero because many
+ tools (most notably `make' and other build-management systems) depend
+ on being able to detect failure in other tools via their exit status. */
+
+void
+close_stdout (void)
+{
+ int prev_fail = ferror (stdout);
+ int fclose_fail = fclose (stdout);
+
+ if (prev_fail || fclose_fail)
+ {
+ if (fclose_fail)
+ error (NILF, _("write error: %s"), strerror (errno));
+ else
+ error (NILF, _("write error"));
+ exit (EXIT_FAILURE);
+ }
+}