xref: /netbsd/distrib/utils/zcat/zcat.c (revision bf9ec67e)
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 
23 char *prog;
24 
25 void error           __P((const char *msg));
26 void gz_uncompress   __P((gzFile in, FILE   *out));
27 int  main            __P((int argc, char *argv[]));
28 
29 /* ===========================================================================
30  * Display error message and exit
31  */
32 void error(msg)
33     const char *msg;
34 {
35     fprintf(stderr, "%s: %s\n", prog, msg);
36     exit(1);
37 }
38 
39 /* ===========================================================================
40  * Uncompress input to output then close both files.
41  */
42 void gz_uncompress(in, out)
43     gzFile in;
44     FILE   *out;
45 {
46     char buf[BUFLEN];
47     int len;
48     int err;
49 
50     for (;;) {
51         len = gzread(in, buf, sizeof(buf));
52         if (len < 0) error (gzerror(in, &err));
53         if (len == 0) break;
54 
55         if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
56 	    error("failed fwrite");
57 	}
58     }
59     if (fclose(out)) error("failed fclose");
60 
61     if (gzclose(in) != Z_OK) error("failed gzclose");
62 }
63 
64 
65 /* ===========================================================================
66  * Usage:  zcat [files...]
67  */
68 
69 int main(argc, argv)
70     int argc;
71     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(1);
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 
109 int deflateInit2_ (z_streamp strm, int  level, int  method,
110 				    int windowBits, int memLevel, int strategy,
111 				    const char *version, int stream_size)
112 {
113 	return(-1);
114 }
115 
116 int deflate (z_streamp strm, int flush)
117 {
118 	return(-1);
119 }
120 
121 int deflateEnd (z_streamp strm)
122 {
123 	return(-1);
124 }
125 
126 int deflateParams (z_streamp strm, int level, int strategy)
127 {
128 	return(Z_STREAM_ERROR);
129 }
130