From toshi@jp.freebsd.org Mon Aug  9 07:33:44 1999
Return-Path: <toshi@jp.freebsd.org>
Received: from castle.jp.freebsd.org (castle.jp.freebsd.org [210.226.20.15])
	by hub.freebsd.org (Postfix) with ESMTP id 5596A150AF
	for <FreeBSD-gnats-submit@freebsd.org>; Mon,  9 Aug 1999 07:33:40 -0700 (PDT)
	(envelope-from toshi@jp.freebsd.org)
Received: (from toshi@localhost)
	by castle.jp.freebsd.org (8.9.3+3.2W/8.7.3) id XAA68208;
	Mon, 9 Aug 1999 23:29:30 +0900 (JST)
	(envelope-from toshi)
Message-Id: <199908091429.XAA68208@castle.jp.freebsd.org>
Date: Mon, 9 Aug 1999 23:29:30 +0900 (JST)
From: toshi@jp.freebsd.org
Reply-To: toshi@jp.freebsd.org
To: FreeBSD-gnats-submit@freebsd.org
Subject: minigzip -c option support.
X-Send-Pr-Version: 3.2

>Number:         13043
>Category:       bin
>Synopsis:       minigzip -c option support.
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    johan
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Aug  9 07:40:00 PDT 1999
>Closed-Date:    Mon Oct 28 00:58:18 PST 2002
>Last-Modified:  Mon Oct 28 00:58:18 PST 2002
>Originator:     Toshihiko ARAI
>Release:        FreeBSD 3.2-RELEASE i386
>Organization:
>Environment:

	FreeBSD 3.2-RELEASE PC/AT Install floppies.

>Description:

	/stand/zcat foo.gz uncompressed original file to foo.
	(i.e. same as gunzip -d foo.gz)

>How-To-Repeat:

	ln -s /usr/bin/minigzip zcat
	zcat foo.gz

>Fix:
	
	Implement minigzip -c option.

--- ../../lib/libz/minigzip.c.orig	Sun Jan 10 18:53:50 1999
+++ ../../lib/libz/minigzip.c	Mon Aug  9 00:30:12 1999
@@ -122,7 +122,6 @@
         if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
     }
     fclose(in);
-    if (gzclose(out) != Z_OK) error("failed gzclose");
 }
 
 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
@@ -157,7 +156,6 @@
 
     munmap(buf, buf_len);
     fclose(in);
-    if (gzclose(out) != Z_OK) error("failed gzclose");
     return Z_OK;
 }
 #endif /* USE_MMAP */
@@ -182,7 +180,6 @@
 	    error("failed fwrite");
 	}
     }
-    if (fclose(out)) error("failed fclose");
 
     if (gzclose(in) != Z_OK) error("failed gzclose");
 }
@@ -215,6 +212,7 @@
     }
     gz_compress(in, out);
 
+    if (gzclose(out) != Z_OK) error("failed gzclose");
     unlink(file);
 }
 
@@ -255,12 +253,14 @@
 
     gz_uncompress(in, out);
 
+    if (fclose(out)) error("failed fclose");
     unlink(infile);
 }
 
 
 /* ===========================================================================
- * Usage:  minigzip [-d] [-f] [-h] [-1 to -9] [files...]
+ * Usage:  minigzip [-c] [-d] [-f] [-h] [-1 to -9] [files...]
+ *   -c : standart output
  *   -d : decompress
  *   -f : compress with Z_FILTERED
  *   -h : compress with Z_HUFFMAN_ONLY
@@ -271,6 +271,7 @@
     int argc;
     char *argv[];
 {
+    int copyout = 0;
     int uncompr = 0;
     gzFile file;
     char *bname, outmode[20];
@@ -285,12 +286,14 @@
       bname = argv[0];
     argc--, argv++;
 
-    if (!strcmp(bname, "gunzip") || !strcmp(bname, "zcat"))
+    if (!strcmp(bname, "gunzip"))
       uncompr = 1;
+    else if (!strcmp(bname, "zcat"))
+      copyout = uncompr = 1;
 
     while (argc > 0) {
       if (strcmp(*argv, "-c") == 0)
-	; /* Just for compatability with gzip */
+	copyout = 1; /* Just for compatability with gzip */
       else if (strcmp(*argv, "-d") == 0)
 	uncompr = 1;
       else if (strcmp(*argv, "-f") == 0)
@@ -315,15 +318,39 @@
             file = gzdopen(fileno(stdout), outmode);
             if (file == NULL) error("can't gzdopen stdout");
             gz_compress(stdin, file);
