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
16 /* @(#) $Id: minigzip.c,v 1.1 2005/01/08 05:38:13 hpa Exp $ */
25 extern void exit OF((int));
29 # include <sys/types.h>
30 # include <sys/mman.h>
31 # include <sys/stat.h>
34 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
37 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
39 # define SET_BINARY_MODE(file)
43 # define unlink delete
44 # define GZ_SUFFIX "-gz"
47 # define unlink remove
48 # define GZ_SUFFIX "-gz"
49 # define fileno(file) file->__file
51 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
52 # include <unix.h> /* for fileno */
55 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
56 extern int unlink OF((const char *));
60 # define GZ_SUFFIX ".gz"
62 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
65 #define MAX_NAME_LEN 1024
69 /* Needed for systems with limitation on stack size. */
76 void error OF((const char *msg));
77 void gz_compress OF((FILE *in, gzFile out));
79 int gz_compress_mmap OF((FILE *in, gzFile out));
81 void gz_uncompress OF((gzFile in, FILE *out));
82 void file_compress OF((char *file, char *mode));
83 void file_uncompress OF((char *file));
84 int main OF((int argc, char *argv[]));
86 /* ===========================================================================
87 * Display error message and exit
92 fprintf(stderr, "%s: %s\n", prog, msg);
96 /* ===========================================================================
97 * Compress input to output then close both files.
100 void gz_compress(in, out)
104 local char buf[BUFLEN];
109 /* Try first compressing with mmap. If mmap fails (minigzip used in a
110 * pipe), use the normal fread loop.
112 if (gz_compress_mmap(in, out) == Z_OK) return;
116 len = (int)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 strcpy(outfile, file);
205 strcat(outfile, GZ_SUFFIX);
207 in = fopen(file, "rb");
212 out = gzopen(outfile, mode);
214 fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
217 gz_compress(in, out);
223 /* ===========================================================================
224 * Uncompress the given file and remove the original.
226 void file_uncompress(file)
229 local char buf[MAX_NAME_LEN];
230 char *infile, *outfile;
233 uInt len = (uInt)strlen(file);
237 if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
240 outfile[len-3] = '\0';
244 strcat(infile, GZ_SUFFIX);
246 in = gzopen(infile, "rb");
248 fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
251 out = fopen(outfile, "wb");
257 gz_uncompress(in, out);
263 /* ===========================================================================
264 * Usage: minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...]
266 * -f : compress with Z_FILTERED
267 * -h : compress with Z_HUFFMAN_ONLY
268 * -r : compress with Z_RLE
269 * -1 to -9 : compression level
280 strcpy(outmode, "wb6 ");
286 if (strcmp(*argv, "-d") == 0)
288 else if (strcmp(*argv, "-f") == 0)
290 else if (strcmp(*argv, "-h") == 0)
292 else if (strcmp(*argv, "-r") == 0)
294 else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
296 outmode[2] = (*argv)[1];
302 SET_BINARY_MODE(stdin);
303 SET_BINARY_MODE(stdout);
305 file = gzdopen(fileno(stdin), "rb");
306 if (file == NULL) error("can't gzdopen stdin");
307 gz_uncompress(file, stdout);
309 file = gzdopen(fileno(stdout), outmode);
310 if (file == NULL) error("can't gzdopen stdout");
311 gz_compress(stdin, file);
316 file_uncompress(*argv);
318 file_compress(*argv, outmode);
320 } while (argv++, --argc);