diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2012-11-03 11:54:17 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2012-11-03 11:54:17 -0700 |
commit | 7ccfb720b477df05042729e0e300bae5922f5120 (patch) | |
tree | d5bfded71b48dfefbc4e3334c0ae586de29ab7bc /src/emacs.c | |
parent | 376a8e83bb3438f77dadf2d9910ef7baabcc82c2 (diff) | |
download | emacs-7ccfb720b477df05042729e0e300bae5922f5120.tar.gz |
Fix data-loss with --batch.
* admin/merge-gnulib (GNULIB_MODULES): Add close-stream.
* lib/close-stream.c, lib/close-stream.h, lib/fpending.c
* lib/fpending.h, m4/close-stream.m4, m4/fpending.m4:
New files, from gnulib.
* lib/gnulib.mk, m4/gnulib-comp.m4: Regenerate.
* src/emacs.c: Include <close-stream.h>.
(close_output_streams): New function.
(main): Pass it to atexit, so that Emacs closes stdout and stderr
and handles errors appropriately.
(Fkill_emacs): Don't worry about flushing, as close_output_stream
does that now.
Fixes: debbugs:9574
Diffstat (limited to 'src/emacs.c')
-rw-r--r-- | src/emacs.c | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/emacs.c b/src/emacs.c index 98e3f11f0cb..6588bcd78b1 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -27,6 +27,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ #include <sys/file.h> #include <unistd.h> +#include <close-stream.h> #include <ignore-value.h> #include "lisp.h" @@ -675,6 +676,22 @@ void (*__malloc_initialize_hook) (void) EXTERNALLY_VISIBLE = malloc_initialize_h #endif /* DOUG_LEA_MALLOC */ +/* Close standard output and standard error, reporting any write + errors as best we can. This is intended for use with atexit. */ +static void +close_output_streams (void) +{ + if (close_stream (stdout) != 0) + { + fprintf (stderr, "Write error to standard output: %s\n", + emacs_strerror (errno)); + fflush (stderr); + _exit (EXIT_FAILURE); + } + + if (close_stream (stderr) != 0) + _exit (EXIT_FAILURE); +} /* ARGSUSED */ int @@ -890,6 +907,8 @@ main (int argc, char **argv) if (do_initial_setlocale) setlocale (LC_ALL, ""); + atexit (close_output_streams); + inhibit_window_system = 0; /* Handle the -t switch, which specifies filename to use as terminal. */ @@ -1867,8 +1886,6 @@ all of which are called before Emacs is actually killed. */) exit_code = (XINT (arg) < 0 ? XINT (arg) | INT_MIN : XINT (arg) & INT_MAX); - else if (noninteractive && (fflush (stdout) || ferror (stdout))) - exit_code = EXIT_FAILURE; else exit_code = EXIT_SUCCESS; exit (exit_code); |