+	    if (gzclose(file) != Z_OK) error("failed gzclose");
         }
     } else {
+	if (copyout) {
+	    SET_BINARY_MODE(stdout);
+            if (!uncompr) {
+		file = gzdopen(fileno(stdout), outmode);
+		if (file == NULL) error("can't gzdopen stdout");
+	    }
+	}
         do {
             if (uncompr) {
-                file_uncompress(*argv);
+		if (copyout) {
+		    file = gzopen(*argv, "rb");
+		    if (file == NULL)
+			fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
+		    else
+			gz_uncompress(file, stdout);
+		} else
+		    file_uncompress(*argv);
             } else {
-                file_compress(*argv, outmode);
+		if (copyout) {
+		    FILE * in = fopen(*argv, "rb");
+		    if (in == NULL)
+			perror(*argv);
+		    else
+			gz_compress(in, file);
+		} else
+		    file_compress(*argv, outmode);
             }
         } while (argv++, --argc);
+	if (copyout && !uncompr)
+	    if (gzclose(file) != Z_OK) error("failed gzclose");
     }
     exit(0);
     return 0; /* to avoid warning */
--- minigzip.1.orig	Mon Jan  5 16:14:54 1998
+++ minigzip.1	Mon Aug  9 00:07:49 1999
@@ -32,6 +32,7 @@
 .Nd minimal implementation of the 'gzip' compression tool
 .Sh SYNOPSIS
 .Nm minigzip
+.Op Fl c
 .Op Fl d
 .Op Ar file ...
 .Sh DESCRIPTION
@@ -63,6 +64,10 @@
 .Nm
 reads from standard input and writes the results of the operation 
 to standard output.
+.Pp
+If the
+.Fl c
+option is specified, write results to standard output.
 .Sh SEE ALSO
 .Xr gzip 1
 .Sh AUTHORS


>Release-Note:
>Audit-Trail:

