Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/gzip factor out filetype checking code
details: https://anonhg.NetBSD.org/src/rev/712fb4cbe45a
branches: trunk
changeset: 565130:712fb4cbe45a
user: mrg <mrg%NetBSD.org@localhost>
date: Tue Mar 30 11:42:04 2004 +0000
description:
factor out filetype checking code
diffstat:
usr.bin/gzip/gzip.c | 59 +++++++++++++++++++++++++++++++---------------------
1 files changed, 35 insertions(+), 24 deletions(-)
diffs (100 lines):
diff -r 700001d9997c -r 712fb4cbe45a usr.bin/gzip/gzip.c
--- a/usr.bin/gzip/gzip.c Tue Mar 30 11:12:32 2004 +0000
+++ b/usr.bin/gzip/gzip.c Tue Mar 30 11:42:04 2004 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: gzip.c,v 1.31 2004/03/30 09:42:58 mrg Exp $ */
+/* $NetBSD: gzip.c,v 1.32 2004/03/30 11:42:04 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.31 2004/03/30 09:42:58 mrg Exp $");
+__RCSID("$NetBSD: gzip.c,v 1.32 2004/03/30 11:42:04 mrg Exp $");
#endif /* not lint */
/*
@@ -133,6 +133,7 @@
static void maybe_errx(int rv, const char *fmt, ...);
static void maybe_warn(const char *fmt, ...);
static void maybe_warnx(const char *fmt, ...);
+static enum filetype file_gettype(u_char *);
static void gz_compress(FILE *, gzFile);
static off_t gz_uncompress(gzFile, FILE *);
static off_t file_compress(char *);
@@ -537,6 +538,29 @@
}
#endif
+/* what sort of file is this? */
+static enum filetype
+file_gettype(u_char *buf)
+{
+
+ if (buf[0] == GZIP_MAGIC0 &&
+ (buf[1] == GZIP_MAGIC1 || buf[1] == GZIP_OMAGIC1))
+ return FT_GZIP;
+ else
+#ifndef NO_BZIP2_SUPPORT
+ if (memcmp(buf, BZIP2_MAGIC, 3) == 0 &&
+ buf[3] >= '0' && buf[3] <= '9')
+ return FT_BZIP2;
+ else
+#endif
+#ifndef NO_COMPRESS_SUPPORT
+ if (memcmp(buf, Z_MAGIC, 2) == 0)
+ return FT_Z;
+ else
+#endif
+ return FT_UNKNOWN;
+}
+
/*
* compress the given file: create a corresponding .gz file and remove the
* original.
@@ -664,34 +688,21 @@
maybe_err(1, "can't read %s", file);
}
- if (header1[0] == GZIP_MAGIC0 &&
- (header1[1] == GZIP_MAGIC1 || header1[1] == GZIP_OMAGIC1))
- method = FT_GZIP;
- else
+ method = file_gettype(header1);
-#ifndef NO_BZIP2_SUPPORT
- if (memcmp(header1, BZIP2_MAGIC, 3) == 0 &&
- header1[3] >= '0' && header1[3] <= '9') {
-# ifndef SMALL
- if (Sflag == NULL)
+#ifndef SMALL
+ if (Sflag == NULL) {
+# ifndef NO_BZIP2_SUPPORT
+ if (method == FT_BZIP2)
suffix = BZ2_SUFFIX;
- method = FT_BZIP2;
+ else
# endif
- } else
-#endif
-
-#ifndef NO_COMPRESS_SUPPORT
- if (memcmp(header1, Z_MAGIC, 2) == 0) {
-# ifndef SMALL
- if (Sflag == NULL)
+# ifndef NO_COMPRESS_SUPPORT
+ if (method == FT_Z)
suffix = Z_SUFFIX;
# endif
- method = FT_Z;
- } else
-#endif
- method = FT_UNKNOWN;
+ }
-#ifndef SMALL
if (fflag == 0 && method == FT_UNKNOWN)
maybe_errx(1, "%s: not in gzip format", file);
#endif
Home |
Main Index |
Thread Index |
Old Index