comparison zlib/examples/gzappend.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents ae3a4bfb450b
children
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* gzappend -- command to append to a gzip file 1 /* gzappend -- command to append to a gzip file
2 2
3 Copyright (C) 2003 Mark Adler, all rights reserved 3 Copyright (C) 2003, 2012 Mark Adler, all rights reserved
4 version 1.1, 4 Nov 2003 4 version 1.2, 11 Oct 2012
5 5
6 This software is provided 'as-is', without any express or implied 6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the author be held liable for any damages 7 warranty. In no event will the author be held liable for any damages
8 arising from the use of this software. 8 arising from the use of this software.
9 9
37 * - Finish off gzip file in gztack() 37 * - Finish off gzip file in gztack()
38 * - Use deflatePrime() instead of adding empty blocks 38 * - Use deflatePrime() instead of adding empty blocks
39 * - Keep gzip file clean on appended file read errors 39 * - Keep gzip file clean on appended file read errors
40 * - Use in-place rotate instead of auxiliary buffer 40 * - Use in-place rotate instead of auxiliary buffer
41 * (Why you ask? Because it was fun to write!) 41 * (Why you ask? Because it was fun to write!)
42 * 1.2 11 Oct 2012 - Fix for proper z_const usage
43 * - Check for input buffer malloc failure
42 */ 44 */
43 45
44 /* 46 /*
45 gzappend takes a gzip file and appends to it, compressing files from the 47 gzappend takes a gzip file and appends to it, compressing files from the
46 command line or data from stdin. The gzip file is written to directly, to 48 command line or data from stdin. The gzip file is written to directly, to
168 typedef struct { 170 typedef struct {
169 int fd; /* file descriptor */ 171 int fd; /* file descriptor */
170 int size; /* 1 << size is bytes in buf */ 172 int size; /* 1 << size is bytes in buf */
171 unsigned left; /* bytes available at next */ 173 unsigned left; /* bytes available at next */
172 unsigned char *buf; /* buffer */ 174 unsigned char *buf; /* buffer */
173 unsigned char *next; /* next byte in buffer */ 175 z_const unsigned char *next; /* next byte in buffer */
174 char *name; /* file name for error messages */ 176 char *name; /* file name for error messages */
175 } file; 177 } file;
176 178
177 /* reload buffer */ 179 /* reload buffer */
178 local int readin(file *in) 180 local int readin(file *in)
397 fprintf(stderr, "gzappend warning: %s not found, skipping ...\n", 399 fprintf(stderr, "gzappend warning: %s not found, skipping ...\n",
398 name); 400 name);
399 } 401 }
400 402
401 /* allocate buffers */ 403 /* allocate buffers */
402 in = fd == -1 ? NULL : malloc(CHUNK); 404 in = malloc(CHUNK);
403 out = malloc(CHUNK); 405 out = malloc(CHUNK);
404 if (out == NULL) bye("out of memory", ""); 406 if (in == NULL || out == NULL) bye("out of memory", "");
405 407
406 /* compress input file and append to gzip file */ 408 /* compress input file and append to gzip file */
407 do { 409 do {
408 /* get more input */ 410 /* get more input */
409 len = fd == -1 ? 0 : read(fd, in, CHUNK); 411 len = read(fd, in, CHUNK);
410 if (len == -1) { 412 if (len == -1) {
411 fprintf(stderr, 413 fprintf(stderr,
412 "gzappend warning: error reading %s, skipping rest ...\n", 414 "gzappend warning: error reading %s, skipping rest ...\n",
413 name); 415 name);
414 len = 0; 416 len = 0;
451 close(gd); 453 close(gd);
452 } 454 }
453 455
454 /* clean up and return */ 456 /* clean up and return */
455 free(out); 457 free(out);
456 if (in != NULL) free(in); 458 free(in);
457 if (fd > 0) close(fd); 459 if (fd > 0) close(fd);
458 } 460 }
459 461
460 /* process the compression level option if present, scan the gzip file, and 462 /* process the compression level option if present, scan the gzip file, and
461 append the specified files, or append the data from stdin if no other file 463 append the specified files, or append the data from stdin if no other file
465 { 467 {
466 int gd, level; 468 int gd, level;
467 z_stream strm; 469 z_stream strm;
468 470
469 /* ignore command name */ 471 /* ignore command name */
470 argv++; 472 argc--; argv++;
471 473
472 /* provide usage if no arguments */ 474 /* provide usage if no arguments */
473 if (*argv == NULL) { 475 if (*argv == NULL) {
474 printf("gzappend 1.1 (4 Nov 2003) Copyright (C) 2003 Mark Adler\n"); 476 printf(
477 "gzappend 1.2 (11 Oct 2012) Copyright (C) 2003, 2012 Mark Adler\n"
478 );
475 printf( 479 printf(
476 "usage: gzappend [-level] file.gz [ addthis [ andthis ... ]]\n"); 480 "usage: gzappend [-level] file.gz [ addthis [ andthis ... ]]\n");
477 return 0; 481 return 0;
478 } 482 }
479 483