On Sat, Mar 12 2022, Solene Rapenne <[email protected]> wrote:
> On Sat, 12 Mar 2022 15:49:40 +0100
> Solene Rapenne <[email protected]>:
>
>> On Sat, 05 Mar 2022 19:15:02 -0700
>> "Todd C. Miller" <[email protected]>:
>> 
>> > On Sun, 06 Mar 2022 02:58:30 +0100, Jeremie Courreges-Anglas wrote:
>> >   
>> > > I'm not sure what you mean here.  Solene's diff added -k to both
>> > > compress(1) and gzip(1) (and their uncompressor counterparts).
>> > > Adding -k to gzip/gunzip only would indeed make the usage() slightly
>> > > more complicated.
>> > >
>> > > So do we want to add -k to compress(1) or not?  (No strong opinion.)
>> > >
>> > > I like the general idea and the diff seems to work as intended.    
>> > 
>> > I don't really care whether or not we add -k to compress(1).  However,
>> > since other versions of compress do not have a -k option it is
>> > probably best to avoiding adding it.
>> > 
>> >  - todd  
>> 
>> here is a new version that doesn't add -k to compress and uncompress
>> 
>> gzip -k and gunzip -k works as expected
>> compress -k and uncompress -k will display unknown option -- k and
>> show the usage that doesn't show k flag
>
> as millert@ told me, I forgot to update the usage, new diff

Works fine.  Here's an updated diff with suggestions:
- "k" was not completely removed from compress's struct compressor opt
  string, and was not needed in null_method
- try to keep the *flag variables ordered
- rework the condition that decides whether we unlink and print
  a warning message.  I found the existing code not much readable with
  a chain of flags check, a syscall and another flag check.
  Also the new line was over 80 chars.
- adding "k" to usage can be made shorter since "L" is next to "k"

If you like those changes, ok jca@


Index: gzip.1
===================================================================
RCS file: /home/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      13 Mar 2022 11:47:00 -0000
@@ -43,13 +43,13 @@
 .Nd compress and expand data (deflate mode)
 .Sh SYNOPSIS
 .Nm gzip
-.Op Fl 123456789cdfhLlNnOqrtVv
+.Op Fl 123456789cdfhkLlNnOqrtVv
 .Op Fl b Ar bits
 .Op Fl o Ar filename
 .Op Fl S Ar suffix
 .Op Ar
 .Nm gunzip
-.Op Fl cfhLlNnqrtVv
+.Op Fl cfhkLlNnqrtVv
 .Op Fl o Ar filename
 .Op Ar
 .Nm gzcat
@@ -183,6 +183,8 @@ behave as
 .Xr cat 1 .
 .It Fl h
 Print a short help message.
+.It Fl k
+Keep input files after 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: /home/cvs/src/usr.bin/compress/main.c,v
retrieving revision 1.98
diff -u -p -r1.98 main.c
--- main.c      18 Jan 2021 00:46:58 -0000      1.98
+++ main.c      13 Mar 2022 12:04:05 -0000
@@ -75,8 +75,8 @@ const struct compressor {
                "deflate",
                ".gz",
                "\037\213",
-               "123456789ab:cdfhLlNnOo:qrS:tVv",
-               "cfhLlNno:qrtVv",
+               "123456789ab:cdfhkLlNnOo:qrS:tVv",
+               "cfhkLlNno:qrtVv",
                "fhqr",
                gz_ropen,
                gz_read,
@@ -141,6 +141,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' },
@@ -166,12 +167,12 @@ main(int argc, char *argv[])
        const char *optstr, *s;
        char *p, *infile;
        char outfile[PATH_MAX], _infile[PATH_MAX], suffix[16];
-       int bits, ch, error, rc, cflag, oflag;
+       int bits, ch, error, rc, cflag, kflag, oflag;
 
        if (pledge("stdio rpath wpath cpath fattr chown", NULL) == -1)
                err(1, "pledge");
 
-       bits = cflag = oflag = 0;
+       bits = cflag = kflag = oflag = 0;
        storename = -1;
        p = __progname;
        if (p[0] == 'g') {
@@ -276,6 +277,9 @@ main(int argc, char *argv[])
                        strlcpy(suffix, method->suffix, sizeof(suffix));
                        bits = 6;
                        break;
+               case 'k':
+                       kflag = 1;
+                       break;
                case 'l':
                        list++;
                        testmode = 1;
@@ -458,8 +462,8 @@ main(int argc, char *argv[])
 
                switch (error) {
                case SUCCESS:
-                       if (!cat && !testmode) {
-                               if (!pipin && unlink(infile) && verbose >= 0)
+                       if (!cat && !pipin && !testmode && !kflag) {
+                               if (unlink(infile) == -1 && verbose >= 0)
                                        warn("input: %s", infile);
                        }
                        break;
@@ -947,13 +951,13 @@ usage(int status)
                fprintf(stderr, "usage: %s [-123456789cdf%sh%slNnOqrt%sv] "
                    "[-b bits] [-o filename] [-S suffix]\n"
                    "       %*s [file ...]\n", __progname,
-                   !gzip ? "g" : "", gzip ? "L" : "", gzip ? "V" : "",
+                   !gzip ? "g" : "", gzip ? "kL" : "", gzip ? "V" : "",
                    (int)strlen(__progname), "");
                break;
        case MODE_DECOMP:
                fprintf(stderr, "usage: %s [-cfh%slNnqrt%sv] [-o filename] "
                    "[file ...]\n", __progname,
-                   gzip ? "L" : "", gzip ? "V" : "");
+                   gzip ? "kL" : "", gzip ? "V" : "");
                break;
        case MODE_CAT:
                fprintf(stderr, "usage: %s [-f%shqr] [file ...]\n",

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply via email to