1/* $NetBSD: zcat.c,v 1.3 1998/11/12 16:49:47 thorpej Exp $ */ 2 3/* mini zcat.c -- a minimal zcat using the zlib compression library 4 * Copyright (C) 1995-1996 Jean-loup Gailly. 5 * For conditions of distribution and use, see copyright notice in zlib.h 6 */ 7 8/* 9 * Credits, History: 10 * This program is a reduced version of the minigzip.c 11 * program originally written by Jean-loup Gailly. 12 * This reduction is the work of Gordon Ross. 13 */ 14 15#include <stdio.h> 16#include <string.h> 17#include <stdlib.h> 18 19#include "zlib.h" 20 21#define BUFLEN 4096 22 23char *prog; 24 25void error(const char *msg); 26void gz_uncompress(gzFile in, FILE *out); 27int main(int argc, char *argv[]); 28 29/* =========================================================================== 30 * Display error message and exit 31 */ 32void error(const char *msg) 33{ 34 35 fprintf(stderr, "%s: %s\n", prog, msg); 36 exit(EXIT_SUCCESS); 37} 38 39/* =========================================================================== 40 * Uncompress input to output then close both files. 41 */ 42void gz_uncompress(gzFile in, FILE *out) 43{ 44 char buf[BUFLEN]; 45 int len; 46 int err; 47 48 for (;;) { 49 len = gzread(in, buf, sizeof(buf)); 50 if (len < 0) 51 error (gzerror(in, &err)); 52 if (len == 0) 53 break; 54 55 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) { 56 error("failed fwrite"); 57 } 58 } 59 if (fclose(out)) 60 error("failed fclose"); 61 62 if (gzclose(in) != Z_OK) 63 error("failed gzclose"); 64} 65 66 67/* =========================================================================== 68 * Usage: zcat [files...] 69 */ 70 71int main(int argc, char *argv[]) 72{ 73 gzFile zfp; 74 75 /* save program name and skip */ 76 prog = argv[0]; 77 argc--, argv++; 78 79 /* ignore any switches */ 80 while (*argv && (**argv == '-')) { 81 argc--, argv++; 82 } 83 84 if (argc == 0) { 85 zfp = gzdopen(fileno(stdin), "rb"); 86 if (zfp == NULL) 87 error("can't gzdopen stdin"); 88 gz_uncompress(zfp, stdout); 89 return 0; 90 } 91 92 do { 93 /* file_uncompress(*argv); */ 94 zfp = gzopen(*argv, "rb"); 95 if (zfp == NULL) { 96 fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv); 97 exit(EXIT_FAILURE); 98 } 99 gz_uncompress(zfp, stdout); 100 } while (argv++, --argc); 101 return 0; /* to avoid warning */ 102} 103 104 105/* 106 * XXX: hacks to keep gzio.c from pulling in deflate stuff 107 */ 108 109int deflateInit2_(z_streamp strm, int level, int method, 110 int windowBits, int memLevel, int strategy, 111 const char *version, int stream_size) 112{ 113 114 return -1; 115} 116 117int deflate(z_streamp strm, int flush) 118{ 119 120 return -1; 121} 122 123int deflateEnd(z_streamp strm) 124{ 125 126 return -1; 127} 128 129int deflateParams(z_streamp strm, int level, int strategy) 130{ 131 132 return Z_STREAM_ERROR; 133} 134