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
| | #include "cache.h"
#include "builtin.h"
#include "parse-options.h"
static const char * const gzip_usage[] = {
N_("git gzip [-NUM]"),
NULL
};
static int level_callback(const struct option *opt, const char *arg, int unset)
{
int *levelp = opt->value;
int value;
const char *endp;
if (unset)
BUG("switch -NUM cannot be negated");
value = strtol(arg, (char **)&endp, 10);
if (*endp)
BUG("switch -NUM cannot be non-numeric");
*levelp = value;
return 0;
}
#define BUFFERSIZE (64 * 1024)
int cmd_gzip(int argc, const char **argv, const char *prefix)
{
gzFile gz;
int level = Z_DEFAULT_COMPRESSION;
struct option options[] = {
OPT_NUMBER_CALLBACK(&level, N_("compression level"),
level_callback),
OPT_END()
};
argc = parse_options(argc, argv, prefix, options, gzip_usage, 0);
if (argc > 0)
usage_with_options(gzip_usage, options);
gz = gzdopen(1, "wb");
if (!gz)
die(_("unable to gzdopen stdout"));
if (gzsetparams(gz, level, Z_DEFAULT_STRATEGY) != Z_OK)
die(_("unable to set compression level %d"), level);
for (;;) {
char buf[BUFFERSIZE];
ssize_t read_bytes = xread(0, buf, sizeof(buf));
if (read_bytes < 0)
die_errno(_("unable to read from stdin"));
if (read_bytes == 0)
break;
if (gzwrite(gz, buf, read_bytes) != read_bytes)
die(_("gzwrite failed"));
}
if (gzclose(gz) != Z_OK)
die(_("gzclose failed"));
return 0;
}
|