X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=gzip.c;h=bfab6e74b96f0e0f7a55a558a8c247b9cd14f914;hb=c9eb602f6f5c10a521e7274640033f6a35deca1d;hp=61f721e6ed725d3375e88755e3522d607f1f84fe;hpb=db00b4e39677b0dbf3ea6f7694e0eac7be4ef141;p=gzip diff --git a/gzip.c b/gzip.c index 61f721e..bfab6e7 100644 --- a/gzip.c +++ b/gzip.c @@ -467,16 +467,16 @@ int main (argc, argv) foreground = signal(SIGINT, SIG_IGN) != SIG_IGN; if (foreground) { - (void) signal (SIGINT, (sig_type)abort_gzip); + (void) signal (SIGINT, (sig_type)abort_gzip_signal); } #ifdef SIGTERM if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { - (void) signal(SIGTERM, (sig_type)abort_gzip); + (void) signal(SIGTERM, (sig_type)abort_gzip_signal); } #endif #ifdef SIGHUP if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { - (void) signal(SIGHUP, (sig_type)abort_gzip); + (void) signal(SIGHUP, (sig_type)abort_gzip_signal); } #endif @@ -586,7 +586,7 @@ int main (argc, argv) #ifdef SIGPIPE /* Ignore "Broken Pipe" message with --quiet */ if (quiet && signal (SIGPIPE, SIG_IGN) != SIG_IGN) - signal (SIGPIPE, (sig_type) abort_gzip); + signal (SIGPIPE, (sig_type) abort_gzip_signal); #endif /* By default, save name and timestamp on compression but do not @@ -882,10 +882,21 @@ local void treat_file(iname) close(ifd); if (!to_stdout) { - /* Copy modes, times, ownership, and remove the input file */ + /* Copy modes, times, and ownership */ copy_stat(&istat); if (close(ofd)) write_error(); + remove_ofname = 0; + + /* It's now safe to remove the input file: */ + if (xunlink (ifname)) { + int e = errno; + WARN((stderr, "%s: ", progname)); + if (!quiet) { + errno = e; + perror(ifname); + } + } } if (method == -1) { if (!to_stdout) xunlink (ofname); @@ -1169,8 +1180,9 @@ local int make_ofname() } else if (suff != NULL) { /* Avoid annoying messages with -r (see treat_dir()) */ if (verbose || (!recursive && !quiet)) { - WARN((stderr, "%s: %s already has %s suffix -- unchanged\n", - progname, ifname, suff)); + /* don't use WARN -- it will cause an exit_code of 2 */ + fprintf(stderr, "%s: %s already has %s suffix -- unchanged\n", + progname, ifname, suff); } return WARNING; } else { @@ -1744,16 +1756,6 @@ local void copy_stat(ifstat) #ifndef NO_CHOWN fchown(ofd, ifstat->st_uid, ifstat->st_gid); /* Copy ownership */ #endif - remove_ofname = 0; - /* It's now safe to remove the input file: */ - if (xunlink (ifname)) { - int e = errno; - WARN((stderr, "%s: ", progname)); - if (!quiet) { - errno = e; - perror(ifname); - } - } } #if ! NO_DIR @@ -1850,13 +1852,31 @@ local void do_exit(exitcode) } /* ======================================================================== - * Signal and error handler. + * Close and unlink the output file if appropriate. This routine must be + * async-signal-safe. */ -RETSIGTYPE abort_gzip() -{ +local void do_remove() { if (remove_ofname) { close(ofd); xunlink (ofname); } - do_exit(ERROR); } + +/* ======================================================================== + * Error handler. + */ +RETSIGTYPE abort_gzip() +{ + do_remove(); + do_exit(ERROR); +} + +/* ======================================================================== + * Signal handler. + */ +RETSIGTYPE abort_gzip_signal() +{ + do_remove(); + _exit(ERROR); +} +