1 /* minigzip.c -- simulate gzip using the zlib compression library
2 * Copyright (C) 1995 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
7 * minigzip is a minimal implementation of the gzip utility. This is
8 * only an example of using zlib and isn't meant to replace the
9 * full-featured gzip. No attempt is made to deal with file systems
10 * limiting names to 14 or 8+3 characters, etc... Error checking is
11 * very limited. So use minigzip only for testing; use gzip for the
12 * real thing. On MSDOS, use only on file names without extension
16 /* $Id: minigzip.c,v 1.4 1995/05/02 15:54:22 jloup Exp $ */
22 extern void exit __P((int));
24 extern int unlink __P((const char *));
33 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
35 # define SET_BINARY_MODE(file)
39 #define MAX_NAME_LEN 1024
42 /* For MSDOS and other systems with limitation on stack size. For Unix,
49 void error __P((char *msg));
50 void gz_compress __P((FILE *in, gzFile out));
51 void gz_uncompress __P((gzFile in, FILE *out));
52 void file_compress __P((char *file));
53 void file_uncompress __P((char *file));
54 void main __P((int argc, char *argv[]));
56 /* ===========================================================================
57 * Display error message and exit
62 fprintf(stderr, "%s: %s\n", prog, msg);
66 /* ===========================================================================
67 * Compress input to output then close both files.
69 void gz_compress(in, out)
73 local char buf[BUFLEN];
78 len = fread(buf, 1, sizeof(buf), in);
85 if (gzwrite(out, buf, len) != len) error(gzerror(out, &err));
88 if (gzclose(out) != Z_OK) error("failed gzclose");
91 /* ===========================================================================
92 * Uncompress input to output then close both files.
94 void gz_uncompress(in, out)
98 local char buf[BUFLEN];
103 len = gzread(in, buf, sizeof(buf));
104 if (len < 0) error (gzerror(in, &err));
107 if (fwrite(buf, 1, len, out) != (uInt)len) error("failed fwrite");
109 if (fclose(out)) error("failed fclose");
111 if (gzclose(in) != Z_OK) error("failed gzclose");
115 /* ===========================================================================
116 * Compress the given file: create a corresponding .gz file and remove the
119 void file_compress(file)
122 local char outfile[MAX_NAME_LEN];
126 strcpy(outfile, file);
127 strcat(outfile, ".gz");
129 in = fopen(file, "rb");
134 out = gzopen(outfile, "wb");
136 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
139 gz_compress(in, out);
145 /* ===========================================================================
146 * Uncompress the given file and remove the original.
148 void file_uncompress(file)
151 local char buf[MAX_NAME_LEN];
152 char *infile, *outfile;
155 int len = strlen(file);
159 if (len > 3 && strcmp(file+len-3, ".gz") == 0) {
162 outfile[len-3] = '\0';
166 strcat(infile, ".gz");
168 in = gzopen(infile, "rb");
170 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
173 out = fopen(outfile, "wb");
179 gz_uncompress(in, out);
185 /* ===========================================================================
186 * Usage: minigzip [-d] [files...]
189 void main(argc, argv)
200 uncompr = (strcmp(*argv, "-d") == 0);
206 SET_BINARY_MODE(stdin);
207 SET_BINARY_MODE(stdout);
209 file = gzdopen(fileno(stdin), "rb");
210 if (file == NULL) error("can't gzdopen stdin");
211 gz_uncompress(file, stdout);
213 file = gzdopen(fileno(stdout), "wb");
214 if (file == NULL) error("can't gzdopen stdout");
215 gz_compress(stdin, file);
220 file_uncompress(*argv);
222 file_compress(*argv);
224 } while (argv++, --argc);