hello,

here is a simple patch to add -k (keep) flag to
gzip/compress.  freebsd has it, netbsd has it (although
omitted from the manpages), linux has it.

my use case is automating gzip compression for nginx's
ngx_http_gzip_static_module that can serve
precompressed files from the same directory, but
i also need to keep the input files.

i am aware that
$ gzip -c file > file.gz
does the same, but -k could make certain things easier:

$ find . -name "*.html" -exec gzip -k {} \;

-f
-- 
Index: compress.1
===================================================================
RCS file: /cvs/src/usr.bin/compress/compress.1,v
retrieving revision 1.48
diff -u -p -r1.48 compress.1
--- compress.1  17 Mar 2014 14:23:50 -0000      1.48
+++ compress.1  7 Jan 2017 19:10:58 -0000
@@ -44,7 +44,7 @@
 .Nd compress and expand data (compress mode)
 .Sh SYNOPSIS
 .Nm compress
-.Op Fl 123456789cdfghlNnOqrtv
+.Op Fl 123456789cdfghklNnOqrtv
 .Op Fl b Ar bits
 .Op Fl o Ar filename
 .Op Fl S Ar suffix
@@ -192,6 +192,8 @@ Use the deflate scheme, which reportedly
 mode).
 .It Fl h
 Print a short help message.
+.It Fl k
+Keep (do not delete) input files during compression or decompression.
 .It Fl l
 List information for the specified compressed files.
 The following information is listed:
Index: gzip.1
===================================================================
RCS file: /cvs/src/usr.bin/compress/gzip.1,v
retrieving revision 1.14
diff -u -p -r1.14 gzip.1
--- gzip.1      7 Oct 2014 21:06:30 -0000       1.14
+++ gzip.1      7 Jan 2017 19:10:58 -0000
@@ -183,6 +183,8 @@ behave as
 .Xr cat 1 .
 .It Fl h
 Print a short help message.
+.It Fl k
+Keep (do not delete) input files during compression or decompression.
 .It Fl L
 A no-op which exists for compatibility only.
 On GNU gzip, it displays the program's license.
Index: main.c
===================================================================
RCS file: /cvs/src/usr.bin/compress/main.c,v
retrieving revision 1.94
diff -u -p -r1.94 main.c
--- main.c      3 Sep 2016 13:26:50 -0000       1.94
+++ main.c      7 Jan 2017 19:10:58 -0000
@@ -51,6 +51,7 @@
 #define min(a,b) ((a) < (b)? (a) : (b))
 
 int cat, decomp, pipin, force, verbose, testmode, list, recurse, storename;
+int keep;
 extern char *__progname;
 
 const struct compressor {
@@ -73,7 +74,7 @@ const struct compressor {
                "deflate",
                ".gz",
                "\037\213",
-               "123456789ab:cdfhLlNnOo:qrS:tVv",
+               "123456789ab:cdfhkLlNnOo:qrS:tVv",
                "cfhLlNno:qrtVv",
                "fhqr",
                gz_ropen,
@@ -90,7 +91,7 @@ const struct compressor {
                "compress",
                ".Z",
                "\037\235",
-               "123456789ab:cdfghlNnOo:qrS:tv",
+               "123456789ab:cdfghklNnOo:qrS:tv",
                "cfhlNno:qrtv",
                "fghqr",
                z_ropen,
@@ -108,7 +109,7 @@ const struct compressor null_method = {
        "null",
        ".nul",
        "XX",
-       "123456789ab:cdfghlNnOo:qrS:tv",
+       "123456789ab:cdfghklNnOo:qrS:tv",
        "cfhlNno:qrtv",
        "fghqr",
        null_ropen,
@@ -139,6 +140,7 @@ const struct option longopts[] = {
        { "uncompress", no_argument,            0, 'd' },
        { "force",      no_argument,            0, 'f' },
        { "help",       no_argument,            0, 'h' },
+       { "keep",       no_argument,            0, 'k' },
        { "list",       no_argument,            0, 'l' },
        { "license",    no_argument,            0, 'L' },
        { "no-name",    no_argument,            0, 'n' },
@@ -274,6 +276,9 @@ main(int argc, char *argv[])
                        strlcpy(suffix, method->suffix, sizeof(suffix));
                        bits = 6;
                        break;
+               case 'k':
+                       keep++;
+                       break;
                case 'l':
                        list++;
                        testmode = 1;
@@ -456,7 +461,7 @@ main(int argc, char *argv[])
 
                switch (error) {
                case SUCCESS:
-                       if (!cat && !testmode) {
+                       if (!cat && !testmode && !keep) {
                                if (!pipin && unlink(infile) && verbose >= 0)
                                        warn("input: %s", infile);
                        }
@@ -939,7 +944,7 @@ usage(int status)
 
        switch (pmode) {
        case MODE_COMP:
-               fprintf(stderr, "usage: %s [-123456789cdf%sh%slNnOqrt%sv] "
+               fprintf(stderr, "usage: %s [-123456789cdf%shk%slNnOqrt%sv] "
                    "[-b bits] [-o filename] [-S suffix]\n"
                    "       %*s [file ...]\n", __progname,
                    !gzip ? "g" : "", gzip ? "L" : "", gzip ? "V" : "",

Reply via email to