summaryrefslogtreecommitdiff
path: root/minigzip.c
diff options
context:
space:
mode:
Diffstat (limited to 'minigzip.c')
-rw-r--r--minigzip.c61
1 files changed, 55 insertions, 6 deletions
diff --git a/minigzip.c b/minigzip.c
index 9d18266..84d823b 100644
--- a/minigzip.c
+++ b/minigzip.c
@@ -262,6 +262,11 @@ void file_compress(file, mode)
262 FILE *in; 262 FILE *in;
263 gzFile out; 263 gzFile out;
264 264
265 if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
266 fprintf(stderr, "%s: filename too long\n", prog);
267 exit(1);
268 }
269
265 strcpy(outfile, file); 270 strcpy(outfile, file);
266 strcat(outfile, GZ_SUFFIX); 271 strcat(outfile, GZ_SUFFIX);
267 272
@@ -291,7 +296,12 @@ void file_uncompress(file)
291 char *infile, *outfile; 296 char *infile, *outfile;
292 FILE *out; 297 FILE *out;
293 gzFile in; 298 gzFile in;
294 uInt len = (uInt)strlen(file); 299 size_t len = strlen(file);
300
301 if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
302 fprintf(stderr, "%s: filename too long\n", prog);
303 exit(1);
304 }
295 305
296 strcpy(buf, file); 306 strcpy(buf, file);
297 307
@@ -322,7 +332,8 @@ void file_uncompress(file)
322 332
323 333
324/* =========================================================================== 334/* ===========================================================================
325 * Usage: minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...] 335 * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
336 * -c : write to standard output
326 * -d : decompress 337 * -d : decompress
327 * -f : compress with Z_FILTERED 338 * -f : compress with Z_FILTERED
328 * -h : compress with Z_HUFFMAN_ONLY 339 * -h : compress with Z_HUFFMAN_ONLY
@@ -334,17 +345,30 @@ int main(argc, argv)
334 int argc; 345 int argc;
335 char *argv[]; 346 char *argv[];
336{ 347{
348 int copyout = 0;
337 int uncompr = 0; 349 int uncompr = 0;
338 gzFile file; 350 gzFile file;
339 char outmode[20]; 351 char *bname, outmode[20];
340 352
341 strcpy(outmode, "wb6 "); 353 strcpy(outmode, "wb6 ");
342 354
343 prog = argv[0]; 355 prog = argv[0];
356 bname = strrchr(argv[0], '/');
357 if (bname)
358 bname++;
359 else
360 bname = argv[0];
344 argc--, argv++; 361 argc--, argv++;
345 362
363 if (!strcmp(bname, "gunzip"))
364 uncompr = 1;
365 else if (!strcmp(bname, "zcat"))
366 copyout = uncompr = 1;
367
346 while (argc > 0) { 368 while (argc > 0) {
347 if (strcmp(*argv, "-d") == 0) 369 if (strcmp(*argv, "-c") == 0)
370 copyout = 1;
371 else if (strcmp(*argv, "-d") == 0)
348 uncompr = 1; 372 uncompr = 1;
349 else if (strcmp(*argv, "-f") == 0) 373 else if (strcmp(*argv, "-f") == 0)
350 outmode[3] = 'f'; 374 outmode[3] = 'f';
@@ -374,11 +398,36 @@ int main(argc, argv)
374 gz_compress(stdin, file); 398 gz_compress(stdin, file);
375 } 399 }
376 } else { 400 } else {
401 if (copyout) {
402 SET_BINARY_MODE(stdout);
403 }
377 do { 404 do {
378 if (uncompr) { 405 if (uncompr) {
379 file_uncompress(*argv); 406 if (copyout) {
407 file = gzopen(*argv, "rb");
408 if (file == NULL)
409 fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
410 else
411 gz_uncompress(file, stdout);
412 } else {
413 file_uncompress(*argv);
414 }
380 } else { 415 } else {
381 file_compress(*argv, outmode); 416 if (copyout) {
417 FILE * in = fopen(*argv, "rb");
418
419 if (in == NULL) {
420 perror(*argv);
421 } else {
422 file = gzdopen(fileno(stdout), outmode);
423 if (file == NULL) error("can't gzdopen stdout");
424
425 gz_compress(in, file);
426 }
427
428 } else {
429 file_compress(*argv, outmode);
430 }
382 } 431 }
383 } while (argv++, --argc); 432 } while (argv++, --argc);
384 } 433 }