Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/gzip port simonb's bzip2 support.



details:   https://anonhg.NetBSD.org/src/rev/afe3d1d6abdc
branches:  trunk
changeset: 556998:afe3d1d6abdc
user:      mrg <mrg%NetBSD.org@localhost>
date:      Thu Jan 01 02:44:09 2004 +0000

description:
port simonb's bzip2 support.
misc cleanups.

diffstat:

 usr.bin/gzip/Makefile  |    6 +-
 usr.bin/gzip/gzip.1    |   13 ++-
 usr.bin/gzip/gzip.c    |  179 +++++++++++++++++++++++++++++++-----------------
 usr.bin/gzip/unbzip2.c |   75 ++++++++++++++++++++
 4 files changed, 202 insertions(+), 71 deletions(-)

diffs (truncated from 480 to 300 lines):

diff -r 33c65da14328 -r afe3d1d6abdc usr.bin/gzip/Makefile
--- a/usr.bin/gzip/Makefile     Thu Jan 01 01:19:36 2004 +0000
+++ b/usr.bin/gzip/Makefile     Thu Jan 01 02:44:09 2004 +0000
@@ -1,10 +1,10 @@
-#      $NetBSD: Makefile,v 1.7 2003/12/28 17:26:00 wiz Exp $
+#      $NetBSD: Makefile,v 1.8 2004/01/01 02:44:09 mrg Exp $
 
 PROG=          gzip
 MAN=           gzip.1 gzexe.1 zdiff.1 zforce.1 zgrep.1 zmore.1 znew.1
 
-DPADD=         ${LIBZ}
-LDADD=         -lz
+DPADD=         ${LIBZ} ${LIBBZ2}
+LDADD=         -lz -lbz2
 
 SCRIPTS=       gzexe zdiff zforce zgrep zmore znew
 
diff -r 33c65da14328 -r afe3d1d6abdc usr.bin/gzip/gzip.1
--- a/usr.bin/gzip/gzip.1       Thu Jan 01 01:19:36 2004 +0000
+++ b/usr.bin/gzip/gzip.1       Thu Jan 01 02:44:09 2004 +0000
@@ -1,6 +1,6 @@
-.\"    $NetBSD: gzip.1,v 1.9 2003/12/26 15:24:00 wiz Exp $
+.\"    $NetBSD: gzip.1,v 1.10 2004/01/01 02:44:09 mrg Exp $
 .\"
-.\" Copyright (c) 1997, 2003 Matthew R. Green
+.\" Copyright (c) 1997, 2003, 2004 Matthew R. Green
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -26,7 +26,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd December 26, 2003
+.Dd January 1, 2004
 .Dt GZIP 1
 .Os
 .Sh NAME
@@ -90,6 +90,11 @@
 and
 .Fl d
 options are enabled.
+.P
+This version of
+.Nm
+is also capable of decompressing files compressed using the bzip2
+library.
 .Sh OPTIONS
 The following options are available:
 .Bl -tag -width XXrXXXrecursiveX
@@ -175,7 +180,7 @@
 This manual documents
 .Nx
 .Nm
-version 2.0.
+version 2.1.
 .Sh AUTHORS
 The
 .Nx
diff -r 33c65da14328 -r afe3d1d6abdc usr.bin/gzip/gzip.c
--- a/usr.bin/gzip/gzip.c       Thu Jan 01 01:19:36 2004 +0000
+++ b/usr.bin/gzip/gzip.c       Thu Jan 01 02:44:09 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gzip.c,v 1.9 2003/12/28 13:42:28 mrg Exp $     */
+/*     $NetBSD: gzip.c,v 1.10 2004/01/01 02:44:09 mrg Exp $    */
 
 /*
  * Copyright (c) 1997, 1998, 2003 Matthew R. Green
@@ -32,7 +32,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003 Matthew R. Green\n\
      All rights reserved.\n");
-__RCSID("$NetBSD: gzip.c,v 1.9 2003/12/28 13:42:28 mrg Exp $");
+__RCSID("$NetBSD: gzip.c,v 1.10 2004/01/01 02:44:09 mrg Exp $");
 #endif /* not lint */
 
 /*
@@ -43,6 +43,7 @@
  *
  * TODO:
  *     - handle .taz/.tgz files?
+ *     - use mmap where possible
  */
 
 #include <sys/param.h>
