diff options
author | Michael Shalayeff <mickey@cvs.openbsd.org> | 2002-12-08 16:07:55 +0000 |
---|---|---|
committer | Michael Shalayeff <mickey@cvs.openbsd.org> | 2002-12-08 16:07:55 +0000 |
commit | b967dfa0fdbc44a096efb127ae28bbeaf82b640a (patch) | |
tree | b0e2b5eb68f63545513412ec2ce779cd5a579970 /usr.bin | |
parent | e8243efe1973f449fe06bb14d700b75a7772f827 (diff) |
grop long option names, like gzip.
add most of the gzip's options (a few unimplemented yet).
some cleaning, etc.
millert@ ok
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/compress/compress.1 | 48 | ||||
-rw-r--r-- | usr.bin/compress/compress.h | 21 | ||||
-rw-r--r-- | usr.bin/compress/gzopen.c | 15 | ||||
-rw-r--r-- | usr.bin/compress/main.c | 319 | ||||
-rw-r--r-- | usr.bin/compress/zopen.c | 27 |
5 files changed, 273 insertions, 157 deletions
diff --git a/usr.bin/compress/compress.1 b/usr.bin/compress/compress.1 index 4305de4a19c..1d55224a8d0 100644 --- a/usr.bin/compress/compress.1 +++ b/usr.bin/compress/compress.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: compress.1,v 1.13 2002/05/30 20:34:57 mickey Exp $ +.\" $OpenBSD: compress.1,v 1.14 2002/12/08 16:07:54 mickey Exp $ .\" $NetBSD: compress.1,v 1.5 1995/03/26 09:44:34 glass Exp $ .\" .\" Copyright (c) 1986, 1990, 1993 @@ -47,12 +47,14 @@ .Nd compress and expand data .Sh SYNOPSIS .Nm compress -.Op Fl cdftgOqv +.Op Fl LV +.Nm compress +.Op Fl cdfgOqtv123456789 .Op Fl b Ar bits .Op Fl o Ar filename .Op Ar .Nm uncompress -.Op Fl cftoqv +.Op Fl cfoqtv .Op Fl o Ar filename .Op Ar .Nm zcat @@ -66,19 +68,26 @@ reduces the size of the named files using adaptive Lempel-Ziv coding. Each .Ar file is renamed to the same name plus the extension -.Dq .Z . +.Dq .Z , +or +.Dq .gz +(in deflate mode). As many of the modification time, access time, file flags, file mode, user ID, and group ID as allowed by permissions are retained in the new file. If compression would not reduce the size of a .Ar file , -the file is ignored. +the file is ignored (unless +.Fl f +is used). .Pp The .Nm uncompress utility restores compressed files to their original form, renaming the files by removing the .Dq .Z +or +.Dq .gz extension. .Pp The @@ -101,34 +110,49 @@ not removed, and the attributes of the input file are not retained. .Pp The options are as follows: .Bl -tag -width Ds +.It Fl L +Display the source code license and exit. +.It Fl V +Display the program version (RCS Ids of the source files) and exit. .It Fl b Ar bits Specify the .Ar bits code limit (see below). .It Fl c Compressed or uncompressed output is written to the standard output. -No files are modified. +No files are modified (force +.Nm zcat +mode). .It Fl d -Decompress the source files instead of compressing them. +Decompress the source files instead of compressing them (force +.Nm uncompress +mode). .It Fl f Force compression of .Ar file , even if it is not actually reduced in size. Additionally, files are overwritten without prompting for confirmation. .It Fl g -Use deflate scheme which reportedly provides better compression rates. +Use deflate scheme which reportedly provides better compression rates (force +.Nm gzip +mode). +.It Fl 1...9 +Use deflate scheme with compression factor of one to nine. .It Fl O -Use old compression method (default is based on the program name). +Use old compression method. .It Fl o Ar filename Set the output file name. +.It Fl S Ar suffix +Set suffix for compressed files. .It Fl t Test the integrity of each file leaving any files intact. .It Fl q -Be quiet, suppress any messages. +Be quiet, suppress all messages. .It Fl v -Print the percentage reduction of each file. +Print the percentage reduction of each file and other information. .El .Pp +In normal mode, .Nm uses a modified Lempel-Ziv algorithm. Common substrings in the file are first replaced by 9-bit codes 257 and up. @@ -181,7 +205,7 @@ time to compute. .Pp The .Nm -utility exits 0 on success, 1 if an error occurred, or 2 if one or +utility exits with 0 on success, 1 if an error occurred, or 2 if one or more files were not compressed because they would have grown in size (and .Fl f diff --git a/usr.bin/compress/compress.h b/usr.bin/compress/compress.h index e12f6802d69..38e3767497d 100644 --- a/usr.bin/compress/compress.h +++ b/usr.bin/compress/compress.h @@ -1,4 +1,4 @@ -/* $OpenBSD: compress.h,v 1.2 2002/02/16 21:27:45 millert Exp $ */ +/* $OpenBSD: compress.h,v 1.3 2002/12/08 16:07:54 mickey Exp $ */ /* * Copyright (c) 1997 Michael Shalayeff @@ -18,8 +18,8 @@ * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL @@ -32,17 +32,32 @@ * */ +/* + * making it any bigger does not affect perfomance very much. + * actually this value is just a little bit better than 8192. + */ #define Z_BUFSIZE 16384 +extern const char main_rcsid[], z_rcsid[], gz_rcsid[], pkzip_rcsid[], + pack_rcsid[], lzh_rcsid[]; + extern int z_check_header(int, struct stat *, const char *); extern void *z_open(int, const char *, int); extern FILE *zopen(const char *, const char *,int); extern int zread(void *, char *, int); extern int zwrite(void *, const char *, int); extern int zclose(void *); + extern int gz_check_header(int, struct stat *, const char *); extern void *gz_open(int, const char *, int); extern int gz_read(void *, char *, int); extern int gz_write(void *, const char *, int); extern int gz_close(void *); extern int gz_flush(void *, int); + +extern int lzh_check_header(int, struct stat *, const char *); +extern void *lzh_open(int, const char *, int); +extern int lzh_read(void *, char *, int); +extern int lzh_write(void *, const char *, int); +extern int lzh_close(void *); +extern int lzh_flush(void *, int); diff --git a/usr.bin/compress/gzopen.c b/usr.bin/compress/gzopen.c index 43f997041fa..831a6feee60 100644 --- a/usr.bin/compress/gzopen.c +++ b/usr.bin/compress/gzopen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gzopen.c,v 1.4 2002/08/12 00:42:56 aaron Exp $ */ +/* $OpenBSD: gzopen.c,v 1.5 2002/12/08 16:07:54 mickey Exp $ */ /* * Copyright (c) 1997 Michael Shalayeff @@ -18,8 +18,8 @@ * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL @@ -63,6 +63,9 @@ (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). */ +const char gz_rcsid[] = + "$OpenBSD: gzopen.c,v 1.5 2002/12/08 16:07:54 mickey Exp $"; + #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> @@ -94,7 +97,7 @@ struct gz_stream { } gz_stream; -static u_char gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ +static const u_char gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ static int put_int32(gz_stream *, u_int32_t); static u_int32_t get_int32(gz_stream *); @@ -285,7 +288,7 @@ get_byte(s) { if (s->z_eof) return EOF; - + if (s->z_stream.avail_in == 0) { errno = 0; s->z_stream.avail_in = read(s->z_fd, s->z_buf, Z_BUFSIZE); @@ -299,7 +302,7 @@ get_byte(s) return *s->z_stream.next_in++; } -static u_int32_t +static u_int32_t get_int32 (s) gz_stream *s; { diff --git a/usr.bin/compress/main.c b/usr.bin/compress/main.c index 7f3da2cf249..adccf2d5401 100644 --- a/usr.bin/compress/main.c +++ b/usr.bin/compress/main.c @@ -1,47 +1,42 @@ -/* $OpenBSD: main.c,v 1.17 2002/02/16 21:27:45 millert Exp $ */ - -/*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -static char copyright[] = +/* $OpenBSD: main.c,v 1.18 2002/12/08 16:07:54 mickey Exp $ */ + +static const char copyright[] = "@(#) Copyright (c) 1992, 1993\n\ - The Regents of the University of California. All rights reserved.\n"; + The Regents of the University of California. All rights reserved.\n" +"Copyright (c) 1997-2002 Michael Shalayeff\n"; + +static const char license[] = +"\n" +" Redistribution and use in source and binary forms, with or without\n" +" modification, are permitted provided that the following conditions\n" +" are met:\n" +" 1. Redistributions of source code must retain the above copyright\n" +" notice, this list of conditions and the following disclaimer.\n" +" 2. Redistributions in binary form must reproduce the above copyright\n" +" notice, this list of conditions and the following disclaimer in the\n" +" documentation and/or other materials provided with the distribution.\n" +" 3. All advertising materials mentioning features or use of this software\n" +" must display the following acknowledgement:\n" +" This product includes software developed by the University of\n" +" California, Berkeley and its contributors.\n" +"\n" +" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n" +" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n" +" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n" +" IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,\n" +" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n" +" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n" +" SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n" +" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n" +" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n" +" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\n" +" THE POSSIBILITY OF SUCH DAMAGE.\n"; #ifndef lint #if 0 static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94"; #else -static char rcsid[] = "$OpenBSD: main.c,v 1.17 2002/02/16 21:27:45 millert Exp $"; +static const char main_rcsid[] = "$OpenBSD: main.c,v 1.18 2002/12/08 16:07:54 mickey Exp $"; #endif #endif /* not lint */ @@ -49,6 +44,7 @@ static char rcsid[] = "$OpenBSD: main.c,v 1.17 2002/02/16 21:27:45 millert Exp $ #include <sys/time.h> #include <sys/stat.h> +#include <getopt.h> #include <err.h> #include <errno.h> #include <stdio.h> @@ -61,10 +57,12 @@ static char rcsid[] = "$OpenBSD: main.c,v 1.17 2002/02/16 21:27:45 millert Exp $ #define min(a,b) ((a) < (b)? (a) : (b)) -int pipin = 0, force = 0, verbose = 0, testmode = 0, list = 0, nosave = 0; +int pipin, force, verbose, testmode, list, nosave; +int savename, recurse; +int bits, cat, decomp; extern char *__progname; -struct compressor { +const struct compressor { char *name; char *suffix; int (*check_header)(int, struct stat *, const char *); @@ -77,36 +75,64 @@ struct compressor { { "compress", ".Z", z_check_header, z_open, zread, zwrite, zclose }, #define M_DEFLATE (&c_table[1]) { "deflate", ".gz", gz_check_header, gz_open, gz_read, gz_write, gz_close }, +#if 0 +#define M_LZH (&c_table[2]) + { "lzh", ".lzh", lzh_check_header, lzh_open, lzh_read, lzh_write, lzh_close }, +#define M_ZIP (&c_table[3]) + { "zip", ".zip", zip_check_header, zip_open, zip_read, zip_write, zip_close }, +#define M_PACK (&c_table[4]) + { "pack", ".pak",pak_check_header, pak_open, pak_read, pak_write, pak_close }, +#endif { NULL } }; -int permission(char *); -void setfile(char *, struct stat *); +int permission(const char *); +void setfile(const char *, struct stat *); void usage(void); -int compress -(const char *, const char *, struct compressor *, int); -int decompress -(const char *, const char *, struct compressor *, int); -struct compressor *check_method(int, const char *); - -struct stat sb, osb; +int compress(const char *, const char *, const struct compressor *, int, struct stat *); +int decompress(const char *, const char *, const struct compressor *, int, struct stat *); +const struct compressor *check_method(int, struct stat *, const char *); + +#define OPTSTRING "123456789ab:cdfhlLnNqrS:tvV" +const struct option longopts[] = { + { "ascii", no_argument, 0, 'a' }, + { "stdout", no_argument, 0, 'c' }, + { "to-stdout", no_argument, 0, 'c' }, + { "decompress", no_argument, 0, 'd' }, + { "uncompress", no_argument, 0, 'd' }, + { "force", no_argument, 0, 'f' }, + { "help", no_argument, 0, 'h' }, + { "list", no_argument, 0, 'l' }, + { "license", no_argument, 0, 'L' }, + { "no-name", no_argument, 0, 'n' }, + { "name", no_argument, 0, 'N' }, + { "quiet", no_argument, 0, 'q' }, + { "recursive", no_argument, 0, 'r' }, + { "suffix", required_argument, 0, 'S' }, + { "test", no_argument, 0, 't' }, + { "verbose", no_argument, 0, 'v' }, + { "version", no_argument, 0, 'V' }, + { "fast", no_argument, 0, '1' }, + { "best", no_argument, 0, '9' }, + { NULL } +}; int main(argc, argv) int argc; char *argv[]; { - int ch, bits, cat, decomp, error; - struct compressor *method; - int exists, isreg, oreg; - char *infile, outfile[MAXPATHLEN+4], suffix[16]; - char *p; - int rc = 0; + struct stat sb, osb; + const struct compressor *method; + char *p, *s, *infile, outfile[MAXPATHLEN], suffix[16]; + char *nargv[512]; /* some estimate based on ARG_MAX */ + int exists, isreg, oreg, ch, error, i, rc = 0; bits = cat = decomp = 0; p = __progname; if (p[0] == 'g') { method = M_DEFLATE; + bits = 6; p++; } else method = M_COMPRESS; @@ -126,10 +152,28 @@ main(argc, argv) errx(1, "unknown program name"); } + strlcpy(suffix, method->suffix, sizeof(suffix)); outfile[0] = '\0'; - while ((ch = getopt(argc, argv, "0123456789b:cdfghlnOo:qS:tv")) != -1) + + if ((s = getenv("GZIP")) != NULL) { + char *last; + + nargv[0] = *argv++; + for (i = 1, (p = strtok_r(s, " ", &last)); p; + (p = strtok_r(NULL, " ", &last)), i++) + if (i < sizeof(nargv)/sizeof(nargv[1]) - argc - 1) + nargv[i] = p; + else { + errx(1, "GZIP is too long"); + } + argc += i - 1; + while ((nargv[i++] = *argv++)) + ; + argv = nargv; + } + + while ((ch = getopt_long(argc, argv, OPTSTRING, longopts, NULL)) != -1) switch(ch) { - case '0': case '1': case '2': case '3': @@ -140,8 +184,12 @@ main(argc, argv) case '8': case '9': method = M_DEFLATE; + strlcpy(suffix, method->suffix, sizeof(suffix)); bits = ch - '0'; break; + case 'a': + warnx("option -a is ignored on this system"); + break; case 'b': bits = strtol(optarg, &p, 10); /* @@ -163,24 +211,26 @@ main(argc, argv) break; case 'g': method = M_DEFLATE; + strlcpy(suffix, method->suffix, sizeof(suffix)); + bits = 6; break; case 'l': list++; break; - case 'L': - fputs(copyright, stderr); case 'n': nosave++; break; case 'N': - nosave = 0; + nosave = 0; /* XXX not yet */ break; case 'O': method = M_COMPRESS; + strlcpy(suffix, method->suffix, sizeof(suffix)); break; case 'o': - strncpy(outfile, optarg, sizeof(outfile)-1); - outfile[sizeof(outfile)-1] = '\0'; + if (strlcpy(outfile, optarg, + sizeof(outfile)) >= sizeof(outfile)) + errx(1, "-o argument is too long"); break; case 'q': verbose = -1; @@ -189,14 +239,27 @@ main(argc, argv) p = suffix; if (optarg[0] != '.') *p++ = '.'; - strncpy(p, optarg, sizeof(suffix) - (p - suffix) - 1); + strlcpy(p, optarg, sizeof(suffix) - (p - suffix)); + p = optarg; break; case 't': testmode++; break; + case 'V': + printf("%s\n%s\n%s\n", main_rcsid, + z_rcsid, gz_rcsid); + return (0); case 'v': verbose++; break; + case 'L': + fputs(copyright, stderr); + fputs(license, stderr); + return (0); + case 'r': + recurse++; /* XXX not yet */ + break; + case 'h': case '?': default: @@ -209,26 +272,20 @@ main(argc, argv) if (*argv != NULL) { infile = *argv; if (outfile[0] == '\0') { - if (!decomp && !cat && outfile[0] == '\0') { + if (!decomp && !cat) { int len; char *p; - snprintf(outfile, sizeof(outfile), - "%s%s", infile, - method->suffix); + if (snprintf(outfile, sizeof(outfile), + "%s%s", infile, suffix) >= sizeof(outfile)) + errx(1, "out file name is too long"); - len = strlen(outfile); - if (len > MAXPATHLEN) { - errx(1, "pathname%s too long", - method->suffix); - } - p = strrchr(outfile, '/'); if (p == NULL) p = outfile; len = strlen(p); if (len > NAME_MAX) { errx(1, "filename%s too long", - method->suffix); + suffix); } } else if (decomp && !cat) { char *p = strrchr(infile, '.'); @@ -238,14 +295,12 @@ main(argc, argv) !strcmp(p, method->suffix); method++) ; - if (method->name != NULL) { - int l = min(sizeof(outfile), - (p - infile)); - strncpy(outfile, infile, l); - outfile[l] = '\0'; - } + if (method->name != NULL) + strlcpy(outfile, infile, + min(sizeof(outfile), + (p - infile) + 1)); } - } + } } else { infile = "/dev/stdin"; pipin++; @@ -261,13 +316,13 @@ main(argc, argv) exists = !stat(outfile, &sb); if (!force && exists && S_ISREG(sb.st_mode) && !permission(outfile)) { - argv++; + argv++; continue; } isreg = oreg = !exists || S_ISREG(sb.st_mode); if (stat(infile, &sb) != 0 && verbose >= 0) - err(1, "%s", infile); + err(1, "input: %s", infile); if (!S_ISREG(sb.st_mode)) isreg = 0; @@ -276,7 +331,7 @@ main(argc, argv) fprintf(stderr, "%s:\t", infile); error = (decomp? decompress: compress) - (infile, outfile, method, bits); + (infile, outfile, method, bits, &sb); if (!error && isreg && stat(outfile, &osb) == 0) { @@ -285,13 +340,13 @@ main(argc, argv) fprintf(stderr, "file would grow; " "left unmodified\n"); error = 1; - rc = 2; + rc = rc? rc : 2; } else { setfile(outfile, &sb); if (unlink(infile) && verbose >= 0) - warn("%s", infile); + warn("input: %s", infile); if (verbose > 0) { u_int ratio; @@ -307,9 +362,13 @@ main(argc, argv) } if (error && oreg && unlink(outfile) && errno != ENOENT && - verbose >= 0) - warn("%s", outfile); - else if (!error && verbose > 0) + verbose >= 0) { + if (force) { + warn("output: %s", outfile); + rc = 1; + } else + err(1, "output: %s", outfile); + } else if (!error && verbose > 0) fputs("OK\n", stderr); outfile[0] = '\0'; @@ -318,22 +377,21 @@ main(argc, argv) } while (*argv != NULL); - return (rc); + exit(rc); } int -compress(in, out, method, bits) +compress(in, out, method, bits, sb) const char *in; const char *out; - struct compressor *method; + const struct compressor *method; int bits; + struct stat *sb; { - int ifd; - int ofd; + u_char buf[Z_BUFSIZE]; + int error, ifd, ofd; void *cookie; ssize_t nr; - u_char buf[Z_BUFSIZE]; - int error; error = 0; cookie = NULL; @@ -351,8 +409,13 @@ compress(in, out, method, bits) return -1; } - if ((ifd = open(in, O_RDONLY)) >= 0 && - (cookie = (*method->open)(ofd, "w", bits)) != NULL) { + if ((ifd = open(in, O_RDONLY)) < 0) { + if (verbose >= 0) + warn("%s", out); + return -1; + } + + if ((cookie = (*method->open)(ofd, "w", bits)) != NULL) { while ((nr = read(ifd, buf, sizeof(buf))) > 0) if ((method->write)(cookie, buf, nr) != nr) { @@ -363,7 +426,7 @@ compress(in, out, method, bits) } } - if (ifd < 0 || close(ifd) || nr < 0) { + if (cookie == NULL || nr < 0) { if (!error && verbose >= 0) warn("%s", in); error++; @@ -376,19 +439,25 @@ compress(in, out, method, bits) (void) close(ofd); } + if (close(ifd)) { + if (!error && verbose >= 0) + warn("%s", out); + error++; + } + return error? -1 : 0; } -struct compressor * -check_method(fd, out) +const struct compressor * +check_method(fd, sb, out) int fd; + struct stat *sb; const char *out; { - struct compressor *method; + const struct compressor *method; for (method = &c_table[0]; - method->name != NULL && - !(*method->check_header)(fd, &sb, out); + method->name != NULL && !(*method->check_header)(fd, sb, out); method++) ; @@ -399,18 +468,17 @@ check_method(fd, out) } int -decompress(in, out, method, bits) +decompress(in, out, method, bits, sb) const char *in; const char *out; - struct compressor *method; + const struct compressor *method; int bits; + struct stat *sb; { - int ifd; - int ofd; + u_char buf[Z_BUFSIZE]; + int error, ifd, ofd; void *cookie; ssize_t nr; - u_char buf[Z_BUFSIZE]; - int error; error = 0; cookie = NULL; @@ -429,15 +497,20 @@ decompress(in, out, method, bits) return -1; } - if (!pipin && (method = check_method(ifd, out)) == NULL) { + if (!pipin && (method = check_method(ifd, sb, out)) == NULL) { if (verbose >= 0) warnx("%s: unrecognized file format", in); close (ifd); return -1; } - if ((ofd = open(out, O_WRONLY|O_CREAT, S_IWUSR)) >= 0 && - (cookie = (*method->open)(ifd, "r", bits)) != NULL) { + if ((ofd = open(out, O_WRONLY|O_CREAT, S_IWUSR)) < 0) { + if (verbose >= 0) + warn("%s", in); + return -1; + } + + if ((cookie = (*method->open)(ifd, "r", bits)) != NULL) { while ((nr = (method->read)(cookie, buf, sizeof(buf))) > 0) if (write(ofd, buf, nr) != nr) { @@ -448,17 +521,17 @@ decompress(in, out, method, bits) } } - if (ofd < 0 || close(ofd)) { + if (cookie == NULL || (method->close)(cookie) || nr < 0) { if (!error && verbose >= 0) - warn("%s", out); + warn("%s", in); error++; + close (ifd); } - if (cookie == NULL || (method->close)(cookie) || nr < 0) { + if (close(ofd)) { if (!error && verbose >= 0) - warn("%s", in); + warn("%s", out); error++; - (void) close (ifd); } return error; @@ -466,10 +539,10 @@ decompress(in, out, method, bits) void setfile(name, fs) - char *name; + const char *name; struct stat *fs; { - static struct timeval tv[2]; + struct timeval tv[2]; fs->st_mode &= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; @@ -498,7 +571,7 @@ setfile(name, fs) int permission(fname) - char *fname; + const char *fname; { int ch, first; @@ -515,8 +588,8 @@ void usage() { fprintf(stderr, - "usage: %s [-cdfghlnOtqv] [-b <bits>] [-[0-9]] [file ...]\n", - __progname); + "usage: %s [-cdfghlnLOqrStvV] [-b <bits>] [-[0-9]] [file ...]\n", + __progname); exit(1); } diff --git a/usr.bin/compress/zopen.c b/usr.bin/compress/zopen.c index 97eb7fb0c60..133eb129175 100644 --- a/usr.bin/compress/zopen.c +++ b/usr.bin/compress/zopen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: zopen.c,v 1.8 2002/02/16 21:27:45 millert Exp $ */ +/* $OpenBSD: zopen.c,v 1.9 2002/12/08 16:07:54 mickey Exp $ */ /* $NetBSD: zopen.c,v 1.5 1995/03/26 09:44:53 glass Exp $ */ /*- @@ -36,15 +36,16 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * From: @(#)zopen.c 8.1 (Berkeley) 6/27/93 */ -#if defined(LIBC_SCCS) && !defined(lint) #if 0 static char sccsid[] = "@(#)zopen.c 8.1 (Berkeley) 6/27/93"; #else -static char rcsid[] = "$OpenBSD: zopen.c,v 1.8 2002/02/16 21:27:45 millert Exp $"; +const char z_rcsid[] = + "$OpenBSD: zopen.c,v 1.9 2002/12/08 16:07:54 mickey Exp $"; #endif -#endif /* LIBC_SCCS and not lint */ /*- * fcompress.c - File compression ala IEEE Computer, June 1984. @@ -89,7 +90,7 @@ static char rcsid[] = "$OpenBSD: zopen.c,v 1.8 2002/02/16 21:27:45 millert Exp $ typedef long code_int; typedef long count_int; -static u_char z_magic[] = +static const u_char z_magic[] = {'\037', '\235'}; /* 1F 9D */ #define BIT_MASK 0x1f /* Defines for third byte of header. */ @@ -186,7 +187,7 @@ struct s_zstate { #define CLEAR 256 /* Table clear output code. */ static int cl_block(struct s_zstate *); -static void cl_hash(struct s_zstate *, register count_int); +static void cl_hash(struct s_zstate *, count_int); static code_int getcode(struct s_zstate *); static int output(struct s_zstate *, code_int); @@ -195,7 +196,7 @@ static int output(struct s_zstate *, code_int); * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19. * * Algorithm: - * Modified Lempel-Ziv method (LZW). Basically finds common + * Modified Lempel-Ziv method (LZW). Basically finds common * substrings and replaces them with a variable size code. This is * deterministic, and can be done on the fly. Thus, the decompression * procedure needs no input table, but tracks the way the table was built. @@ -279,7 +280,7 @@ zwrite(cookie, wbp, num) zs->zs_ent); /* Xor hashing. */ i = ((c << zs->zs_hshift) ^ zs->zs_ent); - + if (htabof(i) == zs->zs_fcode) { zs->zs_ent = codetabof(i); continue; @@ -345,14 +346,14 @@ zclose(cookie) /*- * Output the given code. * Inputs: - * code: A n_bits-bit integer. If == -1, then EOF. This assumes + * code: A n_bits-bit integer. If == -1, then EOF. This assumes * that n_bits =< (long)wordsize - 1. * Outputs: - * Outputs code to the file. + * Outputs code to the file. * Assumptions: * Chars are 8 bits long. * Algorithm: - * Maintain a BITS character long buffer (so that 8 codes will + * Maintain a BITS character long buffer (so that 8 codes will * fit in it exactly). Use the VAX insv instruction to insert each * code in turn. When the buffer fills up empty it and start over. */ @@ -568,9 +569,9 @@ eof: return (num - count); /*- * Read one code from the standard input. If EOF, return -1. * Inputs: - * stdin + * stdin * Outputs: - * code or -1 is returned. + * code or -1 is returned. */ static code_int getcode(zs) |