diff options
Diffstat (limited to 'test/minigzip.c')
-rw-r--r-- | test/minigzip.c | 172 |
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 | ||
152 | void *myalloc OF((void *, unsigned, unsigned)); | 152 | void *myalloc(void *q, unsigned n, unsigned m) { |
153 | void myfree OF((void *, void *)); | ||
154 | |||
155 | void *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 | ||
163 | void myfree(q, p) | 157 | void 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 | ||
178 | gzFile gzopen OF((const char *, const char *)); | 170 | gzFile gz_open(const char *path, int fd, const char *mode) { |
179 | gzFile gzdopen OF((int, const char *)); | ||
180 | gzFile gz_open OF((const char *, int, const char *)); | ||
181 | |||
182 | gzFile gzopen(path, mode) | ||
183 | const char *path; | ||
184 | const char *mode; | ||
185 | { | ||
186 | return gz_open(path, -1, mode); | ||
187 | } | ||
188 | |||
189 | gzFile gzdopen(fd, mode) | ||
190 | int fd; | ||
191 | const char *mode; | ||
192 | { | ||
193 | return gz_open(NULL, fd, mode); | ||
194 | } | ||
195 | |||
196 | gzFile 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 | ||
234 | int gzwrite OF((gzFile, const void *, unsigned)); | 204 | gzFile gzopen(const char *path, const char *mode) { |
205 | return gz_open(path, -1, mode); | ||
206 | } | ||
235 | 207 | ||
236 | int gzwrite(gz, buf, len) | 208 | gzFile 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 | { | 212 | int 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 | ||
258 | int gzread OF((gzFile, void *, unsigned)); | 230 | int gzread(gzFile gz, void *buf, unsigned len) { |
259 | |||
260 | int 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 | ||
295 | int gzclose OF((gzFile)); | 261 | int gzclose(gzFile gz) { |
296 | |||
297 | int 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 | ||
324 | const char *gzerror OF((gzFile, int *)); | 286 | const char *gzerror(gzFile gz, int *err) { |
325 | |||
326 | const 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 | ||
336 | static char *prog; | 293 | static char *prog; |
337 | 294 | ||
338 | void error OF((const char *msg)); | ||
339 | void gz_compress OF((FILE *in, gzFile out)); | ||
340 | #ifdef USE_MMAP | ||
341 | int gz_compress_mmap OF((FILE *in, gzFile out)); | ||
342 | #endif | ||
343 | void gz_uncompress OF((gzFile in, FILE *out)); | ||
344 | void file_compress OF((char *file, char *mode)); | ||
345 | void file_uncompress OF((char *file)); | ||
346 | int main OF((int argc, char *argv[])); | ||
347 | |||
348 | /* =========================================================================== | 295 | /* =========================================================================== |
349 | * Display error message and exit | 296 | * Display error message and exit |
350 | */ | 297 | */ |
351 | void error(msg) | 298 | void 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 | |||
362 | void 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 | */ |
395 | int gz_compress_mmap(in, out) | 308 | int 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 | |||
341 | void 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 | */ |
430 | void gz_uncompress(in, out) | 369 | void 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 | */ |
457 | void file_compress(file, mode) | 393 | void 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 | */ |
496 | void file_uncompress(file) | 429 | void 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 | ||
556 | int main(argc, argv) | 487 | int 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; |