@@ -62,21 +63,34 @@
 #include <stdarg.h>
 #include <getopt.h>
 
-#ifndef GZ_SUFFIX
-# define GZ_SUFFIX ".gz"
-#endif
+/* what type of file are we dealing with */
+enum filetype {
+       FT_GZIP,
+       FT_BZIP2,
+       FT_LAST,
+       FT_UNKNOWN
+};
 
-#define BUFLEN 4096
+#define BZ_NO_STDIO
+#include <bzlib.h>
+
+#define BZ2_SUFFIX     ".bz2"
+#define BZIP2_MAGIC    "\102\132\150"
+
+#define GZ_SUFFIX      ".gz"
+
+#define BUFLEN         (32 * 1024)
 
 #define GZIP_MAGIC0    0x1F
-#define GZIP_MAGIC1    0x2B
+#define GZIP_MAGIC1    0x8B
+#define GZIP_OMAGIC1   0x9E
 
 #define ORIG_NAME      0x08
 
 /* Define this if you have the NetBSD gzopenfull(3) extension to zlib(3) */
 #define HAVE_ZLIB_GZOPENFULL 0
 
-static const char      gzip_version[] = "NetBSD gzip 2.0";
+static const char      gzip_version[] = "NetBSD gzip 2.1";
 
 static char    gzipflags[3];           /* `w' or `r', possible with [1-9] */
 static int     cflag;                  /* stdout mode */
@@ -89,9 +103,10 @@
 static int     rflag;                  /* recursive mode */
 static int     tflag;                  /* test */
 static int     vflag;                  /* verbose mode */
-static const char *Sflag = GZ_SUFFIX;  /* suffix (.gz) */
+static char    *Sflag;
+static char    *suffix;
 
-static int     suffix_len;             /* length of suffix; includes nul */
+#define suffix_len     (strlen(suffix) + 1)    /* len + nul */
 static char    *newfile;               /* name of newly created file */
 static char    *infile;                /* name of file coming in */
 
@@ -99,8 +114,6 @@
 static void    maybe_errx(int rv, const char *fmt, ...);
 static void    maybe_warn(const char *fmt, ...);
 static void    maybe_warnx(const char *fmt, ...);
-static void    usage(void);
-static void    display_version(void);
 static void    gz_compress(FILE *, gzFile);
 static off_t   gz_uncompress(gzFile, FILE *);
 static void    copymodes(const char *, struct stat *);
@@ -115,6 +128,9 @@
 static void    print_verbage(char *, char *, ssize_t, ssize_t);
 static void    print_test(char *, int);
 static void    print_list(int fd, off_t, const char *, time_t);
+static void    usage(void);
+static void    display_version(void);
+static off_t   unbzip2(int, int);
 
 int main(int, char *p[]);
 
@@ -138,9 +154,8 @@
        { "best",               no_argument,            0,      '9' },
 #if 0
        /*
-        * This is what else GNU gzip implements.  Maybe --list is
-        * useful, but --ascii isn't useful on NetBSD, and I don't
-        * care to have a --license.
+        * This is what else GNU gzip implements.  --ascii isn't useful
+        * on NetBSD, and I don't care to have a --license.
         */
        { "ascii",              no_argument,            0,      'a' },
        { "license",            no_argument,            0,      'L' },
@@ -156,6 +171,8 @@
        gzipflags[0] = 'w';
        gzipflags[1] = '\0';
 
+       suffix = GZ_SUFFIX;;
+
        /*
         * XXX
         * handle being called `gunzip', `zcat' and `gzcat'
@@ -227,8 +244,6 @@
        if (dflag)
                gzipflags[0] = 'r';
 
-       suffix_len = strlen(Sflag) + 1;
-
        if (argc == 0) {
                if (dflag)      /* stdin mode */
                        handle_stdin();
@@ -338,7 +353,7 @@
                                print_test(infile, 0);
                                return (0);
                        } else
