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