summaryrefslogtreecommitdiff
path: root/test/minigzip.c
diff options
context:
space:
mode:
authoralk3pInjection <webmaster@raspii.tech>2023-04-20 00:08:54 +0800
committeralk3pInjection <webmaster@raspii.tech>2023-04-20 00:08:54 +0800
commit004b98220a30de0d1956a8149d8bc6ec356667da (patch)
tree1eaee2603984d7ab4524be68b57ce0a2b2b72118 /test/minigzip.c
parent2ca0d0b38b60e8d6d49a8959bf674a79e7d16f41 (diff)
parenta583e215afa2356e23b418efa871a1cc4348702a (diff)
Merge tag '2.0.7' into tachibanatachibana-mr1tachibana
Change-Id: I7b03d60d67d184c21ff7437a35062077666951e9
Diffstat (limited to 'test/minigzip.c')
-rw-r--r--test/minigzip.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/test/minigzip.c b/test/minigzip.c
index 29729f3..d1b9e68 100644
--- a/test/minigzip.c
+++ b/test/minigzip.c
@@ -64,6 +64,7 @@ extern int unlink (const char *);
static char *prog;
void error (const char *msg);
+void gz_fatal (gzFile file);
void gz_compress (FILE *in, gzFile out);
#ifdef USE_MMAP
int gz_compress_mmap (FILE *in, gzFile out);
@@ -82,13 +83,23 @@ void error(const char *msg) {
}
/* ===========================================================================
+ * Display last error message of gzFile, close it and exit
+ */
+
+void gz_fatal(gzFile file) {
+ int err;
+ fprintf(stderr, "%s: %s\n", prog, PREFIX(gzerror)(file, &err));
+ PREFIX(gzclose)(file);
+ exit(1);
+}
+
+/* ===========================================================================
* Compress input to output then close both files.
*/
void gz_compress(FILE *in, gzFile out) {
char *buf;
int len;
- int err;
#ifdef USE_MMAP
/* Try first compressing with mmap. If mmap fails (minigzip used in a
@@ -111,7 +122,7 @@ void gz_compress(FILE *in, gzFile out) {
}
if (len == 0) break;
- if (PREFIX(gzwrite)(out, buf, (unsigned)len) != len) error(PREFIX(gzerror)(out, &err));
+ if (PREFIX(gzwrite)(out, buf, (unsigned)len) != len) gz_fatal(out);
}
free(buf);
fclose(in);
@@ -125,7 +136,6 @@ void gz_compress(FILE *in, gzFile out) {
*/
int gz_compress_mmap(FILE *in, gzFile out) {
int len;
- int err;
int ifd = fileno(in);
char *buf; /* mmap'ed buffer for the entire input file */
off_t buf_len; /* length of the input file */
@@ -143,7 +153,7 @@ int gz_compress_mmap(FILE *in, gzFile out) {
/* Compress the whole file at once: */
len = PREFIX(gzwrite)(out, buf, (unsigned)buf_len);
- if (len != (int)buf_len) error(PREFIX(gzerror)(out, &err));
+ if (len != (int)buf_len) gz_fatal(out);
munmap(buf, buf_len);
fclose(in);
@@ -158,7 +168,6 @@ int gz_compress_mmap(FILE *in, gzFile out) {
void gz_uncompress(gzFile in, FILE *out) {
char *buf = (char *)malloc(BUFLENW);
int len;
- int err;
if (buf == NULL) error("out of memory");
@@ -166,7 +175,7 @@ void gz_uncompress(gzFile in, FILE *out) {
len = PREFIX(gzread)(in, buf, BUFLENW);
if (len < 0) {
free(buf);
- error(PREFIX(gzerror)(in, &err));
+ gz_fatal(in);
}
if (len == 0) break;