Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / distrib / utils / zcat / zcat.c
blob97098a44aa854baeba5c9cc26b39df43d1d4b49d
1 /* $NetBSD: zcat.c,v 1.2 1996/09/24 20:40:11 gwr Exp $ */
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 */
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.
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
19 #include "zlib.h"
21 #define BUFLEN 4096
23 char *prog;
25 void error __P((const char *msg));
26 void gz_uncompress __P((gzFile in, FILE *out));
27 int main __P((int argc, char *argv[]));
29 /* ===========================================================================
30 * Display error message and exit
32 void error(msg)
33 const char *msg;
35 fprintf(stderr, "%s: %s\n", prog, msg);
36 exit(1);
39 /* ===========================================================================
40 * Uncompress input to output then close both files.
42 void gz_uncompress(in, out)
43 gzFile in;
44 FILE *out;
46 char buf[BUFLEN];
47 int len;
48 int err;
50 for (;;) {
51 len = gzread(in, buf, sizeof(buf));
52 if (len < 0) error (gzerror(in, &err));
53 if (len == 0) break;
55 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
56 error("failed fwrite");
59 if (fclose(out)) error("failed fclose");
61 if (gzclose(in) != Z_OK) error("failed gzclose");
65 /* ===========================================================================
66 * Usage: zcat [files...]
69 int main(argc, argv)
70 int argc;
71 char *argv[];
73 gzFile zfp;
75 /* save program name and skip */
76 prog = argv[0];
77 argc--, argv++;
79 /* ignore any switches */
80 while (*argv && (**argv == '-')) {
81 argc--, argv++;
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;
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(1);
99 gz_uncompress(zfp, stdout);
100 } while (argv++, --argc);
101 return 0; /* to avoid warning */
106 * XXX: hacks to keep gzio.c from pulling in deflate stuff
109 int deflateInit2_ (z_streamp strm, int level, int method,
110 int windowBits, int memLevel, int strategy,
111 const char *version, int stream_size)
113 return(-1);
116 int deflate (z_streamp strm, int flush)
118 return(-1);
121 int deflateEnd (z_streamp strm)
123 return(-1);
126 int deflateParams (z_streamp strm, int level, int strategy)
128 return(Z_STREAM_ERROR);