diff options
author | Eric Blake <ebb9@byu.net> | 2009-11-04 14:02:20 -0700 |
---|---|---|
committer | Eric Blake <ebb9@byu.net> | 2009-11-05 06:53:21 -0700 |
commit | 41b3a8ed8b2480ca12baaa6c692a7fba3ebd53cf (patch) | |
tree | a75c662443607e08f1d457081957bd4cf25bdbc1 /src/mktemp.c | |
parent | cd65f11c4f5fbfff1f35df56d8afa708c8ac2066 (diff) | |
download | coreutils-41b3a8ed8b2480ca12baaa6c692a7fba3ebd53cf.tar.xz |
mktemp: don't leave file behind on write failure
* src/mktemp.c (main): Remove just-created file if stdout had
problems.
* bootstrap.conf (gnulib_modules): Add remove.
* tests/misc/close-stdout: Test it.
* NEWS: Document it.
Diffstat (limited to 'src/mktemp.c')
-rw-r--r-- | src/mktemp.c | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/mktemp.c b/src/mktemp.c index 808efa914..2b6e4b45f 100644 --- a/src/mktemp.c +++ b/src/mktemp.c @@ -23,6 +23,7 @@ #include "system.h" +#include "close-stream.h" #include "error.h" #include "filenamecat.h" #include "quote.h" @@ -277,7 +278,17 @@ main (int argc, char **argv) } if (status == EXIT_SUCCESS) - puts (dest_name); + { + puts (dest_name); + /* If we created a file, but then failed to output the file + name, we should clean up the mess before failing. */ + if (!dry_run && close_stream (stdout)) + { + int saved_errno = errno; + remove (dest_name); + error (EXIT_FAILURE, saved_errno, _("write error")); + } + } #ifdef lint free (dest_name); |