-                               maybe_err(1, gzerror(in, &i));
+                               maybe_errx(1, gzerror(in, &i));
                } else if (len == 0) {
                        if (tflag)
                                print_test(infile, 1);
@@ -355,7 +370,7 @@
                        maybe_err(1, "failed fwrite");
        }
        if (gzclose(in) != Z_OK)
-               maybe_err(1, "failed gzclose");
+               maybe_errx(1, "failed gzclose");
 
        return (size);
 }
@@ -419,7 +434,7 @@
        if (cflag == 0) {
                (void)strncpy(outfile, file, MAXPATHLEN - suffix_len);
                outfile[MAXPATHLEN - suffix_len] = '\0';
-               (void)strlcat(outfile, Sflag, sizeof(outfile));
+               (void)strlcat(outfile, suffix, sizeof(outfile));
 
                if (fflag == 0) {
                        if (stat(outfile, &osb) == 0) {
@@ -505,29 +520,44 @@
        ssize_t len = strlen(file);
        int fd;
        unsigned char header1[10], name[PATH_MAX + 1];
-
-       if (cflag == 0 || lflag) {
-               s = &file[len - suffix_len + 1];
-               if (strncmp(s, Sflag, suffix_len) == 0) {
-                       (void)strncpy(outfile, file, len - suffix_len + 1);
-                       outfile[len - suffix_len + 1] = '\0';
-               } else
-                       maybe_err(1, "unknown suffix %s", s);
-       }
+       enum filetype method;
 
        /* gather the old name info */
 
        fd = open(file, O_RDONLY);
        if (fd < 0)
                maybe_err(1, "can't open %s", file);
-       if (read(fd, header1, 10) != 10)
+       if (read(fd, header1, 10) != 10) {
+               /* we don't want to fail here. */
+               if (fflag)
+                       goto close_it;
                maybe_err(1, "can't read %s", file);
+       }
 
-       if (fflag == 0 &&
-           (header1[0] != GZIP_MAGIC0 || header1[1] != GZIP_MAGIC1))
+       if (header1[0] == GZIP_MAGIC0 &&
+           (header1[1] == GZIP_MAGIC1 || header1[1] == GZIP_OMAGIC1))
+               method = FT_GZIP;
+       else if (memcmp(header1, BZIP2_MAGIC, 3) == 0 &&
+           header1[3] >= '0' && header1[3] <= '9') {
+               if (Sflag == NULL)
+                       suffix = BZ2_SUFFIX;
+               method = FT_BZIP2;
+       } else
+               method = FT_UNKNOWN;
+
+       if (fflag == 0 && method == FT_UNKNOWN)
                maybe_errx(1, "%s: not in gzip format", file);
 
-       if (Nflag || lflag) {
+       if (cflag == 0 || lflag) {
+               s = &file[len - suffix_len + 1];
+               if (strncmp(s, suffix, suffix_len) == 0) {
+                       (void)strncpy(outfile, file, len - suffix_len + 1);
+                       outfile[len - suffix_len + 1] = '\0';
+               } else if (lflag == 0)
+                       maybe_errx(1, "unknown suffix %s", s);
+       }
+
+       if (method == FT_GZIP && (Nflag || lflag)) {
                if (header1[3] & ORIG_NAME) {
                        size_t rbytes;
                        int i;
@@ -551,6 +581,7 @@
                        }
                }
        }
+close_it:
        close(fd);
 
        if ((cflag == 0 || lflag) && fflag == 0) {
@@ -568,34 +599,58 @@
                        goto lose;
        }
 
-       if (lflag) {
-               int fd;
+       if (method == FT_BZIP2) {
+               int in, out;
+
+               if ((in = open(file, O_RDONLY)) == -1)
+                       maybe_err(1, "open for read: %s", file);
+               if (cflag == 1)
+                       out = STDOUT_FILENO;
+               else 
+                       out = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
+               if (out == -1)
+                       maybe_err(1, "open for write: %s", outfile);
 
-               if ((fd = open(file, O_RDONLY)) == -1)



Home | Main Index | Thread Index | Old Index