]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libz/minigzip.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libz / minigzip.c
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
4  */
5
6 /*
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
13  * or in pipe mode.
14  */
15
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
18
19 #include <stdio.h>
20 #include "zlib.h"
21
22 #ifdef STDC
23 #  include <string.h>
24 #  include <stdlib.h>
25 #endif
26
27 #ifdef USE_MMAP
28 #  include <sys/types.h>
29 #  include <sys/mman.h>
30 #  include <sys/stat.h>
31 #endif
32
33 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
34 #  include <fcntl.h>
35 #  include <io.h>
36 #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
37 #else
38 #  define SET_BINARY_MODE(file)
39 #endif
40
41 #ifdef VMS
42 #  define unlink delete
43 #  define GZ_SUFFIX "-gz"
44 #endif
45 #ifdef RISCOS
46 #  define unlink remove
47 #  define GZ_SUFFIX "-gz"
48 #  define fileno(file) file->__file
49 #endif
50 #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
51 #  include <unix.h> /* for fileno */
52 #endif
53
54 #ifndef WIN32 /* unlink already in stdio.h for WIN32 */
55   extern int unlink OF((const char *));
56 #endif
57
58 #ifndef GZ_SUFFIX
59 #  define GZ_SUFFIX ".gz"
60 #endif
61 #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
62
63 #define BUFLEN      16384
64 #define MAX_NAME_LEN 1024
65
66 #ifdef MAXSEG_64K
67 #  define local static
68    /* Needed for systems with limitation on stack size. */
69 #else
70 #  define local
71 #endif
72
73 char *prog;
74
75 void error            OF((const char *msg));
76 void gz_compress      OF((FILE   *in, gzFile out));
77 #ifdef USE_MMAP
78 int  gz_compress_mmap OF((FILE   *in, gzFile out));
79 #endif
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[]));
84
85 /* ===========================================================================
86  * Display error message and exit
87  */
88 void error(msg)
89     const char *msg;
90 {
91     fprintf(stderr, "%s: %s\n", prog, msg);
92     exit(1);
93 }
94
95 /* ===========================================================================
96  * Compress input to output then close both files.
97  */
98
99 void gz_compress(in, out)
100     FILE   *in;
101     gzFile out;
102 {
103     local char buf[BUFLEN];
104     int len;
105     int err;
106
107 #ifdef USE_MMAP
108     /* Try first compressing with mmap. If mmap fails (minigzip used in a
109      * pipe), use the normal fread loop.
110      */
111     if (gz_compress_mmap(in, out) == Z_OK) return;
112 #endif
113     for (;;) {
114         len = (int)fread(buf, 1, sizeof(buf), in);
115         if (ferror(in)) {
116             perror("fread");
117             exit(1);
118         }
119         if (len == 0) break;
120
121         if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
122     }
123     fclose(in);
124     if (gzclose(out) != Z_OK) error("failed gzclose");
125 }
126
127 #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
128
129 /* Try compressing the input file at once using mmap. Return Z_OK if
130  * if success, Z_ERRNO otherwise.
131  */
132 int gz_compress_mmap(in, out)
133     FILE   *in;
134     gzFile out;
135 {
136     int len;
137     int err;
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 */
141     struct stat sb;
142
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;
147
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;
151
152     /* Compress the whole file at once: */
153     len = gzwrite(out, (char *)buf, (unsigned)buf_len);
154
155     if (len != (int)buf_len) error(gzerror(out, &err));
156
157     munmap(buf, buf_len);
158     fclose(in);
159     if (gzclose(out) != Z_OK) error("failed gzclose");
160     return Z_OK;
161 }
162 #endif /* USE_MMAP */
163
164 /* ===========================================================================
165  * Uncompress input to output then close both files.
166  */
167 void gz_uncompress(in, out)
168     gzFile in;
169     FILE   *out;
170 {
171     local char buf[BUFLEN];
172     int len;
173     int err;
174
175     for (;;) {
176         len = gzread(in, buf, sizeof(buf));
177         if (len < 0) error (gzerror(in, &err));
178         if (len == 0) break;
179
180         if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
181             error("failed fwrite");
182         }
183     }
184     if (fclose(out)) error("failed fclose");
185
186     if (gzclose(in) != Z_OK) error("failed gzclose");
187 }
188
189
190 /* ===========================================================================
191  * Compress the given file: create a corresponding .gz file and remove the
192  * original.
193  */
194 void file_compress(file, mode)
195     char  *file;
196     char  *mode;
197 {
198     local char outfile[MAX_NAME_LEN];
199     FILE  *in;
200     gzFile out;
201
202     if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
203         fprintf(stderr, "%s: filename too long\n", prog);
204         exit(1);            
205     }
206     
207     strcpy(outfile, file);
208     strcat(outfile, GZ_SUFFIX);
209
210     in = fopen(file, "rb");
211     if (in == NULL) {
212         perror(file);
213         exit(1);
214     }
215     out = gzopen(outfile, mode);
216     if (out == NULL) {
217         fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
218         exit(1);
219     }
220     gz_compress(in, out);
221
222     unlink(file);
223 }
224
225
226 /* ===========================================================================
227  * Uncompress the given file and remove the original.
228  */
229 void file_uncompress(file)
230     char  *file;
231 {
232     local char buf[MAX_NAME_LEN];
233     char *infile, *outfile;
234     FILE  *out;
235     gzFile in;
236     size_t len = strlen(file);
237
238     if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
239         fprintf(stderr, "%s: filename too long\n", prog);
240         exit(1);            
241     }
242
243     strcpy(buf, file);
244
245     if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
246         infile = file;
247         outfile = buf;
248         outfile[len-3] = '\0';
249     } else {
250         outfile = file;
251         infile = buf;
252         strcat(infile, GZ_SUFFIX);
253     }
254     in = gzopen(infile, "rb");
255     if (in == NULL) {
256         fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
257         exit(1);
258     }
259     out = fopen(outfile, "wb");
260     if (out == NULL) {
261         perror(file);
262         exit(1);
263     }
264
265     gz_uncompress(in, out);
266
267     unlink(infile);
268 }
269
270
271 /* ===========================================================================
272  * Usage:  minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...]
273  *   -c : write to standard output
274  *   -d : decompress
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
279  */
280
281 int main(argc, argv)
282     int argc;
283     char *argv[];
284 {
285     int copyout = 0;
286     int uncompr = 0;
287     gzFile file;
288     char *bname, outmode[20];
289
290     strcpy(outmode, "wb6 ");
291
292     prog = argv[0];
293     bname = strrchr(argv[0], '/');
294     if (bname)
295       bname++;
296     else
297       bname = argv[0];
298     argc--, argv++;
299
300     if (!strcmp(bname, "gunzip"))
301       uncompr = 1;
302     else if (!strcmp(bname, "zcat"))
303       copyout = uncompr = 1;
304
305     while (argc > 0) {
306       if (strcmp(*argv, "-c") == 0)
307         copyout = 1;
308       else if (strcmp(*argv, "-d") == 0)
309         uncompr = 1;
310       else if (strcmp(*argv, "-f") == 0)
311         outmode[3] = 'f';
312       else if (strcmp(*argv, "-h") == 0)
313         outmode[3] = 'h';
314       else if (strcmp(*argv, "-r") == 0)
315         outmode[3] = 'R';
316       else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
317                (*argv)[2] == 0)
318         outmode[2] = (*argv)[1];
319       else
320         break;
321       argc--, argv++;
322     }
323     if (outmode[3] == ' ')
324         outmode[3] = 0;
325     if (argc == 0) {
326         SET_BINARY_MODE(stdin);
327         SET_BINARY_MODE(stdout);
328         if (uncompr) {
329             file = gzdopen(fileno(stdin), "rb");
330             if (file == NULL) error("can't gzdopen stdin");
331             gz_uncompress(file, stdout);
332         } else {
333             file = gzdopen(fileno(stdout), outmode);
334             if (file == NULL) error("can't gzdopen stdout");
335             gz_compress(stdin, file);
336         }
337     } else {
338         if (copyout) {
339             SET_BINARY_MODE(stdout);
340         }
341         do {
342             if (uncompr) {
343                 if (copyout) {
344                     file = gzopen(*argv, "rb");
345                     if (file == NULL)
346                         fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
347                     else
348                         gz_uncompress(file, stdout);
349                 } else {
350                     file_uncompress(*argv);
351                 }
352             } else {
353                 if (copyout) {
354                     FILE * in = fopen(*argv, "rb");
355
356                     if (in == NULL) {
357                         perror(*argv);
358                     } else {
359                         file = gzdopen(fileno(stdout), outmode);
360                         if (file == NULL) error("can't gzdopen stdout");
361
362                         gz_compress(in, file);
363                     }
364                 
365                 } else {
366                     file_compress(*argv, outmode);
367                 }
368             }
369         } while (argv++, --argc);
370     }
371     return 0;
372 }