summaryrefslogtreecommitdiff
path: root/test/minigzip.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/minigzip.c')
-rw-r--r--test/minigzip.c172
1 files changed, 50 insertions, 122 deletions
diff --git a/test/minigzip.c b/test/minigzip.c
index a649d2b..8a21ddf 100644
--- a/test/minigzip.c
+++ b/test/minigzip.c
@@ -59,7 +59,7 @@
59 59
60#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE) 60#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
61#ifndef WIN32 /* unlink already in stdio.h for WIN32 */ 61#ifndef WIN32 /* unlink already in stdio.h for WIN32 */
62 extern int unlink OF((const char *)); 62 extern int unlink(const char *);
63#endif 63#endif
64#endif 64#endif
65 65
@@ -149,20 +149,12 @@ static void pwinerror (s)
149# include <unistd.h> /* for unlink() */ 149# include <unistd.h> /* for unlink() */
150#endif 150#endif
151 151
152void *myalloc OF((void *, unsigned, unsigned)); 152void *myalloc(void *q, unsigned n, unsigned m) {
153void myfree OF((void *, void *));
154
155void *myalloc(q, n, m)
156 void *q;
157 unsigned n, m;
158{
159 (void)q; 153 (void)q;
160 return calloc(n, m); 154 return calloc(n, m);
161} 155}
162 156
163void myfree(q, p) 157void myfree(void *q, void *p) {
164 void *q, *p;
165{
166 (void)q; 158 (void)q;
167 free(p); 159 free(p);
168} 160}
@@ -175,29 +167,7 @@ typedef struct gzFile_s {
175 z_stream strm; 167 z_stream strm;
176} *gzFile; 168} *gzFile;
177 169
178gzFile gzopen OF((const char *, const char *)); 170gzFile gz_open(const char *path, int fd, const char *mode) {
179gzFile gzdopen OF((int, const char *));
180gzFile gz_open OF((const char *, int, const char *));
181
182gzFile gzopen(path, mode)
183const char *path;
184const char *mode;
185{
186 return gz_open(path, -1, mode);
187}
188
189gzFile gzdopen(fd, mode)
190int fd;
191const char *mode;
192{
193 return gz_open(NULL, fd, mode);
194}
195
196gzFile gz_open(path, fd, mode)
197 const char *path;
198 int fd;
199 const char *mode;
200{
201 gzFile gz; 171 gzFile gz;
202 int ret; 172 int ret;
203 173
@@ -231,13 +201,15 @@ gzFile gz_open(path, fd, mode)
231 return gz; 201 return gz;
232} 202}
233 203
234int gzwrite OF((gzFile, const void *, unsigned)); 204gzFile gzopen(const char *path, const char *mode) {
205 return gz_open(path, -1, mode);
206}
235 207
236int gzwrite(gz, buf, len) 208gzFile gzdopen(int fd, const char *mode) {
237 gzFile gz; 209 return gz_open(NULL, fd, mode);
238 const void *buf; 210}
239 unsigned len; 211
240{ 212int gzwrite(gzFile gz, const void *buf, unsigned len) {
241 z_stream *strm; 213 z_stream *strm;
242 unsigned char out[BUFLEN]; 214 unsigned char out[BUFLEN];
243 215
@@ -255,13 +227,7 @@ int gzwrite(gz, buf, len)
255 return len; 227 return len;
256} 228}
257 229
258int gzread OF((gzFile, void *, unsigned)); 230int gzread(gzFile gz, void *buf, unsigned len) {
259
260int gzread(gz, buf, len)
261 gzFile gz;
262 void *buf;
263 unsigned len;
264{
265 int ret; 231 int ret;
266 unsigned got; 232 unsigned got;
267 unsigned char in[1]; 233 unsigned char in[1];
@@ -292,11 +258,7 @@ int gzread(gz, buf, len)
292 return len - strm->avail_out; 258 return len - strm->avail_out;
293} 259}
294 260
295int gzclose OF((gzFile)); 261int gzclose(gzFile gz) {
296
297int gzclose(gz)
298 gzFile gz;
299{
300 z_stream *strm; 262 z_stream *strm;
301 unsigned char out[BUFLEN]; 263 unsigned char out[BUFLEN];
302 264
@@ -321,12 +283,7 @@ int gzclose(gz)
321 return Z_OK; 283 return Z_OK;
322} 284}
323 285
324const char *gzerror OF((gzFile, int *)); 286const char *gzerror(gzFile gz, int *err) {
325
326const char *gzerror(gz, err)
327 gzFile gz;
328 int *err;
329{
330 *err = gz->err; 287 *err = gz->err;
331 return gz->msg; 288 return gz->msg;
332} 289}
@@ -335,67 +292,20 @@ const char *gzerror(gz, err)
335 292
336static char *prog; 293static char *prog;
337 294
338void error OF((const char *msg));
339void gz_compress OF((FILE *in, gzFile out));
340#ifdef USE_MMAP
341int gz_compress_mmap OF((FILE *in, gzFile out));
342#endif
343void gz_uncompress OF((gzFile in, FILE *out));
344void file_compress OF((char *file, char *mode));
345void file_uncompress OF((char *file));
346int main OF((int argc, char *argv[]));
347
348/* =========================================================================== 295/* ===========================================================================
349 * Display error message and exit 296 * Display error message and exit
350 */ 297 */
351void error(msg) 298void error(const char *msg) {
352 const char *msg;
353{
354 fprintf(stderr, "%s: %s\n", prog, msg); 299 fprintf(stderr, "%s: %s\n", prog, msg);
355 exit(1); 300 exit(1);
356} 301}
357 302
358/* ===========================================================================
359 * Compress input to output then close both files.
360 */
361
362void gz_compress(in, out)
363 FILE *in;
364 gzFile out;
365{
366 local char buf[BUFLEN];
367 int len;
368 int err;
369
370#ifdef USE_MMAP
371 /* Try first compressing with mmap. If mmap fails (minigzip used in a
372 * pipe), use the normal fread loop.
373 */
374 if (gz_compress_mmap(in, out) == Z_OK) return;
375#endif
376 for (;;) {
377 len = (int)fread(buf, 1, sizeof(buf), in);
378 if (ferror(in)) {
379 perror("fread");
380 exit(1);
381 }
382 if (len == 0) break;
383
384 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
385 }
386 fclose(in);
387 if (gzclose(out) != Z_OK) error("failed gzclose");
388}
389
390#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */ 303#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
391 304
392/* Try compressing the input file at once using mmap. Return Z_OK if 305/* Try compressing the input file at once using mmap. Return Z_OK if
393 * if success, Z_ERRNO otherwise. 306 * if success, Z_ERRNO otherwise.
394 */ 307 */
395int gz_compress_mmap(in, out) 308int gz_compress_mmap(FILE *in, gzFile out) {
396 FILE *in;
397 gzFile out;
398{
399 int len; 309 int len;
400 int err; 310 int err;
401 int ifd = fileno(in); 311 int ifd = fileno(in);
@@ -425,12 +335,38 @@ int gz_compress_mmap(in, out)
425#endif /* USE_MMAP */ 335#endif /* USE_MMAP */
426 336
427/* =========================================================================== 337/* ===========================================================================
338 * Compress input to output then close both files.
339 */
340
341void gz_compress(FILE *in, gzFile out) {
342 local char buf[BUFLEN];
343 int len;
344 int err;
345
346#ifdef USE_MMAP
347 /* Try first compressing with mmap. If mmap fails (minigzip used in a
348 * pipe), use the normal fread loop.
349 */
350 if (gz_compress_mmap(in, out) == Z_OK) return;
351#endif
352 for (;;) {
353 len = (int)fread(buf, 1, sizeof(buf), in);
354 if (ferror(in)) {
355 perror("fread");
356 exit(1);
357 }
358 if (len == 0) break;
359
360 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
361 }
362 fclose(in);
363 if (gzclose(out) != Z_OK) error("failed gzclose");
364}
365
366/* ===========================================================================
428 * Uncompress input to output then close both files. 367 * Uncompress input to output then close both files.
429 */ 368 */
430void gz_uncompress(in, out) 369void gz_uncompress(gzFile in, FILE *out) {
431 gzFile in;
432 FILE *out;
433{
434 local char buf[BUFLEN]; 370 local char buf[BUFLEN];
435 int len; 371 int len;
436 int err; 372 int err;
@@ -454,10 +390,7 @@ void gz_uncompress(in, out)
454 * Compress the given file: create a corresponding .gz file and remove the 390 * Compress the given file: create a corresponding .gz file and remove the
455 * original. 391 * original.
456 */ 392 */
457void file_compress(file, mode) 393void file_compress(char *file, char *mode) {
458 char *file;
459 char *mode;
460{
461 local char outfile[MAX_NAME_LEN]; 394 local char outfile[MAX_NAME_LEN];
462 FILE *in; 395 FILE *in;
463 gzFile out; 396 gzFile out;
@@ -493,9 +426,7 @@ void file_compress(file, mode)
493/* =========================================================================== 426/* ===========================================================================
494 * Uncompress the given file and remove the original. 427 * Uncompress the given file and remove the original.
495 */ 428 */
496void file_uncompress(file) 429void file_uncompress(char *file) {
497 char *file;
498{
499 local char buf[MAX_NAME_LEN]; 430 local char buf[MAX_NAME_LEN];
500 char *infile, *outfile; 431 char *infile, *outfile;
501 FILE *out; 432 FILE *out;
@@ -553,10 +484,7 @@ void file_uncompress(file)
553 * -1 to -9 : compression level 484 * -1 to -9 : compression level
554 */ 485 */
555 486
556int main(argc, argv) 487int main(int argc, char *argv[]) {
557 int argc;
558 char *argv[];
559{
560 int copyout = 0; 488 int copyout = 0;
561 int uncompr = 0; 489 int uncompr = 0;
562 gzFile file; 490 gzFile file;