Re: add -k / --keep for gzip(1)

2022-03-13 Thread Todd C . Miller
On Sun, 13 Mar 2022 13:26:13 +0100, Jeremie Courreges-Anglas wrote:

> 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"

Looks good to me.  OK millert@ for this version of the diff.

 - todd



Re: add -k / --keep for gzip(1)

2022-03-13 Thread Jeremie Courreges-Anglas
On Sat, Mar 12 2022, Solene Rapenne  wrote:
> On Sat, 12 Mar 2022 15:49:40 +0100
> Solene Rapenne :
>
>> On Sat, 05 Mar 2022 19:15:02 -0700
>> "Todd C. Miller" :
>> 
>> > 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 -   1.14
+++ gzip.1  13 Mar 2022 11:47:00 -
@@ -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 -  1.98
+++ main.c  13 Mar 2022 12:04:05 -
@@ -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)
   

Re: add -k / --keep for gzip(1)

2022-03-12 Thread Solene Rapenne
On Sat, 05 Mar 2022 19:15:02 -0700
"Todd C. Miller" :

> 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

Index: gzip.1
===
RCS file: /home/reposync/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 -   1.14
+++ gzip.1  3 Mar 2022 12:08:21 -
@@ -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/reposync/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 -  1.98
+++ main.c  12 Mar 2022 14:43:18 -
@@ -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,
@@ -93,7 +93,7 @@ const struct compressor {
".Z",
"\037\235",
"123456789ab:cdfghlNnOo:qrS:tv",
-   "cfhlNno:qrtv",
+   "cfhklNno:qrtv",
"fghqr",
z_ropen,
zread,
@@ -110,8 +110,8 @@ const struct compressor null_method = {
"null",
".nul",
"XX",
-   "123456789ab:cdfghlNnOo:qrS:tv",
-   "cfhlNno:qrtv",
+   "123456789ab:cdfghklNnOo:qrS:tv",
+   "cfhklNno:qrtv",
"fghqr",
null_ropen,
null_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, oflag, kflag;
 
if (pledge("stdio rpath wpath cpath fattr chown", NULL) == -1)
err(1, "pledge");
 
-   bits = cflag = oflag = 0;
+   bits = cflag = oflag = kflag = 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;
@@ -459,7 +463,7 @@ main(int argc, char *argv[])
switch (error) {
case SUCCESS:
if (!cat && !testmode) {
-   if (!pipin && unlink(infile) && verbose >= 0)
+   if (!pipin && !kflag && unlink(infile) && 
verbose >= 0)
warn("input: %s", infile);
}
break;



Re: add -k / --keep for gzip(1)

2022-03-05 Thread Theo de Raadt
Todd C. Miller  wrote:

> 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.

Probably skip adding it co compress.  Or people may encode -k in scripts,
which become non-portable.



Re: add -k / --keep for gzip(1)

2022-03-05 Thread Todd C . Miller
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



Re: add -k / --keep for gzip(1)

2022-03-05 Thread Jeremie Courreges-Anglas
On Thu, Mar 03 2022, Todd C. Miller  wrote:
> On Thu, 03 Mar 2022 15:11:13 +, Miod Vallat wrote:
>
>> > I think this makes sense if only for better GNU gzip compatibility.
>> > OK millert@
>>
>> But does the `-k' flag needs to be added to compress(1) too?
>
> No, it just makes usage() slightly more complicated.
> But that diff was missing an update to usage() anyway.

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.


> Index: compress.1
> ===
> RCS file: /home/reposync/src/usr.bin/compress/compress.1,v
> retrieving revision 1.48
> diff -u -p -r1.48 compress.1
> --- compress.117 Mar 2014 14:23:50 -  1.48
> +++ compress.13 Mar 2022 12:08:01 -
> @@ -44,13 +44,13 @@
>  .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
>  .Op Ar
>  .Nm uncompress
> -.Op Fl cfhlNnqrtv
> +.Op Fl cfhklNnqrtv
>  .Op Fl o Ar filename
>  .Op Ar
>  .Nm zcat
> @@ -192,6 +192,8 @@ Use the deflate scheme, which reportedly
>  mode).
>  .It Fl h
>  Print a short help message.
> +.It Fl k
> +Keep input files after compression or decompression.
>  .It Fl l
>  List information for the specified compressed files.
>  The following information is listed:
> Index: gzip.1
> ===
> RCS file: /home/reposync/src/usr.bin/compress/gzip.1,v
> retrieving revision 1.14
> diff -u -p -r1.14 gzip.1
> --- gzip.17 Oct 2014 21:06:30 -   1.14
> +++ gzip.13 Mar 2022 12:08:21 -
> @@ -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/reposync/src/usr.bin/compress/main.c,v
> retrieving revision 1.98
> diff -u -p -r1.98 main.c
> --- main.c18 Jan 2021 00:46:58 -  1.98
> +++ main.c3 Mar 2022 12:00:28 -
> @@ -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,
> @@ -92,8 +92,8 @@ const struct compressor {
>   "compress",
>   ".Z",
>   "\037\235",
> - "123456789ab:cdfghlNnOo:qrS:tv",
> - "cfhlNno:qrtv",
> + "123456789ab:cdfghklNnOo:qrS:tv",
> + "cfhklNno:qrtv",
>   "fghqr",
>   z_ropen,
>   zread,
> @@ -110,8 +110,8 @@ const struct compressor null_method = {
>   "null",
>   ".nul",
>   "XX",
> - "123456789ab:cdfghlNnOo:qrS:tv",
> - "cfhlNno:qrtv",
> + "123456789ab:cdfghklNnOo:qrS:tv",
> + "cfhklNno:qrtv",
>   "fghqr",
>   null_ropen,
>   null_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, oflag, kflag;
>  
>   if (pledge("stdio rpath wpath cpath fattr chown", NULL) == -1)
>   err(1, "pledge");
>  
> - bits = cflag = oflag = 0;
> + bits = cflag = oflag = kflag = 0;
>   storename = -1;
>   p = __progname;
>   if (p[0] == 'g') {
> @@ -276,6 +277,9 @@ main(int argc, char *argv[])
>   

Re: add -k / --keep for gzip(1)

2022-03-03 Thread Todd C . Miller
On Thu, 03 Mar 2022 15:11:13 +, Miod Vallat wrote:

> > I think this makes sense if only for better GNU gzip compatibility.
> > OK millert@
>
> But does the `-k' flag needs to be added to compress(1) too?

No, it just makes usage() slightly more complicated.
But that diff was missing an update to usage() anyway.

 - todd



Re: add -k / --keep for gzip(1)

2022-03-03 Thread Miod Vallat
> I think this makes sense if only for better GNU gzip compatibility.
> OK millert@

But does the `-k' flag needs to be added to compress(1) too?



Re: add -k / --keep for gzip(1)

2022-03-03 Thread Todd C . Miller
On Thu, 03 Mar 2022 13:13:16 +0100, Solene Rapenne wrote:

> The following diff adds support for -k flag to keep the input file for
> gzip / compress when compressing, and the input file (the compressed
> one) for gunzip / uncompress
>
> This will improve uses cases like: zcat -f "${file}" > "${file}.gz"

I think this makes sense if only for better GNU gzip compatibility.
OK millert@

 - todd



Re: add -k / --keep for gzip(1)

2022-03-03 Thread Jonathan Gray
On Thu, Mar 03, 2022 at 01:13:16PM +0100, Solene Rapenne wrote:
> The following diff adds support for -k flag to keep the input file for
> gzip / compress when compressing, and the input file (the compressed
> one) for gunzip / uncompress

what case is not covered by -c > file ?

> 
> This will improve uses cases like: zcat -f "${file}" > "${file}.gz"

the result won't be gzip compressed
unclear what you have in mind