1 /* minigzip.c -- simulate gzip using the zlib compression library
2 * Copyright (C) 1995-2005 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 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
28 # include <sys/types.h>
29 # include <sys/mman.h>
30 # include <sys/stat.h>
33 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
36 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
38 # define SET_BINARY_MODE(file)
42 # define unlink delete
43 # define GZ_SUFFIX "-gz"
46 # define unlink remove
47 # define GZ_SUFFIX "-gz"
48 # define fileno(file) file->__file
50 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
51 # include <unix.h> /* for fileno */
54 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
55 extern int unlink OF((const char *));
59 # define GZ_SUFFIX ".gz"
61 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
64 #define MAX_NAME_LEN 1024
68 /* Needed for systems with limitation on stack size. */
75 void error OF((const char *msg));
76 void gz_compress OF((FILE *in, gzFile out));
78 int gz_compress_mmap OF((FILE *in, gzFile out));
80 void gz_uncompress OF((gzFile in, FILE *out));
81 void file_compress OF((char *file, char *mode));
82 void file_uncompress OF((char *file));
83 int main OF((int argc, char *argv[]));
85 /* ===========================================================================
86 * Display error message and exit
91 fprintf(stderr, "%s: %s\n", prog, msg);
95 /* ===========================================================================
96 * Compress input to output then close both files.
99 void gz_compress(in, out)
103 local char buf[BUFLEN];
108 /* Try first compressing with mmap. If mmap fails (minigzip used in a
109 * pipe), use the normal fread loop.
111 if (gz_compress_mmap(in, out) == Z_OK) return;
114 len = (int)fread(buf, 1, sizeof(buf), in);
121 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
124 if (gzclose(out) != Z_OK) error("failed gzclose");
127 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
129 /* Try compressing the input file at once using mmap. Return Z_OK if
130 * if success, Z_ERRNO otherwise.
132 int gz_compress_mmap(in, out)
138 int ifd = fileno(in);
139 caddr_t buf; /* mmap'ed buffer for the entire input file */
140 off_t buf_len; /* length of the input file */
143 /* Determine the size of the file, needed for mmap: */
144 if (fstat(ifd, &sb) < 0) return Z_ERRNO;
145 buf_len = sb.st_size;
146 if (buf_len <= 0) return Z_ERRNO;
148 /* Now do the actual mmap: */
149 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
150 if (buf == (caddr_t)(-1)) return Z_ERRNO;
152 /* Compress the whole file at once: */
153 len = gzwrite(out, (char *)buf, (unsigned)buf_len);
155 if (len != (int)buf_len) error(gzerror(out, &err));
157 munmap(buf, buf_len);
159 if (gzclose(out) != Z_OK) error("failed gzclose");
162 #endif /* USE_MMAP */
164 /* ===========================================================================
165 * Uncompress input to output then close both files.
167 void gz_uncompress(in, out)
171 local char buf[BUFLEN];
176 len = gzread(in, buf, sizeof(buf));
177 if (len < 0) error (gzerror(in, &err));
180 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
181 error("failed fwrite");
184 if (fclose(out)) error("failed fclose");
186 if (gzclose(in) != Z_OK) error("failed gzclose");
190 /* ===========================================================================
191 * Compress the given file: create a corresponding .gz file and remove the
194 void file_compress(file, mode)
198 local char outfile[MAX_NAME_LEN];
202 if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
203 fprintf(stderr, "%s: filename too long\n", prog);
207 strcpy(outfile, file);
208 strcat(outfile, GZ_SUFFIX);
210 in = fopen(file, "rb");
215 out = gzopen(outfile, mode);
217 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
220 gz_compress(in, out);
226 /* ===========================================================================
227 * Uncompress the given file and remove the original.
229 void file_uncompress(file)
232 local char buf[MAX_NAME_LEN];
233 char *infile, *outfile;
236 size_t len = strlen(file);
238 if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
239 fprintf(stderr, "%s: filename too long\n", prog);
245 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
248 outfile[len-3] = '\0';
252 strcat(infile, GZ_SUFFIX);
254 in = gzopen(infile, "rb");
256 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
259 out = fopen(outfile, "wb");
265 gz_uncompress(in, out);
271 /* ===========================================================================
272 * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
273 * -c : write to standard output
275 * -f : compress with Z_FILTERED
276 * -h : compress with Z_HUFFMAN_ONLY
277 * -r : compress with Z_RLE
278 * -1 to -9 : compression level
288 char *bname, outmode[20];
290 strcpy(outmode, "wb6 ");
293 bname = strrchr(argv[0], '/');
300 if (!strcmp(bname, "gunzip"))
302 else if (!strcmp(bname, "zcat"))
303 copyout = uncompr = 1;
306 if (strcmp(*argv, "-c") == 0)
308 else if (strcmp(*argv, "-d") == 0)
310 else if (strcmp(*argv, "-f") == 0)
312 else if (strcmp(*argv, "-h") == 0)
314 else if (strcmp(*argv, "-r") == 0)
316 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
318 outmode[2] = (*argv)[1];
323 if (outmode[3] == ' ')
326 SET_BINARY_MODE(stdin);
327 SET_BINARY_MODE(stdout);
329 file = gzdopen(fileno(stdin), "rb");
330 if (file == NULL) error("can't gzdopen stdin");
331 gz_uncompress(file, stdout);
333 file = gzdopen(fileno(stdout), outmode);
334 if (file == NULL) error("can't gzdopen stdout");
335 gz_compress(stdin, file);
339 SET_BINARY_MODE(stdout);
344 file = gzopen(*argv, "rb");
346 fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
348 gz_uncompress(file, stdout);
350 file_uncompress(*argv);
354 FILE * in = fopen(*argv, "rb");
359 file = gzdopen(fileno(stdout), outmode);
360 if (file == NULL) error("can't gzdopen stdout");
362 gz_compress(in, file);
366 file_compress(*argv, outmode);
369 } while (argv++, --argc);