From: Jonathan Towne <jontow@twcny.rr.com>
To: freebsd-gnats-submit@FreeBSD.org
Cc: toshi@jp.freebsd.org
Subject: Re: bin/13043: minigzip -c option support.
Date: Sun, 9 Jun 2002 10:37:39 -0400

 --1LKvkjL3sHcu1TtY
 Content-Type: text/plain; charset=us-ascii
 
 Having stumbled across this, I thought it was a great idea.. and
 upon noticing that it was a rather old PR, decided to make it a
 little less stale.  The patch that follows applies to -CURRENT
 from anoncvs.freebsd.org as of Sunday, June 9, ~10:30 EDT.
 
 cd /usr/src/usr.bin/minigzip 
 patch < path/to/minigzip.stdout.fbsd5.patch
 
 
 - Jonathan Towne
 
 --1LKvkjL3sHcu1TtY
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: attachment; filename="minigzip.stdout.fbsd5.patch"
 
 *** ../../lib/libz/minigzip.c.orig	Sun Jun  9 10:00:14 2002
 --- ../../lib/libz/minigzip.c	Sun Jun  9 10:18:00 2002
 ***************
 *** 123,129 ****
           if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
       }
       fclose(in);
 -     if (gzclose(out) != Z_OK) error("failed gzclose");
   }
   
   #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
 --- 123,128 ----
 ***************
 *** 158,164 ****
   
       munmap(buf, buf_len);
       fclose(in);
 -     if (gzclose(out) != Z_OK) error("failed gzclose");
       return Z_OK;
   }
   #endif /* USE_MMAP */
 --- 157,162 ----
 ***************
 *** 183,189 ****
   	    error("failed fwrite");
   	}
       }
 -     if (fclose(out)) error("failed fclose");
   
       if (gzclose(in) != Z_OK) error("failed gzclose");
   }
 --- 181,186 ----
 ***************
 *** 221,226 ****
 --- 218,224 ----
       }
       gz_compress(in, out);
   
 +     if (gzclose(out) != Z_OK) error("failed gzclose");
       unlink(file);
   }
   
 ***************
 *** 266,277 ****
   
       gz_uncompress(in, out);
   
       unlink(infile);
   }
   
   
   /* ===========================================================================
 !  * Usage:  minigzip [-d] [-f] [-h] [-1 to -9] [files...]
    *   -d : decompress
    *   -f : compress with Z_FILTERED
    *   -h : compress with Z_HUFFMAN_ONLY
 --- 264,277 ----
   
       gz_uncompress(in, out);
   
 +     if (fclose(out)) error("failed fclose");
       unlink(infile);
   }
   
   
   /* ===========================================================================
 !  * Usage:  minigzip [-c ] [-d] [-f] [-h] [-1 to -9] [files...]
 !  *   -c : decompress to standard output
    *   -d : decompress
    *   -f : compress with Z_FILTERED
    *   -h : compress with Z_HUFFMAN_ONLY
 ***************
 *** 282,287 ****
 --- 282,288 ----
       int argc;
       char *argv[];
   {
 +     int copyout = 0;
       int uncompr = 0;
       gzFile file;
       char *bname, outmode[20];
 ***************
 *** 296,307 ****
         bname = argv[0];
       argc--, argv++;
   
 !     if (!strcmp(bname, "gunzip") || !strcmp(bname, "zcat"))
         uncompr = 1;
   
       while (argc > 0) {
         if (strcmp(*argv, "-c") == 0)
 ! 	; /* Just for compatibility with gzip */
         else if (strcmp(*argv, "-d") == 0)
   	uncompr = 1;
         else if (strcmp(*argv, "-f") == 0)
 --- 297,310 ----
         bname = argv[0];
       argc--, argv++;
   
 !     if (!strcmp(bname, "gunzip"))
         uncompr = 1;
 +     else if (!strcmp(bname, "zcat"))
 +       copyout = uncompr = 1;
   
       while (argc > 0) {
         if (strcmp(*argv, "-c") == 0)
 ! 	copyout = 1; /* Just for compatibility with gzip */
         else if (strcmp(*argv, "-d") == 0)
   	uncompr = 1;
         else if (strcmp(*argv, "-f") == 0)
 ***************
 *** 326,341 ****
               file = gzdopen(fileno(stdout), outmode);
               if (file == NULL) error("can't gzdopen stdout");
               gz_compress(stdin, file);
           }
       } else {
           do {
               if (uncompr) {
 !                 file_uncompress(*argv);
               } else {
 !                 file_compress(*argv, outmode);
               }
           } while (argv++, --argc);
       }
       exit(0);
       return 0; /* to avoid warning */
   }
 --- 329,371 ----
               file = gzdopen(fileno(stdout), outmode);
               if (file == NULL) error("can't gzdopen stdout");
               gz_compress(stdin, file);
 + 	    if (gzclose(file) != Z_OK) error("failed gzclose");
           }
       } else {
 + 	if (copyout) {
 + 	    SET_BINARY_MODE(stdout);
 + 	    if (!uncompr) {
 + 		file = gzdopen(fileno(stdout), outmode);
 + 		if (file == NULL) error("can't gzdopen stdout");
 + 	    }
 + 	}
           do {
               if (uncompr) {
 ! 	    	if (copyout) {
 ! 		    file = gzopen(*argv, "rb");
 ! 		    if (file == NULL)
 ! 			fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
 ! 		    else
 ! 			gz_uncompress(file, stdout);
 ! 		} else
 ! 		    file_uncompress(*argv);
               } else {
 ! 		if (copyout) {
 ! 		    FILE * in = fopen(*argv, "rb");
 ! 		    if (in == NULL)
 ! 			perror(*argv);
 ! 		    else
 ! 			gz_compress(in, file);
 ! 		} else
 ! 		    file_compress(*argv, outmode);
               }
           } while (argv++, --argc);
 + 
 +         if (copyout && !uncompr)
 + 	    if (gzclose(file) != Z_OK) error("failed gzclose");
       }
 + 
 + 
       exit(0);
       return 0; /* to avoid warning */
   }
 *** ../../usr.bin/minigzip/minigzip.1.orig	Sat Apr 20 08:15:48 2002
 --- ../../usr.bin/minigzip/minigzip.1	Sun Jun  9 10:27:05 2002
 ***************
 *** 32,37 ****
 --- 32,38 ----
   .Nd minimal implementation of the 'gzip' compression tool
   .Sh SYNOPSIS
   .Nm
 + .Op Fl c
   .Op Fl d
   .Op Ar
   .Sh DESCRIPTION
 ***************
 *** 64,69 ****
 --- 65,74 ----
   .Nm
   reads from standard input and writes the results of the operation
   to standard output.
 + .Pp
 + If the
 + .Fl c
 + option is specified, write results to standard output.
   .Sh SEE ALSO
   .Xr gzip 1
   .Sh AUTHORS
 
 --1LKvkjL3sHcu1TtY--
Responsible-Changed-From-To: freebsd-bugs->johan 
Responsible-Changed-By: johan 
Responsible-Changed-When: Wed Aug 21 12:39:49 PDT 2002 
Responsible-Changed-Why:  
I will have a look at this. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=13043 
State-Changed-From-To: open->patched 
State-Changed-By: johan 
State-Changed-When: Thu Oct 3 12:33:02 PDT 2002 
State-Changed-Why:  
Similar patch based on this patch has been committed 
to current. 
Thanks for the submission. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=13043 
State-Changed-From-To: patched->closed 
State-Changed-By: johan 
State-Changed-When: Mon Oct 28 00:57:43 PST 2002 
State-Changed-Why:  
MFC:ed to -stable. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=13043 
>Unformatted:
