summaryrefslogtreecommitdiffstats
path: root/usr.bin/gzip/gzip.c
diff options
context:
space:
mode:
authordelphij <delphij@FreeBSD.org>2011-10-10 06:37:32 +0000
committerdelphij <delphij@FreeBSD.org>2011-10-10 06:37:32 +0000
commit2ccfd227c99957cc227d891e8df4a13e3911407b (patch)
tree3cf242dc34928d982f3b416e9a625b307aad92de /usr.bin/gzip/gzip.c
parent9de53f6dc0c192bb58cabb93558ce6e89af3d196 (diff)
downloadFreeBSD-src-2ccfd227c99957cc227d891e8df4a13e3911407b.zip
FreeBSD-src-2ccfd227c99957cc227d891e8df4a13e3911407b.tar.gz
Incorporate recent changes from NetBSD. Most notable change is the addition
of support of decompressing xz files. Obtained from: NetBSD
Diffstat (limited to 'usr.bin/gzip/gzip.c')
-rw-r--r--usr.bin/gzip/gzip.c95
1 files changed, 67 insertions, 28 deletions
diff --git a/usr.bin/gzip/gzip.c b/usr.bin/gzip/gzip.c
index 217660e..d7477dd 100644
--- a/usr.bin/gzip/gzip.c
+++ b/usr.bin/gzip/gzip.c
@@ -1,4 +1,4 @@
-/* $NetBSD: gzip.c,v 1.99 2011/03/23 12:59:44 tsutsui Exp $ */
+/* $NetBSD: gzip.c,v 1.105 2011/08/30 23:06:00 joerg Exp $ */
/*-
* Copyright (c) 1997, 1998, 2003, 2004, 2006 Matthew R. Green
@@ -77,6 +77,9 @@ enum filetype {
#ifndef NO_PACK_SUPPORT
FT_PACK,
#endif
+#ifndef NO_XZ_SUPPORT
+ FT_XZ,
+#endif
FT_LAST,
FT_UNKNOWN
};
@@ -97,6 +100,12 @@ enum filetype {
#define PACK_MAGIC "\037\036"
#endif
+#ifndef NO_XZ_SUPPORT
+#include <lzma.h>
+#define XZ_SUFFIX ".xz"
+#define XZ_MAGIC "\3757zXZ"
+#endif
+
#define GZ_SUFFIX ".gz"
#define BUFLEN (64 * 1024)
@@ -139,6 +148,9 @@ static suffixes_t suffixes[] = {
#ifndef NO_COMPRESS_SUPPORT
SUFFIX(Z_SUFFIX, ""),
#endif
+#ifndef NO_XZ_SUPPORT
+ SUFFIX(XZ_SUFFIX, ""),
+#endif
SUFFIX(GZ_SUFFIX, ""), /* Overwritten by -S "" */
#endif /* SMALL */
#undef SUFFIX
@@ -146,7 +158,7 @@ static suffixes_t suffixes[] = {
#define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0])
#define SUFFIX_MAXLEN 30
-static const char gzip_version[] = "FreeBSD gzip 20110523";
+static const char gzip_version[] = "FreeBSD gzip 20111009";
#ifndef SMALL
static const char gzip_copyright[] = \
@@ -199,16 +211,13 @@ static int exit_value = 0; /* exit value */
static char *infile; /* name of file coming in */
-static void maybe_err(const char *fmt, ...) __dead2
- __attribute__((__format__(__printf__, 1, 2)));
-#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
-static void maybe_errx(const char *fmt, ...) __dead2
- __attribute__((__format__(__printf__, 1, 2)));
+static void maybe_err(const char *fmt, ...) __printflike(1, 2) __dead2;
+#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
+ !defined(NO_XZ_SUPPORT)
+static void maybe_errx(const char *fmt, ...) __printflike(1, 2) __dead2;
#endif
-static void maybe_warn(const char *fmt, ...)
- __attribute__((__format__(__printf__, 1, 2)));
-static void maybe_warnx(const char *fmt, ...)
- __attribute__((__format__(__printf__, 1, 2)));
+static void maybe_warn(const char *fmt, ...) __printflike(1, 2);
+static void maybe_warnx(const char *fmt, ...) __printflike(1, 2);
static enum filetype file_gettype(u_char *);
#ifdef SMALL
#define gz_compress(if, of, sz, fn, tm) gz_compress(if, of, sz)
@@ -223,8 +232,8 @@ static void handle_stdin(void);
static void handle_stdout(void);
static void print_ratio(off_t, off_t, FILE *);
static void print_list(int fd, off_t, const char *, time_t);
-static void usage(void);
-static void display_version(void);
+static void usage(void) __dead2;
+static void display_version(void) __dead2;
#ifndef SMALL
static void display_license(void);
static void sigint_handler(int);
@@ -257,7 +266,9 @@ static off_t zuncompress(FILE *, FILE *, char *, size_t, off_t *);
static off_t unpack(int, int, char *, size_t, off_t *);
#endif
-int main(int, char **p);
+#ifndef NO_XZ_SUPPORT
+static off_t unxz(int, int, char *, size_t, off_t *);
+#endif
#ifdef SMALL
#define getopt_long(a,b,c,d,e) getopt(a,b,c)
@@ -456,7 +467,8 @@ maybe_err(const char *fmt, ...)
exit(2);
}
-#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT)
+#if !defined(NO_BZIP2_SUPPORT) || !defined(NO_PACK_SUPPORT) || \
+ !defined(NO_XZ_SUPPORT)
/* ... without an errno. */
void
maybe_errx(const char *fmt, ...)
@@ -1122,6 +1134,11 @@ file_gettype(u_char *buf)
return FT_PACK;
else
#endif
+#ifndef NO_XZ_SUPPORT
+ if (memcmp(buf, XZ_MAGIC, 4) == 0) /* XXX: We only have 4 bytes */
+ return FT_XZ;
+ else
+#endif
return FT_UNKNOWN;
}
@@ -1369,7 +1386,6 @@ file_uncompress(char *file, char *outfile, size_t outsize)
}
method = file_gettype(header1);
-
#ifndef SMALL
if (fflag == 0 && method == FT_UNKNOWN) {
maybe_warnx("%s: not in gzip format", file);
@@ -1447,9 +1463,9 @@ file_uncompress(char *file, char *outfile, size_t outsize)
} else
zfd = STDOUT_FILENO;
+ switch (method) {
#ifndef NO_BZIP2_SUPPORT
- if (method == FT_BZIP2) {
-
+ case FT_BZIP2:
/* XXX */
if (lflag) {
maybe_warnx("no -l with bzip2 files");
@@ -1457,11 +1473,11 @@ file_uncompress(char *file, char *outfile, size_t outsize)
}
size = unbzip2(fd, zfd, NULL, 0, NULL);
- } else
+ break;
#endif
#ifndef NO_COMPRESS_SUPPORT
- if (method == FT_Z) {
+ case FT_Z: {
FILE *in, *out;
/* XXX */
@@ -1494,30 +1510,42 @@ file_uncompress(char *file, char *outfile, size_t outsize)
unlink(outfile);
goto lose;
}
- } else
+ break;
+ }
#endif
#ifndef NO_PACK_SUPPORT
- if (method == FT_PACK) {
+ case FT_PACK:
if (lflag) {
maybe_warnx("no -l with packed files");
goto lose;
}
size = unpack(fd, zfd, NULL, 0, NULL);
- } else
+ break;
+#endif
+
+#ifndef NO_XZ_SUPPORT
+ case FT_XZ:
+ if (lflag) {
+ maybe_warnx("no -l with xz files");
+ goto lose;
+ }
+
+ size = unxz(fd, zfd, NULL, 0, NULL);
+ break;
#endif
#ifndef SMALL
- if (method == FT_UNKNOWN) {
+ case FT_UNKNOWN:
if (lflag) {
maybe_warnx("no -l for unknown filetypes");
goto lose;
}
size = cat_fd(NULL, 0, NULL, fd);
- } else
+ break;
#endif
- {
+ default:
if (lflag) {
print_list(fd, isb.st_size, outfile, isb.st_mtime);
close(fd);
@@ -1525,6 +1553,7 @@ file_uncompress(char *file, char *outfile, size_t outsize)
}
size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
+ break;
}
if (close(fd) != 0)
@@ -1697,7 +1726,8 @@ handle_stdin(void)
return;
}
- usize = zuncompress(in, stdout, (char *)header1, sizeof header1, &gsize);
+ usize = zuncompress(in, stdout, (char *)header1,
+ sizeof header1, &gsize);
fclose(in);
break;
#endif
@@ -1707,6 +1737,12 @@ handle_stdin(void)
(char *)header1, sizeof header1, &gsize);
break;
#endif
+#ifndef NO_XZ_SUPPORT
+ case FT_XZ:
+ usize = unxz(STDIN_FILENO, STDOUT_FILENO,
+ (char *)header1, sizeof header1, &gsize);
+ break;
+#endif
}
#ifndef SMALL
@@ -2074,7 +2110,7 @@ static void
display_license(void)
{
- fprintf(stderr, "%s (based on NetBSD gzip 20091011)\n", gzip_version);
+ fprintf(stderr, "%s (based on NetBSD gzip 20111009)\n", gzip_version);
fprintf(stderr, "%s\n", gzip_copyright);
exit(0);
}
@@ -2098,6 +2134,9 @@ display_version(void)
#ifndef NO_PACK_SUPPORT
#include "unpack.c"
#endif
+#ifndef NO_XZ_SUPPORT
+#include "unxz.c"
+#endif
static ssize_t
read_retry(int fd, void *buf, size_t sz)
OpenPOWER on IntegriCloud