Deleted Added
sdiff udiff text old ( 222287 ) new ( 226184 )
full compact
1/* $NetBSD: gzip.c,v 1.99 2011/03/23 12:59:44 tsutsui Exp $ */
2
3/*-
4 * Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:

--- 16 unchanged lines hidden (view full) ---

26 * SUCH DAMAGE.
27 *
28 */
29
30#include <sys/cdefs.h>
31#ifndef lint
32__COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006\
33 Matthew R. Green. All rights reserved.");
34__FBSDID("$FreeBSD: head/usr.bin/gzip/gzip.c 222287 2011-05-25 18:04:11Z delphij $");
35#endif /* not lint */
36
37/*
38 * gzip.c -- GPL free gzip using zlib.
39 *
40 * RFC 1950 covers the zlib format
41 * RFC 1951 covers the deflate format
42 * RFC 1952 covers the gzip format

--- 29 unchanged lines hidden (view full) ---

72 FT_BZIP2,
73#endif
74#ifndef NO_COMPRESS_SUPPORT
75 FT_Z,
76#endif
77#ifndef NO_PACK_SUPPORT
78 FT_PACK,
79#endif
80 FT_LAST,
81 FT_UNKNOWN
82};
83
84#ifndef NO_BZIP2_SUPPORT
85#include <bzlib.h>
86
87#define BZ2_SUFFIX ".bz2"

--- 4 unchanged lines hidden (view full) ---

92#define Z_SUFFIX ".Z"
93#define Z_MAGIC "\037\235"
94#endif
95
96#ifndef NO_PACK_SUPPORT
97#define PACK_MAGIC "\037\036"
98#endif
99
100#define GZ_SUFFIX ".gz"
101
102#define BUFLEN (64 * 1024)
103
104#define GZIP_MAGIC0 0x1F
105#define GZIP_MAGIC1 0x8B
106#define GZIP_OMAGIC1 0x9E
107

--- 26 unchanged lines hidden (view full) ---

134#ifndef NO_BZIP2_SUPPORT
135 SUFFIX(BZ2_SUFFIX, ""),
136 SUFFIX(".tbz", ".tar"),
137 SUFFIX(".tbz2", ".tar"),
138#endif
139#ifndef NO_COMPRESS_SUPPORT
140 SUFFIX(Z_SUFFIX, ""),
141#endif
142 SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
143#endif /* SMALL */
144#undef SUFFIX
145};
146#define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
147#define SUFFIX_MAXLEN 30
148
149static const char gzip_version[] = "FreeBSD gzip 20110523";
150
151#ifndef SMALL
152static const char gzip_copyright[] = \
153" Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green\n"
154" All rights reserved.\n"
155"\n"
156" Redistribution and use in source and binary forms, with or without\n"
157" modification, are permitted provided that the following conditions\n"

--- 36 unchanged lines hidden (view full) ---

194#define qflag 0
195#define tflag 0
196#endif
197
198static int exit_value = 0; /* exit value */
199
200static char *infile; /* name of file coming in */
201
202static void maybe_err(const char *fmt, ...) __dead2
203 __attribute__((__format__(__printf__, 1, 2)));
204#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
205static void maybe_errx(const char *fmt, ...) __dead2
206 __attribute__((__format__(__printf__, 1, 2)));
207#endif
208static void maybe_warn(const char *fmt, ...)
209 __attribute__((__format__(__printf__, 1, 2)));
210static void maybe_warnx(const char *fmt, ...)
211 __attribute__((__format__(__printf__, 1, 2)));
212static enum filetype file_gettype(u_char *);
213#ifdef SMALL
214#define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz)
215#endif
216static off_t gz_compress(int, int, off_t *, const char *, uint32_t);
217static off_t gz_uncompress(int, int, char *, size_t, off_t *, const char *);
218static off_t file_compress(char *, char *, size_t);
219static off_t file_uncompress(char *, char *, size_t);
220static void handle_pathname(char *);
221static void handle_file(char *, struct stat *);
222static void handle_stdin(void);
223static void handle_stdout(void);
224static void print_ratio(off_t, off_t, FILE *);
225static void print_list(int fd, off_t, const char *, time_t);
226static void usage(void);
227static void display_version(void);
228#ifndef SMALL
229static void display_license(void);
230static void sigint_handler(int);
231#endif
232static const suffixes_t *check_suffix(char *, int);
233static ssize_t read_retry(int, void *, size_t);
234
235#ifdef SMALL

--- 16 unchanged lines hidden (view full) ---

252static FILE *zdopen(int);
253static off_t zuncompress(FILE *, FILE *, char *, size_t, off_t *);
254#endif
255
256#ifndef NO_PACK_SUPPORT
257static off_t unpack(int, int, char *, size_t, off_t *);
258#endif
259
260int main(int, char **p);
261
262#ifdef SMALL
263#define getopt_long(a,b,c,d,e) getopt(a,b,c)
264#else
265static const struct option longopts[] = {
266 { "stdout", no_argument, 0, 'c' },
267 { "to-stdout", no_argument, 0, 'c' },
268 { "decompress", no_argument, 0, 'd' },

--- 182 unchanged lines hidden (view full) ---

451 if (qflag == 0) {
452 va_start(ap, fmt);
453 vwarn(fmt, ap);
454 va_end(ap);
455 }
456 exit(2);
457}
458
459#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
460/* ... without an errno. */
461void
462maybe_errx(const char *fmt, ...)
463{
464 va_list ap;
465
466 if (qflag == 0) {
467 va_start(ap, fmt);

--- 649 unchanged lines hidden (view full) ---

1117 return FT_Z;
1118 else
1119#endif
1120#ifndef NO_PACK_SUPPORT
1121 if (memcmp(buf, PACK_MAGIC, 2) == 0)
1122 return FT_PACK;
1123 else
1124#endif
1125 return FT_UNKNOWN;
1126}
1127
1128#ifndef SMALL
1129/* check the outfile is OK. */
1130static int
1131check_outfile(const char *outfile)
1132{

--- 231 unchanged lines hidden (view full) ---

1364 if (rbytes == -1)
1365 maybe_warn("can't read %s", file);
1366 else
1367 goto unexpected_EOF;
1368 goto lose;
1369 }
1370
1371 method = file_gettype(header1);
1372
1373#ifndef SMALL
1374 if (fflag == 0 && method == FT_UNKNOWN) {
1375 maybe_warnx("%s: not in gzip format", file);
1376 goto lose;
1377 }
1378
1379#endif
1380

--- 61 unchanged lines hidden (view full) ---

1442 goto lose;
1443 }
1444#ifndef SMALL
1445 remove_file = outfile;
1446#endif
1447 } else
1448 zfd = STDOUT_FILENO;
1449
1450#ifndef NO_BZIP2_SUPPORT
1451 if (method == FT_BZIP2) {
1452
1453 /* XXX */
1454 if (lflag) {
1455 maybe_warnx("no -l with bzip2 files");
1456 goto lose;
1457 }
1458
1459 size = unbzip2(fd, zfd, NULL, 0, NULL);
1460 } else
1461#endif
1462
1463#ifndef NO_COMPRESS_SUPPORT
1464 if (method == FT_Z) {
1465 FILE *in, *out;
1466
1467 /* XXX */
1468 if (lflag) {
1469 maybe_warnx("no -l with Lempel-Ziv files");
1470 goto lose;
1471 }
1472

--- 16 unchanged lines hidden (view full) ---

1489 unlink(outfile);
1490 (void)fclose(out);
1491 }
1492 if (fclose(out) != 0) {
1493 maybe_warn("failed outfile fclose");
1494 unlink(outfile);
1495 goto lose;
1496 }
1497 } else
1498#endif
1499
1500#ifndef NO_PACK_SUPPORT
1501 if (method == FT_PACK) {
1502 if (lflag) {
1503 maybe_warnx("no -l with packed files");
1504 goto lose;
1505 }
1506
1507 size = unpack(fd, zfd, NULL, 0, NULL);
1508 } else
1509#endif
1510
1511#ifndef SMALL
1512 if (method == FT_UNKNOWN) {
1513 if (lflag) {
1514 maybe_warnx("no -l for unknown filetypes");
1515 goto lose;
1516 }
1517 size = cat_fd(NULL, 0, NULL, fd);
1518 } else
1519#endif
1520 {
1521 if (lflag) {
1522 print_list(fd, isb.st_size, outfile, isb.st_mtime);
1523 close(fd);
1524 return -1; /* XXX */
1525 }
1526
1527 size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
1528 }
1529
1530 if (close(fd) != 0)
1531 maybe_warn("couldn't close input");
1532 if (zfd != STDOUT_FILENO && close(zfd) != 0)
1533 maybe_warn("couldn't close output");
1534
1535 if (size == -1) {

--- 156 unchanged lines hidden (view full) ---

1692#endif
1693#ifndef NO_COMPRESS_SUPPORT
1694 case FT_Z:
1695 if ((in = zdopen(STDIN_FILENO)) == NULL) {
1696 maybe_warnx("zopen of stdin");
1697 return;
1698 }
1699
1700 usize = zuncompress(in, stdout, (char *)header1, sizeof header1, &gsize);
1701 fclose(in);
1702 break;
1703#endif
1704#ifndef NO_PACK_SUPPORT
1705 case FT_PACK:
1706 usize = unpack(STDIN_FILENO, STDOUT_FILENO,
1707 (char *)header1, sizeof header1, &gsize);
1708 break;
1709#endif
1710 }
1711
1712#ifndef SMALL
1713 if (vflag && !tflag && usize != -1 && gsize != -1)
1714 print_verbage(NULL, NULL, usize, gsize);
1715 if (vflag && tflag)
1716 print_test("(stdin)", usize != -1);
1717#endif

--- 351 unchanged lines hidden (view full) ---

2069}
2070
2071#ifndef SMALL
2072/* display the license information of FreeBSD gzip */
2073static void
2074display_license(void)
2075{
2076
2077 fprintf(stderr, "%s (based on NetBSD gzip 20091011)\n", gzip_version);
2078 fprintf(stderr, "%s\n", gzip_copyright);
2079 exit(0);
2080}
2081#endif
2082
2083/* display the version of NetBSD gzip */
2084static void
2085display_version(void)

--- 7 unchanged lines hidden (view full) ---

2093#include "unbzip2.c"
2094#endif
2095#ifndef NO_COMPRESS_SUPPORT
2096#include "zuncompress.c"
2097#endif
2098#ifndef NO_PACK_SUPPORT
2099#include "unpack.c"
2100#endif
2101
2102static ssize_t
2103read_retry(int fd, void *buf, size_t sz)
2104{
2105 char *cp = buf;
2106 size_t left = MIN(sz, (size_t) SSIZE_MAX);
2107
2108 while (left > 0) {

--- 14 unchanged lines hidden ---