gunzip: Hidden option to print progress reports, for debugging

This commit is contained in:
K. Lange 2021-11-14 11:09:45 +09:00
parent fde9fe8753
commit 2a26a37a47

View File

@ -15,11 +15,23 @@
static int to_stdout = 0; static int to_stdout = 0;
static int keep = 0; static int keep = 0;
static int progress = 0;
static off_t total_length = 0;
static size_t count = 0;
static uint8_t _get(struct inflate_context * ctx) { static uint8_t _get(struct inflate_context * ctx) {
return fgetc(ctx->input_priv); return fgetc(ctx->input_priv);
} }
static uint8_t _get_progress(struct inflate_context * ctx) {
count++;
if (!(count & 0x3FF)) {
fprintf(stderr, "\r%zu KiB / %zu KiB (%zu%%)", count / 1024, total_length / 1024, 100 * count / total_length);
fflush(stderr);
}
return fgetc(ctx->input_priv);
}
static void _write(struct inflate_context * ctx, unsigned int sym) { static void _write(struct inflate_context * ctx, unsigned int sym) {
fputc(sym, ctx->output_priv); fputc(sym, ctx->output_priv);
} }
@ -73,6 +85,15 @@ int main(int argc, char * argv[]) {
return 1; return 1;
} }
char * maybeProgress = getenv("GUNZIP_PROGRESS");
if (maybeProgress && !strcmp(maybeProgress, "1")) {
progress = 1;
fseek(f, 0, SEEK_END);
total_length = ftell(f);
fseek(f, 0, SEEK_SET);
}
struct inflate_context ctx; struct inflate_context ctx;
ctx.input_priv = f; ctx.input_priv = f;
if (to_stdout) { if (to_stdout) {
@ -83,7 +104,7 @@ int main(int argc, char * argv[]) {
ctx.output_priv = fopen(tmp,"w"); ctx.output_priv = fopen(tmp,"w");
free(tmp); free(tmp);
} }
ctx.get_input = _get; ctx.get_input = progress ? _get_progress : _get;
ctx.write_output = _write; ctx.write_output = _write;
ctx.ring = NULL; /* Use the global one */ ctx.ring = NULL; /* Use the global one */