1 /* minigzip.c -- simulate gzip using the zlib compression library
2 * Copyright (C) 1995-2002 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
15 * $FreeBSD: src/lib/libz/minigzip.c,v 1.6.2.4 2003/02/01 13:33:12 sobomax Exp $
16 * $DragonFly: src/lib/libz/Attic/minigzip.c,v 1.2 2003/06/17 04:26:52 dillon Exp $
26 extern void exit OF((int));
30 # include <sys/types.h>
31 # include <sys/mman.h>
32 # include <sys/stat.h>
35 #if defined(MSDOS) || defined(OS2) || defined(WIN32)
38 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
40 # define SET_BINARY_MODE(file)
44 # define unlink delete
45 # define GZ_SUFFIX "-gz"
48 # define unlink remove
49 # define GZ_SUFFIX "-gz"
50 # define fileno(file) file->__file
52 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
53 # include <unix.h> /* for fileno */
56 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
57 extern int unlink OF((const char *));
61 # define GZ_SUFFIX ".gz"
63 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
66 #define MAX_NAME_LEN 1024
70 /* Needed for systems with limitation on stack size. */
77 void error OF((const char *msg));
78 void gz_compress OF((FILE *in, gzFile out));
80 int gz_compress_mmap OF((FILE *in, gzFile out));
82 void gz_uncompress OF((gzFile in, FILE *out));
83 void file_compress OF((char *file, char *mode));
84 void file_uncompress OF((char *file));
85 int main OF((int argc, char *argv[]));
87 /* ===========================================================================
88 * Display error message and exit
93 fprintf(stderr, "%s: %s\n", prog, msg);
97 /* ===========================================================================
98 * Compress input to output then close both files.
101 void gz_compress(in, out)
105 local char buf[BUFLEN];
110 /* Try first compressing with mmap. If mmap fails (minigzip used in a
111 * pipe), use the normal fread loop.
113 if (gz_compress_mmap(in, out) == Z_OK) return;
116 len = fread(buf, 1, sizeof(buf), in);
123 if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
126 if (gzclose(out) != Z_OK) error("failed gzclose");
129 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
131 /* Try compressing the input file at once using mmap. Return Z_OK if
132 * if success, Z_ERRNO otherwise.
134 int gz_compress_mmap(in, out)
140 int ifd = fileno(in);
141 caddr_t buf; /* mmap'ed buffer for the entire input file */
142 off_t buf_len; /* length of the input file */
145 /* Determine the size of the file, needed for mmap: */
146 if (fstat(ifd, &sb) < 0) return Z_ERRNO;
147 buf_len = sb.st_size;
148 if (buf_len <= 0) return Z_ERRNO;
150 /* Now do the actual mmap: */
151 buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
152 if (buf == (caddr_t)(-1)) return Z_ERRNO;
154 /* Compress the whole file at once: */
155 len = gzwrite(out, (char *)buf, (unsigned)buf_len);
157 if (len != (int)buf_len) error(gzerror(out, &err));
159 munmap(buf, buf_len);
161 if (gzclose(out) != Z_OK) error("failed gzclose");
164 #endif /* USE_MMAP */
166 /* ===========================================================================
167 * Uncompress input to output then close both files.
169 void gz_uncompress(in, out)
173 local char buf[BUFLEN];
178 len = gzread(in, buf, sizeof(buf));
179 if (len < 0) error (gzerror(in, &err));
182 if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
183 error("failed fwrite");
186 if (fclose(out)) error("failed fclose");
188 if (gzclose(in) != Z_OK) error("failed gzclose");
192 /* ===========================================================================
193 * Compress the given file: create a corresponding .gz file and remove the
196 void file_compress(file, mode)
200 local char outfile[MAX_NAME_LEN];
204 if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
205 fprintf(stderr, "%s: filename too long\n", prog);
209 strcpy(outfile, file);
210 strcat(outfile, GZ_SUFFIX);
212 in = fopen(file, "rb");
217 out = gzopen(outfile, mode);
219 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
222 gz_compress(in, out);
228 /* ===========================================================================
229 * Uncompress the given file and remove the original.
231 void file_uncompress(file)
234 local char buf[MAX_NAME_LEN];
235 char *infile, *outfile;
238 size_t len = strlen(file);
240 if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
241 fprintf(stderr, "%s: filename too long\n", prog);
247 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
250 outfile[len-3] = '\0';
254 strcat(infile, GZ_SUFFIX);
256 in = gzopen(infile, "rb");
258 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
261 out = fopen(outfile, "wb");
267 gz_uncompress(in, out);
273 /* ===========================================================================
274 * Usage: minigzip [-c ] [-d] [-f] [-h] [-1 to -9] [files...]
275 * -c : write to standard output
277 * -f : compress with Z_FILTERED
278 * -h : compress with Z_HUFFMAN_ONLY
279 * -1 to -9 : compression level
289 char *bname, outmode[20];
291 strcpy(outmode, "wb6 ");
294 bname = strrchr(argv[0], '/');
301 if (!strcmp(bname, "gunzip"))
303 else if (!strcmp(bname, "zcat"))
304 copyout = uncompr = 1;
307 if (strcmp(*argv, "-c") == 0)
309 else if (strcmp(*argv, "-d") == 0)
311 else if (strcmp(*argv, "-f") == 0)
313 else if (strcmp(*argv, "-h") == 0)
315 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
317 outmode[2] = (*argv)[1];
323 SET_BINARY_MODE(stdin);
324 SET_BINARY_MODE(stdout);
326 file = gzdopen(fileno(stdin), "rb");
327 if (file == NULL) error("can't gzdopen stdin");
328 gz_uncompress(file, stdout);
330 file = gzdopen(fileno(stdout), outmode);
331 if (file == NULL) error("can't gzdopen stdout");
332 gz_compress(stdin, file);
336 SET_BINARY_MODE(stdout);
341 file = gzopen(*argv, "rb");
343 fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
345 gz_uncompress(file, stdout);
347 file_uncompress(*argv);
351 FILE * in = fopen(*argv, "rb");
356 file = gzdopen(fileno(stdout), outmode);
357 if (file == NULL) error("can't gzdopen stdout");
359 gz_compress(in, file);
363 file_compress(*argv, outmode);
366 } while (argv++, --argc);
369 return 0; /* to avoid warning */