1 | n/a | /* minigzip.c -- simulate gzip using the zlib compression library |
---|
2 | n/a | * Copyright (C) 1995-2006, 2010, 2011 Jean-loup Gailly. |
---|
3 | n/a | * For conditions of distribution and use, see copyright notice in zlib.h |
---|
4 | n/a | */ |
---|
5 | n/a | |
---|
6 | n/a | /* |
---|
7 | n/a | * minigzip is a minimal implementation of the gzip utility. This is |
---|
8 | n/a | * only an example of using zlib and isn't meant to replace the |
---|
9 | n/a | * full-featured gzip. No attempt is made to deal with file systems |
---|
10 | n/a | * limiting names to 14 or 8+3 characters, etc... Error checking is |
---|
11 | n/a | * very limited. So use minigzip only for testing; use gzip for the |
---|
12 | n/a | * real thing. On MSDOS, use only on file names without extension |
---|
13 | n/a | * or in pipe mode. |
---|
14 | n/a | */ |
---|
15 | n/a | |
---|
16 | n/a | /* @(#) $Id$ */ |
---|
17 | n/a | |
---|
18 | n/a | #include "zlib.h" |
---|
19 | n/a | #include <stdio.h> |
---|
20 | n/a | |
---|
21 | n/a | #ifdef STDC |
---|
22 | n/a | # include <string.h> |
---|
23 | n/a | # include <stdlib.h> |
---|
24 | n/a | #endif |
---|
25 | n/a | |
---|
26 | n/a | #ifdef USE_MMAP |
---|
27 | n/a | # include <sys/types.h> |
---|
28 | n/a | # include <sys/mman.h> |
---|
29 | n/a | # include <sys/stat.h> |
---|
30 | n/a | #endif |
---|
31 | n/a | |
---|
32 | n/a | #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) |
---|
33 | n/a | # include <fcntl.h> |
---|
34 | n/a | # include <io.h> |
---|
35 | n/a | # ifdef UNDER_CE |
---|
36 | n/a | # include <stdlib.h> |
---|
37 | n/a | # endif |
---|
38 | n/a | # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) |
---|
39 | n/a | #else |
---|
40 | n/a | # define SET_BINARY_MODE(file) |
---|
41 | n/a | #endif |
---|
42 | n/a | |
---|
43 | n/a | #ifdef _MSC_VER |
---|
44 | n/a | # define snprintf _snprintf |
---|
45 | n/a | #endif |
---|
46 | n/a | |
---|
47 | n/a | #ifdef VMS |
---|
48 | n/a | # define unlink delete |
---|
49 | n/a | # define GZ_SUFFIX "-gz" |
---|
50 | n/a | #endif |
---|
51 | n/a | #ifdef RISCOS |
---|
52 | n/a | # define unlink remove |
---|
53 | n/a | # define GZ_SUFFIX "-gz" |
---|
54 | n/a | # define fileno(file) file->__file |
---|
55 | n/a | #endif |
---|
56 | n/a | #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os |
---|
57 | n/a | # include <unix.h> /* for fileno */ |
---|
58 | n/a | #endif |
---|
59 | n/a | |
---|
60 | n/a | #if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE) |
---|
61 | n/a | #ifndef WIN32 /* unlink already in stdio.h for WIN32 */ |
---|
62 | n/a | extern int unlink OF((const char *)); |
---|
63 | n/a | #endif |
---|
64 | n/a | #endif |
---|
65 | n/a | |
---|
66 | n/a | #if defined(UNDER_CE) |
---|
67 | n/a | # include <windows.h> |
---|
68 | n/a | # define perror(s) pwinerror(s) |
---|
69 | n/a | |
---|
70 | n/a | /* Map the Windows error number in ERROR to a locale-dependent error |
---|
71 | n/a | message string and return a pointer to it. Typically, the values |
---|
72 | n/a | for ERROR come from GetLastError. |
---|
73 | n/a | |
---|
74 | n/a | The string pointed to shall not be modified by the application, |
---|
75 | n/a | but may be overwritten by a subsequent call to strwinerror |
---|
76 | n/a | |
---|
77 | n/a | The strwinerror function does not change the current setting |
---|
78 | n/a | of GetLastError. */ |
---|
79 | n/a | |
---|
80 | n/a | static char *strwinerror (error) |
---|
81 | n/a | DWORD error; |
---|
82 | n/a | { |
---|
83 | n/a | static char buf[1024]; |
---|
84 | n/a | |
---|
85 | n/a | wchar_t *msgbuf; |
---|
86 | n/a | DWORD lasterr = GetLastError(); |
---|
87 | n/a | DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
---|
88 | n/a | | FORMAT_MESSAGE_ALLOCATE_BUFFER, |
---|
89 | n/a | NULL, |
---|
90 | n/a | error, |
---|
91 | n/a | 0, /* Default language */ |
---|
92 | n/a | (LPVOID)&msgbuf, |
---|
93 | n/a | 0, |
---|
94 | n/a | NULL); |
---|
95 | n/a | if (chars != 0) { |
---|
96 | n/a | /* If there is an \r\n appended, zap it. */ |
---|
97 | n/a | if (chars >= 2 |
---|
98 | n/a | && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { |
---|
99 | n/a | chars -= 2; |
---|
100 | n/a | msgbuf[chars] = 0; |
---|
101 | n/a | } |
---|
102 | n/a | |
---|
103 | n/a | if (chars > sizeof (buf) - 1) { |
---|
104 | n/a | chars = sizeof (buf) - 1; |
---|
105 | n/a | msgbuf[chars] = 0; |
---|
106 | n/a | } |
---|
107 | n/a | |
---|
108 | n/a | wcstombs(buf, msgbuf, chars + 1); |
---|
109 | n/a | LocalFree(msgbuf); |
---|
110 | n/a | } |
---|
111 | n/a | else { |
---|
112 | n/a | sprintf(buf, "unknown win32 error (%ld)", error); |
---|
113 | n/a | } |
---|
114 | n/a | |
---|
115 | n/a | SetLastError(lasterr); |
---|
116 | n/a | return buf; |
---|
117 | n/a | } |
---|
118 | n/a | |
---|
119 | n/a | static void pwinerror (s) |
---|
120 | n/a | const char *s; |
---|
121 | n/a | { |
---|
122 | n/a | if (s && *s) |
---|
123 | n/a | fprintf(stderr, "%s: %s\n", s, strwinerror(GetLastError ())); |
---|
124 | n/a | else |
---|
125 | n/a | fprintf(stderr, "%s\n", strwinerror(GetLastError ())); |
---|
126 | n/a | } |
---|
127 | n/a | |
---|
128 | n/a | #endif /* UNDER_CE */ |
---|
129 | n/a | |
---|
130 | n/a | #ifndef GZ_SUFFIX |
---|
131 | n/a | # define GZ_SUFFIX ".gz" |
---|
132 | n/a | #endif |
---|
133 | n/a | #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1) |
---|
134 | n/a | |
---|
135 | n/a | #define BUFLEN 16384 |
---|
136 | n/a | #define MAX_NAME_LEN 1024 |
---|
137 | n/a | |
---|
138 | n/a | #ifdef MAXSEG_64K |
---|
139 | n/a | # define local static |
---|
140 | n/a | /* Needed for systems with limitation on stack size. */ |
---|
141 | n/a | #else |
---|
142 | n/a | # define local |
---|
143 | n/a | #endif |
---|
144 | n/a | |
---|
145 | n/a | #ifdef Z_SOLO |
---|
146 | n/a | /* for Z_SOLO, create simplified gz* functions using deflate and inflate */ |
---|
147 | n/a | |
---|
148 | n/a | #if defined(Z_HAVE_UNISTD_H) || defined(Z_LARGE) |
---|
149 | n/a | # include <unistd.h> /* for unlink() */ |
---|
150 | n/a | #endif |
---|
151 | n/a | |
---|
152 | n/a | void *myalloc OF((void *, unsigned, unsigned)); |
---|
153 | n/a | void myfree OF((void *, void *)); |
---|
154 | n/a | |
---|
155 | n/a | void *myalloc(q, n, m) |
---|
156 | n/a | void *q; |
---|
157 | n/a | unsigned n, m; |
---|
158 | n/a | { |
---|
159 | n/a | q = Z_NULL; |
---|
160 | n/a | return calloc(n, m); |
---|
161 | n/a | } |
---|
162 | n/a | |
---|
163 | n/a | void myfree(q, p) |
---|
164 | n/a | void *q, *p; |
---|
165 | n/a | { |
---|
166 | n/a | q = Z_NULL; |
---|
167 | n/a | free(p); |
---|
168 | n/a | } |
---|
169 | n/a | |
---|
170 | n/a | typedef struct gzFile_s { |
---|
171 | n/a | FILE *file; |
---|
172 | n/a | int write; |
---|
173 | n/a | int err; |
---|
174 | n/a | char *msg; |
---|
175 | n/a | z_stream strm; |
---|
176 | n/a | } *gzFile; |
---|
177 | n/a | |
---|
178 | n/a | gzFile gzopen OF((const char *, const char *)); |
---|
179 | n/a | gzFile gzdopen OF((int, const char *)); |
---|
180 | n/a | gzFile gz_open OF((const char *, int, const char *)); |
---|
181 | n/a | |
---|
182 | n/a | gzFile gzopen(path, mode) |
---|
183 | n/a | const char *path; |
---|
184 | n/a | const char *mode; |
---|
185 | n/a | { |
---|
186 | n/a | return gz_open(path, -1, mode); |
---|
187 | n/a | } |
---|
188 | n/a | |
---|
189 | n/a | gzFile gzdopen(fd, mode) |
---|
190 | n/a | int fd; |
---|
191 | n/a | const char *mode; |
---|
192 | n/a | { |
---|
193 | n/a | return gz_open(NULL, fd, mode); |
---|
194 | n/a | } |
---|
195 | n/a | |
---|
196 | n/a | gzFile gz_open(path, fd, mode) |
---|
197 | n/a | const char *path; |
---|
198 | n/a | int fd; |
---|
199 | n/a | const char *mode; |
---|
200 | n/a | { |
---|
201 | n/a | gzFile gz; |
---|
202 | n/a | int ret; |
---|
203 | n/a | |
---|
204 | n/a | gz = malloc(sizeof(struct gzFile_s)); |
---|
205 | n/a | if (gz == NULL) |
---|
206 | n/a | return NULL; |
---|
207 | n/a | gz->write = strchr(mode, 'w') != NULL; |
---|
208 | n/a | gz->strm.zalloc = myalloc; |
---|
209 | n/a | gz->strm.zfree = myfree; |
---|
210 | n/a | gz->strm.opaque = Z_NULL; |
---|
211 | n/a | if (gz->write) |
---|
212 | n/a | ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0); |
---|
213 | n/a | else { |
---|
214 | n/a | gz->strm.next_in = 0; |
---|
215 | n/a | gz->strm.avail_in = Z_NULL; |
---|
216 | n/a | ret = inflateInit2(&(gz->strm), 15 + 16); |
---|
217 | n/a | } |
---|
218 | n/a | if (ret != Z_OK) { |
---|
219 | n/a | free(gz); |
---|
220 | n/a | return NULL; |
---|
221 | n/a | } |
---|
222 | n/a | gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") : |
---|
223 | n/a | fopen(path, gz->write ? "wb" : "rb"); |
---|
224 | n/a | if (gz->file == NULL) { |
---|
225 | n/a | gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm)); |
---|
226 | n/a | free(gz); |
---|
227 | n/a | return NULL; |
---|
228 | n/a | } |
---|
229 | n/a | gz->err = 0; |
---|
230 | n/a | gz->msg = ""; |
---|
231 | n/a | return gz; |
---|
232 | n/a | } |
---|
233 | n/a | |
---|
234 | n/a | int gzwrite OF((gzFile, const void *, unsigned)); |
---|
235 | n/a | |
---|
236 | n/a | int gzwrite(gz, buf, len) |
---|
237 | n/a | gzFile gz; |
---|
238 | n/a | const void *buf; |
---|
239 | n/a | unsigned len; |
---|
240 | n/a | { |
---|
241 | n/a | z_stream *strm; |
---|
242 | n/a | unsigned char out[BUFLEN]; |
---|
243 | n/a | |
---|
244 | n/a | if (gz == NULL || !gz->write) |
---|
245 | n/a | return 0; |
---|
246 | n/a | strm = &(gz->strm); |
---|
247 | n/a | strm->next_in = (void *)buf; |
---|
248 | n/a | strm->avail_in = len; |
---|
249 | n/a | do { |
---|
250 | n/a | strm->next_out = out; |
---|
251 | n/a | strm->avail_out = BUFLEN; |
---|
252 | n/a | (void)deflate(strm, Z_NO_FLUSH); |
---|
253 | n/a | fwrite(out, 1, BUFLEN - strm->avail_out, gz->file); |
---|
254 | n/a | } while (strm->avail_out == 0); |
---|
255 | n/a | return len; |
---|
256 | n/a | } |
---|
257 | n/a | |
---|
258 | n/a | int gzread OF((gzFile, void *, unsigned)); |
---|
259 | n/a | |
---|
260 | n/a | int gzread(gz, buf, len) |
---|
261 | n/a | gzFile gz; |
---|
262 | n/a | void *buf; |
---|
263 | n/a | unsigned len; |
---|
264 | n/a | { |
---|
265 | n/a | int ret; |
---|
266 | n/a | unsigned got; |
---|
267 | n/a | unsigned char in[1]; |
---|
268 | n/a | z_stream *strm; |
---|
269 | n/a | |
---|
270 | n/a | if (gz == NULL || gz->write) |
---|
271 | n/a | return 0; |
---|
272 | n/a | if (gz->err) |
---|
273 | n/a | return 0; |
---|
274 | n/a | strm = &(gz->strm); |
---|
275 | n/a | strm->next_out = (void *)buf; |
---|
276 | n/a | strm->avail_out = len; |
---|
277 | n/a | do { |
---|
278 | n/a | got = fread(in, 1, 1, gz->file); |
---|
279 | n/a | if (got == 0) |
---|
280 | n/a | break; |
---|
281 | n/a | strm->next_in = in; |
---|
282 | n/a | strm->avail_in = 1; |
---|
283 | n/a | ret = inflate(strm, Z_NO_FLUSH); |
---|
284 | n/a | if (ret == Z_DATA_ERROR) { |
---|
285 | n/a | gz->err = Z_DATA_ERROR; |
---|
286 | n/a | gz->msg = strm->msg; |
---|
287 | n/a | return 0; |
---|
288 | n/a | } |
---|
289 | n/a | if (ret == Z_STREAM_END) |
---|
290 | n/a | inflateReset(strm); |
---|
291 | n/a | } while (strm->avail_out); |
---|
292 | n/a | return len - strm->avail_out; |
---|
293 | n/a | } |
---|
294 | n/a | |
---|
295 | n/a | int gzclose OF((gzFile)); |
---|
296 | n/a | |
---|
297 | n/a | int gzclose(gz) |
---|
298 | n/a | gzFile gz; |
---|
299 | n/a | { |
---|
300 | n/a | z_stream *strm; |
---|
301 | n/a | unsigned char out[BUFLEN]; |
---|
302 | n/a | |
---|
303 | n/a | if (gz == NULL) |
---|
304 | n/a | return Z_STREAM_ERROR; |
---|
305 | n/a | strm = &(gz->strm); |
---|
306 | n/a | if (gz->write) { |
---|
307 | n/a | strm->next_in = Z_NULL; |
---|
308 | n/a | strm->avail_in = 0; |
---|
309 | n/a | do { |
---|
310 | n/a | strm->next_out = out; |
---|
311 | n/a | strm->avail_out = BUFLEN; |
---|
312 | n/a | (void)deflate(strm, Z_FINISH); |
---|
313 | n/a | fwrite(out, 1, BUFLEN - strm->avail_out, gz->file); |
---|
314 | n/a | } while (strm->avail_out == 0); |
---|
315 | n/a | deflateEnd(strm); |
---|
316 | n/a | } |
---|
317 | n/a | else |
---|
318 | n/a | inflateEnd(strm); |
---|
319 | n/a | fclose(gz->file); |
---|
320 | n/a | free(gz); |
---|
321 | n/a | return Z_OK; |
---|
322 | n/a | } |
---|
323 | n/a | |
---|
324 | n/a | const char *gzerror OF((gzFile, int *)); |
---|
325 | n/a | |
---|
326 | n/a | const char *gzerror(gz, err) |
---|
327 | n/a | gzFile gz; |
---|
328 | n/a | int *err; |
---|
329 | n/a | { |
---|
330 | n/a | *err = gz->err; |
---|
331 | n/a | return gz->msg; |
---|
332 | n/a | } |
---|
333 | n/a | |
---|
334 | n/a | #endif |
---|
335 | n/a | |
---|
336 | n/a | char *prog; |
---|
337 | n/a | |
---|
338 | n/a | void error OF((const char *msg)); |
---|
339 | n/a | void gz_compress OF((FILE *in, gzFile out)); |
---|
340 | n/a | #ifdef USE_MMAP |
---|
341 | n/a | int gz_compress_mmap OF((FILE *in, gzFile out)); |
---|
342 | n/a | #endif |
---|
343 | n/a | void gz_uncompress OF((gzFile in, FILE *out)); |
---|
344 | n/a | void file_compress OF((char *file, char *mode)); |
---|
345 | n/a | void file_uncompress OF((char *file)); |
---|
346 | n/a | int main OF((int argc, char *argv[])); |
---|
347 | n/a | |
---|
348 | n/a | /* =========================================================================== |
---|
349 | n/a | * Display error message and exit |
---|
350 | n/a | */ |
---|
351 | n/a | void error(msg) |
---|
352 | n/a | const char *msg; |
---|
353 | n/a | { |
---|
354 | n/a | fprintf(stderr, "%s: %s\n", prog, msg); |
---|
355 | n/a | exit(1); |
---|
356 | n/a | } |
---|
357 | n/a | |
---|
358 | n/a | /* =========================================================================== |
---|
359 | n/a | * Compress input to output then close both files. |
---|
360 | n/a | */ |
---|
361 | n/a | |
---|
362 | n/a | void gz_compress(in, out) |
---|
363 | n/a | FILE *in; |
---|
364 | n/a | gzFile out; |
---|
365 | n/a | { |
---|
366 | n/a | local char buf[BUFLEN]; |
---|
367 | n/a | int len; |
---|
368 | n/a | int err; |
---|
369 | n/a | |
---|
370 | n/a | #ifdef USE_MMAP |
---|
371 | n/a | /* Try first compressing with mmap. If mmap fails (minigzip used in a |
---|
372 | n/a | * pipe), use the normal fread loop. |
---|
373 | n/a | */ |
---|
374 | n/a | if (gz_compress_mmap(in, out) == Z_OK) return; |
---|
375 | n/a | #endif |
---|
376 | n/a | for (;;) { |
---|
377 | n/a | len = (int)fread(buf, 1, sizeof(buf), in); |
---|
378 | n/a | if (ferror(in)) { |
---|
379 | n/a | perror("fread"); |
---|
380 | n/a | exit(1); |
---|
381 | n/a | } |
---|
382 | n/a | if (len == 0) break; |
---|
383 | n/a | |
---|
384 | n/a | if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err)); |
---|
385 | n/a | } |
---|
386 | n/a | fclose(in); |
---|
387 | n/a | if (gzclose(out) != Z_OK) error("failed gzclose"); |
---|
388 | n/a | } |
---|
389 | n/a | |
---|
390 | n/a | #ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */ |
---|
391 | n/a | |
---|
392 | n/a | /* Try compressing the input file at once using mmap. Return Z_OK if |
---|
393 | n/a | * if success, Z_ERRNO otherwise. |
---|
394 | n/a | */ |
---|
395 | n/a | int gz_compress_mmap(in, out) |
---|
396 | n/a | FILE *in; |
---|
397 | n/a | gzFile out; |
---|
398 | n/a | { |
---|
399 | n/a | int len; |
---|
400 | n/a | int err; |
---|
401 | n/a | int ifd = fileno(in); |
---|
402 | n/a | caddr_t buf; /* mmap'ed buffer for the entire input file */ |
---|
403 | n/a | off_t buf_len; /* length of the input file */ |
---|
404 | n/a | struct stat sb; |
---|
405 | n/a | |
---|
406 | n/a | /* Determine the size of the file, needed for mmap: */ |
---|
407 | n/a | if (fstat(ifd, &sb) < 0) return Z_ERRNO; |
---|
408 | n/a | buf_len = sb.st_size; |
---|
409 | n/a | if (buf_len <= 0) return Z_ERRNO; |
---|
410 | n/a | |
---|
411 | n/a | /* Now do the actual mmap: */ |
---|
412 | n/a | buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0); |
---|
413 | n/a | if (buf == (caddr_t)(-1)) return Z_ERRNO; |
---|
414 | n/a | |
---|
415 | n/a | /* Compress the whole file at once: */ |
---|
416 | n/a | len = gzwrite(out, (char *)buf, (unsigned)buf_len); |
---|
417 | n/a | |
---|
418 | n/a | if (len != (int)buf_len) error(gzerror(out, &err)); |
---|
419 | n/a | |
---|
420 | n/a | munmap(buf, buf_len); |
---|
421 | n/a | fclose(in); |
---|
422 | n/a | if (gzclose(out) != Z_OK) error("failed gzclose"); |
---|
423 | n/a | return Z_OK; |
---|
424 | n/a | } |
---|
425 | n/a | #endif /* USE_MMAP */ |
---|
426 | n/a | |
---|
427 | n/a | /* =========================================================================== |
---|
428 | n/a | * Uncompress input to output then close both files. |
---|
429 | n/a | */ |
---|
430 | n/a | void gz_uncompress(in, out) |
---|
431 | n/a | gzFile in; |
---|
432 | n/a | FILE *out; |
---|
433 | n/a | { |
---|
434 | n/a | local char buf[BUFLEN]; |
---|
435 | n/a | int len; |
---|
436 | n/a | int err; |
---|
437 | n/a | |
---|
438 | n/a | for (;;) { |
---|
439 | n/a | len = gzread(in, buf, sizeof(buf)); |
---|
440 | n/a | if (len < 0) error (gzerror(in, &err)); |
---|
441 | n/a | if (len == 0) break; |
---|
442 | n/a | |
---|
443 | n/a | if ((int)fwrite(buf, 1, (unsigned)len, out) != len) { |
---|
444 | n/a | error("failed fwrite"); |
---|
445 | n/a | } |
---|
446 | n/a | } |
---|
447 | n/a | if (fclose(out)) error("failed fclose"); |
---|
448 | n/a | |
---|
449 | n/a | if (gzclose(in) != Z_OK) error("failed gzclose"); |
---|
450 | n/a | } |
---|
451 | n/a | |
---|
452 | n/a | |
---|
453 | n/a | /* =========================================================================== |
---|
454 | n/a | * Compress the given file: create a corresponding .gz file and remove the |
---|
455 | n/a | * original. |
---|
456 | n/a | */ |
---|
457 | n/a | void file_compress(file, mode) |
---|
458 | n/a | char *file; |
---|
459 | n/a | char *mode; |
---|
460 | n/a | { |
---|
461 | n/a | local char outfile[MAX_NAME_LEN]; |
---|
462 | n/a | FILE *in; |
---|
463 | n/a | gzFile out; |
---|
464 | n/a | |
---|
465 | n/a | if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) { |
---|
466 | n/a | fprintf(stderr, "%s: filename too long\n", prog); |
---|
467 | n/a | exit(1); |
---|
468 | n/a | } |
---|
469 | n/a | |
---|
470 | n/a | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) |
---|
471 | n/a | snprintf(outfile, sizeof(outfile), "%s%s", file, GZ_SUFFIX); |
---|
472 | n/a | #else |
---|
473 | n/a | strcpy(outfile, file); |
---|
474 | n/a | strcat(outfile, GZ_SUFFIX); |
---|
475 | n/a | #endif |
---|
476 | n/a | |
---|
477 | n/a | in = fopen(file, "rb"); |
---|
478 | n/a | if (in == NULL) { |
---|
479 | n/a | perror(file); |
---|
480 | n/a | exit(1); |
---|
481 | n/a | } |
---|
482 | n/a | out = gzopen(outfile, mode); |
---|
483 | n/a | if (out == NULL) { |
---|
484 | n/a | fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile); |
---|
485 | n/a | exit(1); |
---|
486 | n/a | } |
---|
487 | n/a | gz_compress(in, out); |
---|
488 | n/a | |
---|
489 | n/a | unlink(file); |
---|
490 | n/a | } |
---|
491 | n/a | |
---|
492 | n/a | |
---|
493 | n/a | /* =========================================================================== |
---|
494 | n/a | * Uncompress the given file and remove the original. |
---|
495 | n/a | */ |
---|
496 | n/a | void file_uncompress(file) |
---|
497 | n/a | char *file; |
---|
498 | n/a | { |
---|
499 | n/a | local char buf[MAX_NAME_LEN]; |
---|
500 | n/a | char *infile, *outfile; |
---|
501 | n/a | FILE *out; |
---|
502 | n/a | gzFile in; |
---|
503 | n/a | size_t len = strlen(file); |
---|
504 | n/a | |
---|
505 | n/a | if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) { |
---|
506 | n/a | fprintf(stderr, "%s: filename too long\n", prog); |
---|
507 | n/a | exit(1); |
---|
508 | n/a | } |
---|
509 | n/a | |
---|
510 | n/a | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) |
---|
511 | n/a | snprintf(buf, sizeof(buf), "%s", file); |
---|
512 | n/a | #else |
---|
513 | n/a | strcpy(buf, file); |
---|
514 | n/a | #endif |
---|
515 | n/a | |
---|
516 | n/a | if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) { |
---|
517 | n/a | infile = file; |
---|
518 | n/a | outfile = buf; |
---|
519 | n/a | outfile[len-3] = '\0'; |
---|
520 | n/a | } else { |
---|
521 | n/a | outfile = file; |
---|
522 | n/a | infile = buf; |
---|
523 | n/a | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) |
---|
524 | n/a | snprintf(buf + len, sizeof(buf) - len, "%s", GZ_SUFFIX); |
---|
525 | n/a | #else |
---|
526 | n/a | strcat(infile, GZ_SUFFIX); |
---|
527 | n/a | #endif |
---|
528 | n/a | } |
---|
529 | n/a | in = gzopen(infile, "rb"); |
---|
530 | n/a | if (in == NULL) { |
---|
531 | n/a | fprintf(stderr, "%s: can't gzopen %s\n", prog, infile); |
---|
532 | n/a | exit(1); |
---|
533 | n/a | } |
---|
534 | n/a | out = fopen(outfile, "wb"); |
---|
535 | n/a | if (out == NULL) { |
---|
536 | n/a | perror(file); |
---|
537 | n/a | exit(1); |
---|
538 | n/a | } |
---|
539 | n/a | |
---|
540 | n/a | gz_uncompress(in, out); |
---|
541 | n/a | |
---|
542 | n/a | unlink(infile); |
---|
543 | n/a | } |
---|
544 | n/a | |
---|
545 | n/a | |
---|
546 | n/a | /* =========================================================================== |
---|
547 | n/a | * Usage: minigzip [-c] [-d] [-f] [-h] [-r] [-1 to -9] [files...] |
---|
548 | n/a | * -c : write to standard output |
---|
549 | n/a | * -d : decompress |
---|
550 | n/a | * -f : compress with Z_FILTERED |
---|
551 | n/a | * -h : compress with Z_HUFFMAN_ONLY |
---|
552 | n/a | * -r : compress with Z_RLE |
---|
553 | n/a | * -1 to -9 : compression level |
---|
554 | n/a | */ |
---|
555 | n/a | |
---|
556 | n/a | int main(argc, argv) |
---|
557 | n/a | int argc; |
---|
558 | n/a | char *argv[]; |
---|
559 | n/a | { |
---|
560 | n/a | int copyout = 0; |
---|
561 | n/a | int uncompr = 0; |
---|
562 | n/a | gzFile file; |
---|
563 | n/a | char *bname, outmode[20]; |
---|
564 | n/a | |
---|
565 | n/a | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) |
---|
566 | n/a | snprintf(outmode, sizeof(outmode), "%s", "wb6 "); |
---|
567 | n/a | #else |
---|
568 | n/a | strcpy(outmode, "wb6 "); |
---|
569 | n/a | #endif |
---|
570 | n/a | |
---|
571 | n/a | prog = argv[0]; |
---|
572 | n/a | bname = strrchr(argv[0], '/'); |
---|
573 | n/a | if (bname) |
---|
574 | n/a | bname++; |
---|
575 | n/a | else |
---|
576 | n/a | bname = argv[0]; |
---|
577 | n/a | argc--, argv++; |
---|
578 | n/a | |
---|
579 | n/a | if (!strcmp(bname, "gunzip")) |
---|
580 | n/a | uncompr = 1; |
---|
581 | n/a | else if (!strcmp(bname, "zcat")) |
---|
582 | n/a | copyout = uncompr = 1; |
---|
583 | n/a | |
---|
584 | n/a | while (argc > 0) { |
---|
585 | n/a | if (strcmp(*argv, "-c") == 0) |
---|
586 | n/a | copyout = 1; |
---|
587 | n/a | else if (strcmp(*argv, "-d") == 0) |
---|
588 | n/a | uncompr = 1; |
---|
589 | n/a | else if (strcmp(*argv, "-f") == 0) |
---|
590 | n/a | outmode[3] = 'f'; |
---|
591 | n/a | else if (strcmp(*argv, "-h") == 0) |
---|
592 | n/a | outmode[3] = 'h'; |
---|
593 | n/a | else if (strcmp(*argv, "-r") == 0) |
---|
594 | n/a | outmode[3] = 'R'; |
---|
595 | n/a | else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' && |
---|
596 | n/a | (*argv)[2] == 0) |
---|
597 | n/a | outmode[2] = (*argv)[1]; |
---|
598 | n/a | else |
---|
599 | n/a | break; |
---|
600 | n/a | argc--, argv++; |
---|
601 | n/a | } |
---|
602 | n/a | if (outmode[3] == ' ') |
---|
603 | n/a | outmode[3] = 0; |
---|
604 | n/a | if (argc == 0) { |
---|
605 | n/a | SET_BINARY_MODE(stdin); |
---|
606 | n/a | SET_BINARY_MODE(stdout); |
---|
607 | n/a | if (uncompr) { |
---|
608 | n/a | file = gzdopen(fileno(stdin), "rb"); |
---|
609 | n/a | if (file == NULL) error("can't gzdopen stdin"); |
---|
610 | n/a | gz_uncompress(file, stdout); |
---|
611 | n/a | } else { |
---|
612 | n/a | file = gzdopen(fileno(stdout), outmode); |
---|
613 | n/a | if (file == NULL) error("can't gzdopen stdout"); |
---|
614 | n/a | gz_compress(stdin, file); |
---|
615 | n/a | } |
---|
616 | n/a | } else { |
---|
617 | n/a | if (copyout) { |
---|
618 | n/a | SET_BINARY_MODE(stdout); |
---|
619 | n/a | } |
---|
620 | n/a | do { |
---|
621 | n/a | if (uncompr) { |
---|
622 | n/a | if (copyout) { |
---|
623 | n/a | file = gzopen(*argv, "rb"); |
---|
624 | n/a | if (file == NULL) |
---|
625 | n/a | fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv); |
---|
626 | n/a | else |
---|
627 | n/a | gz_uncompress(file, stdout); |
---|
628 | n/a | } else { |
---|
629 | n/a | file_uncompress(*argv); |
---|
630 | n/a | } |
---|
631 | n/a | } else { |
---|
632 | n/a | if (copyout) { |
---|
633 | n/a | FILE * in = fopen(*argv, "rb"); |
---|
634 | n/a | |
---|
635 | n/a | if (in == NULL) { |
---|
636 | n/a | perror(*argv); |
---|
637 | n/a | } else { |
---|
638 | n/a | file = gzdopen(fileno(stdout), outmode); |
---|
639 | n/a | if (file == NULL) error("can't gzdopen stdout"); |
---|
640 | n/a | |
---|
641 | n/a | gz_compress(in, file); |
---|
642 | n/a | } |
---|
643 | n/a | |
---|
644 | n/a | } else { |
---|
645 | n/a | file_compress(*argv, outmode); |
---|
646 | n/a | } |
---|
647 | n/a | } |
---|
648 | n/a | } while (argv++, --argc); |
---|
649 | n/a | } |
---|
650 | n/a | return 0; |
---|
651 | n/a | } |
---|