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