1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/* $NetBSD: zcat.c,v 1.5 2017/01/12 01:58:03 christos Exp $ */
/* mini zcat.c -- a minimal zcat using the zlib compression library
* Copyright (C) 1995-1996 Jean-loup Gailly.
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/*
* Credits, History:
* This program is a reduced version of the minigzip.c
* program originally written by Jean-loup Gailly.
* This reduction is the work of Gordon Ross.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "zlib.h"
#define BUFLEN 4096
char *prog;
static void error(const char *, ...) __printflike(1, 2);
static void gz_uncompress(gzFile, int);
/* ===========================================================================
* Display error message and exit
*/
static void
error(const char *fmt, ...)
{
char buf[1024];
va_list ap;
int l;
l = snprintf_ss(buf, sizeof(buf), "%s: ", prog);
write(STDERR_FILENO, buf, l);
va_start(ap, fmt);
l = vsnprintf_ss(buf, sizeof(buf), fmt, ap);
va_end(ap);
write(STDERR_FILENO, buf, l);
_exit(EXIT_SUCCESS);
}
/* ===========================================================================
* Uncompress input to output then close both files.
*/
static void
gz_uncompress(gzFile in, int out)
{
char buf[BUFLEN];
int len;
int err;
for (;;) {
len = gzread(in, buf, sizeof(buf));
if (len < 0)
error ("%s", gzerror(in, &err));
if (len == 0)
break;
if ((int)write(out, buf, (size_t)len) != len) {
error("failed fwrite");
}
}
if (close(out))
error("failed fclose");
if (gzclose(in) != Z_OK)
error("failed gzclose");
}
/* ===========================================================================
* Usage: zcat [files...]
*/
int
main(int argc, char *argv[])
{
gzFile zfp;
/* save program name and skip */
prog = argv[0];
argc--, argv++;
/* ignore any switches */
while (*argv && (**argv == '-')) {
argc--, argv++;
}
if (argc == 0) {
zfp = gzdopen(STDIN_FILENO, "rb");
if (zfp == NULL)
error("can't gzdopen stdin");
gz_uncompress(zfp, STDOUT_FILENO);
return 0;
}
do {
/* file_uncompress(*argv); */
zfp = gzopen(*argv, "rb");
if (zfp == NULL) {
error("can't gzopen `%s'", *argv);
_exit(EXIT_FAILURE);
}
gz_uncompress(zfp, STDOUT_FILENO);
} while (argv++, --argc);
return 0; /* to avoid warning */
}
/*
* XXX: hacks to keep gzio.c from pulling in deflate stuff
*/
int deflateInit2_(z_streamp strm, int level, int method,
int windowBits, int memLevel, int strategy,
const char *version, int stream_size)
{
return -1;
}
int deflate(z_streamp strm, int flush)
{
return -1;
}
int deflateEnd(z_streamp strm)
{
return -1;
}
int deflateParams(z_streamp strm, int level, int strategy)
{
return Z_STREAM_ERROR;
}
|