gzip: add "--keep" option to retain (don't delete) input files
authorRodrigo Campos <rodrigo@sdfg.com.ar>
Mon, 1 Apr 2013 01:53:31 +0000 (22:53 -0300)
committerJim Meyering <meyering@fb.com>
Tue, 28 May 2013 13:25:39 +0000 (06:25 -0700)
gzip now accepts the --keep (-k) option, for consistency with tools
like xz, lzip and bzip2.  With this option, gzip no longer removes
named input files when compressing and decompressing.
* doc/gzip.texi: Document it.
* gzip.1: Likewise.
* gunzip.in: Likewise.
* NEWS: Likewise.
* gzip.c: Add support for "--keep".

NEWS
doc/gzip.texi
gunzip.in
gzip.1
gzip.c

diff --git a/NEWS b/NEWS
index c8ae541f38e3d7ce71834b41593541461631765e..56a3cba1e84f221a829d42f92fef4d76a4bd8912 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,12 @@ GNU gzip NEWS                                    -*- outline -*-
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** New features
+
+  gzip now accepts the --keep (-k) option, for consistency with tools
+  like xz, lzip and bzip2.  With this option, gzip no longer removes
+  named input files when compressing or decompressing.
+
 ** Bug fixes
 
   gzip -d no longer malfunctions with certain invalid data in 'pack' format.
index f7e45e17b1a15a4db684173b90da0af3cce17794..3de3063c17b8f612321977646f16ff491beb9e53 100644 (file)
@@ -195,6 +195,7 @@ Mandatory arguments to long options are mandatory for short options too.
   -d, --decompress  decompress
   -f, --force       force overwrite of output file and compress links
   -h, --help        give this help
+  -k, --keep        keep (don't delete) input files
   -l, --list        list compressed file contents
   -L, --license     display software license
   -n, --no-name     do not save or restore the original name and time stamp
@@ -276,6 +277,10 @@ whether an existing file should be overwritten.
 @itemx -h
 Print an informative help message describing the options then quit.
 
+@item --keep
+@itemx -k
+Keep (don't delete) input files during compression or decompression.
+
 @item --list
 @itemx -l
 For each compressed file, list the following fields:
index b95e97381477163e405c6265c0e5dc3278e22b40..dbfd04b095c39d745c856319810c67481499becd 100644 (file)
--- a/gunzip.in
+++ b/gunzip.in
@@ -38,6 +38,7 @@ Mandatory arguments to long options are mandatory for short options too.
 
   -c, --stdout      write on standard output, keep original files unchanged
   -f, --force       force overwrite of output file and compress links
+  -k, --keep        keep (don't delete) input files
   -l, --list        list compressed file contents
   -n, --no-name     do not save or restore the original name and time stamp
   -N, --name        save or restore the original name and time stamp
diff --git a/gzip.1 b/gzip.1
index c97aeb1bf141ba7f74c23713a1e6efdb41124ea8..71e097cefc04feb33d3dc44c59bf7f8058e1cf73 100644 (file)
--- a/gzip.1
+++ b/gzip.1
@@ -4,7 +4,7 @@ gzip, gunzip, zcat \- compress or expand files
 .SH SYNOPSIS
 .ll +8
 .B gzip
-.RB [ " \-acdfhlLnNrtvV19 " ]
+.RB [ " \-acdfhklLnNrtvV19 " ]
 .RB [ \-S\ suffix ]
 [
 .I "name \&..."
@@ -12,7 +12,7 @@ gzip, gunzip, zcat \- compress or expand files
 .ll -8
 .br
 .B gunzip
-.RB [ " \-acfhlLnNrtvV " ]
+.RB [ " \-acfhklLnNrtvV " ]
 .RB [ \-S\ suffix ]
 [
 .I "name \&..."
@@ -218,6 +218,9 @@ prompts to verify whether an existing file should be overwritten.
 .B \-h --help
 Display a help screen and quit.
 .TP
+.B \-k --keep
+Keep (don't delete) input files during compression or decompression.
+.TP
 .B \-l --list
 For each compressed file, list the following fields:
 
@@ -392,7 +395,7 @@ Internet RFC 1951 (May 1996).
 Exit status is normally 0;
 if an error occurs, exit status is 1. If a warning occurs, exit status is 2.
 .TP
-Usage: gzip [-cdfhlLnNrtvV19] [-S suffix] [file ...]
+Usage: gzip [-cdfhklLnNrtvV19] [-S suffix] [file ...]
 Invalid options were specified on the command line.
 .TP
 \fIfile\fP\^: not in gzip format
diff --git a/gzip.c b/gzip.c
index 66529e0fac586ff228bc752d687d0b54ba43b2b0..93cc7384ff3959664807e7dad470fe01f4c4f99c 100644 (file)
--- a/gzip.c
+++ b/gzip.c
@@ -167,6 +167,7 @@ static int ascii = 0;        /* convert end-of-lines to local OS conventions */
        int to_stdout = 0;    /* output to stdout (-c) */
 static int decompress = 0;   /* decompress (-d) */
 static int force = 0;        /* don't ask questions, compress links (-f) */
+static int keep = 0;         /* keep (don't delete) input files */
 static int no_name = -1;     /* don't save or restore the original file name */
 static int no_time = -1;     /* don't save or restore the original file time */
 static int recursive = 0;    /* recurse through directories (-r) */
@@ -256,6 +257,7 @@ static const struct option longopts[] =
     {"force",      0, 0, 'f'}, /* force overwrite of output file */
     {"help",       0, 0, 'h'}, /* give help */
  /* {"pkzip",      0, 0, 'k'},    force output in pkzip format */
+    {"keep",       0, 0, 'k'}, /* keep (don't delete) input files */
     {"list",       0, 0, 'l'}, /* list .gz file contents */
     {"license",    0, 0, 'L'}, /* display software license */
     {"no-name",    0, 0, 'n'}, /* don't save or restore original name & time */
@@ -334,6 +336,7 @@ local void help()
  "  -f, --force       force overwrite of output file and compress links",
  "  -h, --help        give this help",
 /*  -k, --pkzip       force output in pkzip format */
+ "  -k, --keep        keep (don't delete) input files",
  "  -l, --list        list compressed file contents",
  "  -L, --license     display software license",
 #ifdef UNDOCUMENTED
@@ -437,7 +440,7 @@ int main (int argc, char **argv)
     z_suffix = Z_SUFFIX;
     z_len = strlen(z_suffix);
 
-    while ((optc = getopt_long (argc, argv, "ab:cdfhH?lLmMnNqrS:tvVZ123456789",
+    while ((optc = getopt_long (argc, argv, "ab:cdfhH?klLmMnNqrS:tvVZ123456789",
                                 longopts, (int *)0)) != -1) {
         switch (optc) {
         case 'a':
@@ -460,6 +463,8 @@ int main (int argc, char **argv)
             force++; break;
         case 'h': case 'H':
             help(); do_exit(OK); break;
+        case 'k':
+            keep = 1; break;
         case 'l':
             list = decompress = to_stdout = 1; break;
         case 'L':
@@ -851,25 +856,29 @@ local void treat_file(iname)
 
     if (!to_stdout)
       {
-        sigset_t oldset;
-        int unlink_errno;
 
         copy_stat (&istat);
         if (close (ofd) != 0)
           write_error ();
 
-        sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
-        remove_ofname_fd = -1;
-        unlink_errno = xunlink (ifname) == 0 ? 0 : errno;
-        sigprocmask (SIG_SETMASK, &oldset, NULL);
-
-        if (unlink_errno)
+        if (!keep)
           {
-            WARN ((stderr, "%s: ", program_name));
-            if (!quiet)
+            sigset_t oldset;
+            int unlink_errno;
+
+            sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
+            remove_ofname_fd = -1;
+            unlink_errno = xunlink (ifname) == 0 ? 0 : errno;
+            sigprocmask (SIG_SETMASK, &oldset, NULL);
+
+            if (unlink_errno)
               {
-                errno = unlink_errno;
-                perror (ifname);
+                WARN ((stderr, "%s: ", program_name));
+                if (!quiet)
+                  {
+                    errno = unlink_errno;
+                    perror (ifname);
+                  }
               }
           }
       }