Re: gzip and gunzip should have -k (--keep) option like bzip2 and bunzip2

2007-01-09 Thread Shriramana Sharma

Bob Proulx wrote:

Hmm...  Basic command line shell operation is not a trick.  Input and
 output file redirection is a fundamental operation of the command
line shell.  Most users learn that '>' redirects the output of a
command to the file very early in their use of it.


Right. Now I have many gzipped files in a folder. How do I now gunzip
them all without destroying the gz files themselves? gunzip < *.gz
throws up an error saying "ambiguous redirect". It is evident that this
usage is straightforward, since I have to do one thing for a single file
and apparent another thing for many files.

I can't believe this is typical however.  And now that you have 
learned it you will have a new world of capability available to you.


OK thanks, but I still feel that this small addition would go a long way
to helping people using gzip. We should not force a new user into
learning < and > (especially when he/she is not used to the shell very
much) and whatever solution you are going to give me for the "ambiguous
redirect" up there just to do a small thing like zip or unzip files
without destroying the sources. Not all users will have the patience or
time to mail the list and get a reply. The -k option would really help
them.

See for example also, the tar command has the z and j options, whereas
the same work would be accomplished by tar -c | gzip and tar -c | bzip2.
Just because a more advanced alternative exists should not be a reason
not to provide a single simple alternative.

Just because something can be done does not mean that it should be 
done.  Duplicating code in multiple places can always be done but it 
is almost always a bad idea to do so.  It creates a maintenance 
problem to keep the code in sync with each other.


Just adding a few lines to say: "if there is an argument -k then do not
delete the original files" would create a maintenance problem with
syncing? For big libraries and things like that duplication is a
problem, but not with two-line solutions like -k.

To avoid duplication of code and to provide consistency across 
commands this functionality should be some place that is shared among

 all of the commands.


Are you saying that -R for --recursive is implemented by the shell and
not commands like cp, chmod, chown individually? (All three use -R.)

Shriramana Sharma.






Re: gzip and gunzip should have -k (--keep) option like bzip2 and bunzip2

2007-01-09 Thread Shriramana Sharma

All right so I tried implementing my wish myself, but it was complicated
by the fact that the program previously used to_stdout both:

1. to set the output file to stdout and

2. to stop unlinking the input files.

Now I had to separate the two functionalities and also remember to set
keep_input_files when stdout is used, so that the input files are not
unlinked just because -k was not used. I have tried a little bit,
working on the code of gzip 1.3.9, but I am not sure that I have:

1. replaced all relevant occurrences of to_stdout with keep_input_files
(there were one or two places where the value of to_stdout was tested
for something that did not seem to be to be related to unlinking - so I
left them out in the replacement)

2. set keep_input_files to on whenever to_stdout is set to on (well I
did this fully wherever to_stdout = 1 was there explicitly, but I am not
sure that there is no implicit switching-on of to_stdout so to be
careful I mentioned this also)

So please examine the patch - I'm not great with programming related to
file internals, so I can't be sure I haven't messed up anything, but if
I did help, then I'd be happy. If there are any minor changes needed to
my patch to fully implement the desired functionality without affecting
existing unrelated behaviour, please tell me those changes and with
them, please implement this feature.

Thank you.

Shriramana Sharma.

--- gzip.c~ 2006-12-12 05:33:17.0 +0530
+++ gzip.c  2007-01-07 12:44:30.0 +0530
@@ -196,6 +196,7 @@
 int recursive = 0;/* recurse through directories (-r) */
 int list = 0; /* list the file contents (-l) */
 int verbose = 0;  /* be verbose (-v) */
+int keep_input_files = 0; /* do not delete input files */
 int quiet = 0;/* be very quiet (-q) */
 int do_lzw = 0;   /* generate output compatible with old compress (-Z) */
 int test = 0; /* test .gz file integrity */
@@ -247,10 +248,11 @@
 {"stdout", 0, 0, 'c'}, /* write output on standard output */
 {"decompress", 0, 0, 'd'}, /* decompress */
 {"uncompress", 0, 0, 'd'}, /* decompress */
+{"keep",   0, 0, 'k'}, /* keep input files */
  /* {"encrypt",0, 0, 'e'},encrypt */
 {"force",  0, 0, 'f'}, /* force overwrite of output file */
 {"help",   0, 0, 'h'}, /* give help */
- /* {"pkzip",  0, 0, 'k'},force output in pkzip format */
+ /* {"pkzip",  0, 0, 'k'},force output in pkzip format */ /* IF THIS 
IS ENABLED IT WILL CONFLICT WITH "KEEP" */
 {"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 */
@@ -323,9 +325,10 @@
  "  -c, --stdout  write on standard output, keep original files unchanged",
  "  -d, --decompress  decompress",
 /*  -e, --encrypt encrypt */
+ "  -k, --keepkeep (don't delete) input files",
  "  -f, --force   force overwrite of output file and compress links",
  "  -h, --helpgive this help",
-/*  -k, --pkzip   force output in pkzip format */
+/*  -k, --pkzip   force output in pkzip format */ /* IF THIS IS ENABLED IT 
WILL CONFLICT WITH "KEEP" */
  "  -l, --listlist compressed file contents",
  "  -L, --license display software license",
 #ifdef UNDOCUMENTED
@@ -421,7 +424,7 @@
decompress = 1;
 else if (strequ (program_name + 1, "cat") /* zcat, pcat, gcat */
 || strequ (program_name, "gzcat"))   /* gzcat */
-   decompress = to_stdout = 1;
+   decompress = to_stdout = keep_input_files = 1;
 #endif
 
 z_suffix = Z_SUFFIX;
@@ -443,15 +446,17 @@
}
break;
case 'c':
-   to_stdout = 1; break;
+   to_stdout = keep_input_files = 1; break;
case 'd':
decompress = 1; break;
+   case 'k':
+   keep_input_files = 1; break;
case 'f':
force++; break;
case 'h': case 'H':
help(); do_exit(OK); break;
case 'l':
-   list = decompress = to_stdout = 1; break;
+   list = decompress = to_stdout = keep_input_files = 1; break;
case 'L':
license(); do_exit(OK); break;
case 'm': /* undocumented, may change later */
@@ -481,7 +486,7 @@
z_suffix = optarg;
 break;
case 't':
-   test = decompress = to_stdout = 1;
+   test = decompress = to_stdout = keep_input_files = 1;
break;
case 'v':
verbose++; quiet = 0; break;
@@ -634,7 +639,7 @@
 ifile_size = -1L; /* convention for unknown size */
 
 clear_bufs(); /* clear input and output buffers */
-to_stdout = 1;
+to_stdout = keep_input_files = 1;
 part_nb = 0;
 
 if (decompress) {
@@ -823,7 +828,7 @@
 if (close (ifd) != 0)
   read_error ();
 
-if (!to_stdout)
+if (!keep_input_files)
   {
sigset_t oldset;