1 | /* |
1 | /* |
2 | * Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de> |
2 | * Copyright (c) 2006 Stefan Traby <stefan@hello-penguin.com> |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
5 | * tion, are permitted provided that the following conditions are met: |
5 | * tion, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
… | |
… | |
31 | * allow the use of your version of this file only under the terms of the |
31 | * allow the use of your version of this file only under the terms of the |
32 | * GPL and not to allow others to use your version of this file under the |
32 | * GPL and not to allow others to use your version of this file under the |
33 | * BSD license, indicate your decision by deleting the provisions above and |
33 | * BSD license, indicate your decision by deleting the provisions above and |
34 | * replace them with the notice and other provisions required by the GPL. If |
34 | * replace them with the notice and other provisions required by the GPL. If |
35 | * you do not delete the provisions above, a recipient may use your version |
35 | * you do not delete the provisions above, a recipient may use your version |
36 | * of this file under either the BSD or the GPL. |
36 | * of this file under either the BSD or the GPL License. |
37 | */ |
37 | */ |
38 | |
38 | |
39 | #include "config.h" |
39 | #include "config.h" |
40 | |
|
|
41 | #include <stdio.h> |
40 | #include <stdio.h> |
|
|
41 | #include <string.h> |
42 | #include <stdlib.h> |
42 | #include <stdlib.h> |
43 | #include <string.h> |
|
|
44 | #include <assert.h> |
|
|
45 | |
|
|
46 | #include <unistd.h> |
43 | #include <unistd.h> |
|
|
44 | #include <sys/types.h> |
|
|
45 | #include <sys/stat.h> |
|
|
46 | #include <fcntl.h> |
47 | #include <getopt.h> |
47 | #include <errno.h> |
48 | |
48 | #include <limits.h> |
49 | #include "lzf.h" |
49 | #include "lzf.h" |
50 | |
50 | |
|
|
51 | #ifdef HAVE_GETOPT_H |
|
|
52 | # include <getopt.h> |
|
|
53 | #endif |
|
|
54 | |
|
|
55 | #define BLOCKSIZE (1024 * 64 - 1) |
|
|
56 | #define MAX_BLOCKSIZE BLOCKSIZE |
|
|
57 | |
51 | typedef unsigned char u8; |
58 | typedef unsigned char u8; |
52 | |
59 | |
|
|
60 | static off_t nr_read, nr_written; |
|
|
61 | |
|
|
62 | static const char *imagename; |
|
|
63 | static enum { compress, uncompress, lzcat } mode = compress; |
|
|
64 | static int verbose = 0; |
|
|
65 | static int force = 0; |
|
|
66 | static long blocksize = BLOCKSIZE; |
|
|
67 | |
|
|
68 | #ifdef HAVE_GETOPT_LONG |
|
|
69 | |
|
|
70 | struct option longopts[] = { |
|
|
71 | {"compress", 0, 0, 'c'}, |
|
|
72 | {"decompress", 0, 0, 'd'}, |
|
|
73 | {"uncompress", 0, 0, 'd'}, |
|
|
74 | {"force", 0, 0, 'f'}, |
|
|
75 | {"help", 0, 0, 'h'}, |
|
|
76 | {"verbose", 0, 0, 'v'}, |
|
|
77 | {"blocksize", 1, 0, 'b'}, |
|
|
78 | {0, 0, 0, 0} |
|
|
79 | }; |
|
|
80 | |
|
|
81 | static const char *opt = |
|
|
82 | "-c --compress compress\n" |
|
|
83 | "-d --decompress decompress\n" |
|
|
84 | "-f --force force overwrite of output file\n" |
|
|
85 | "-h --help give this help\n" "-v --verbose verbose mode\n" "-b # --blocksize # set blocksize\n" "\n"; |
|
|
86 | |
|
|
87 | #else |
|
|
88 | |
|
|
89 | static const char *opt = |
|
|
90 | "-c compress\n" |
|
|
91 | "-d decompress\n" |
|
|
92 | "-f force overwrite of output file\n" |
|
|
93 | "-h give this help\n" |
|
|
94 | "-v verbose mode\n" |
|
|
95 | "-b # set blocksize\n" |
|
|
96 | "\n"; |
|
|
97 | |
|
|
98 | #endif |
|
|
99 | |
53 | static void |
100 | static void |
54 | usage (int ec) |
101 | usage (int rc) |
55 | { |
102 | { |
56 | fprintf (stderr, "\n" |
103 | fprintf (stderr, "\n" |
57 | "lzf, a very lightweight compression/decompression filter\n" |
104 | "lzf, a very lightweight compression/decompression utility written by Stefan Traby.\n" |
58 | "written by Marc Lehmann <schmorp@schmorp.de> You can find more info at\n" |
105 | "uses liblzf written by Marc Lehmann <schmorp@schmorp.de> You can find more info at\n" |
59 | "http://liblzf.plan9.de/\n" |
106 | "http://liblzf.plan9.de/\n" |
60 | "\n" |
107 | "\n" |
61 | "USAGE: lzf -c [-b blocksize] | -d\n" |
108 | "usage: lzf [-dufhvb] [file ...]\n" |
62 | " -c compress\n" |
109 | " unlzf [file ...]\n" |
63 | " -d decompress\n" |
110 | " lzcat [file ...]\n" |
64 | " -b specify the blocksize (default 64k-1)\n" |
|
|
65 | "\n" |
111 | "\n%s", |
66 | ); |
112 | opt); |
67 | |
113 | |
68 | exit (ec); |
114 | exit (rc); |
|
|
115 | } |
|
|
116 | |
|
|
117 | static inline ssize_t |
|
|
118 | rread (int fd, void *buf, size_t len) |
|
|
119 | { |
|
|
120 | ssize_t rc = 0, offset = 0; |
|
|
121 | char *p = buf; |
|
|
122 | |
|
|
123 | while (len && (rc = read (fd, &p[offset], len)) > 0) |
|
|
124 | { |
|
|
125 | offset += rc; |
|
|
126 | len -= rc; |
|
|
127 | } |
|
|
128 | |
|
|
129 | nr_read += offset; |
|
|
130 | |
|
|
131 | if (rc < 0) |
|
|
132 | return rc; |
|
|
133 | |
|
|
134 | return offset; |
|
|
135 | } |
|
|
136 | |
|
|
137 | /* returns 0 if all written else -1 */ |
|
|
138 | static inline ssize_t |
|
|
139 | wwrite (int fd, void *buf, size_t len) |
|
|
140 | { |
|
|
141 | ssize_t rc; |
|
|
142 | char *b = buf; |
|
|
143 | size_t l = len; |
|
|
144 | |
|
|
145 | while (l) |
|
|
146 | { |
|
|
147 | rc = write (fd, b, l); |
|
|
148 | if (rc < 0) |
|
|
149 | { |
|
|
150 | fprintf (stderr, "%s: write error: ", imagename); |
|
|
151 | perror (""); |
|
|
152 | return -1; |
|
|
153 | } |
|
|
154 | |
|
|
155 | l -= rc; |
|
|
156 | b += rc; |
|
|
157 | } |
|
|
158 | |
|
|
159 | nr_written += len; |
|
|
160 | return 0; |
69 | } |
161 | } |
70 | |
162 | |
71 | /* |
163 | /* |
72 | * Anatomy: an lzf file consists of any number of blocks in the following format: |
164 | * Anatomy: an lzf file consists of any number of blocks in the following format: |
73 | * |
165 | * |
|
|
166 | * \x00 EOF (optional) |
74 | * "ZV\0" 2-byte-usize <uncompressed data> |
167 | * "ZV\0" 2-byte-usize <uncompressed data> |
75 | * "ZV\1" 2-byte-csize 2-byte-usize <compressed data> |
168 | * "ZV\1" 2-byte-csize 2-byte-usize <compressed data> |
76 | * "ZV\2" 4-byte-crc32-0xdebb20e3 (NYI) |
169 | * "ZV\2" 4-byte-crc32-0xdebb20e3 (NYI) |
77 | * |
|
|
78 | */ |
170 | */ |
79 | |
171 | |
80 | static void compress (unsigned int blocksize) |
172 | |
|
|
173 | #define TYPE0_HDR_SIZE 5 |
|
|
174 | #define TYPE1_HDR_SIZE 7 |
|
|
175 | #define MAX_HDR_SIZE 7 |
|
|
176 | #define MIN_HDR_SIZE 5 |
|
|
177 | |
|
|
178 | static int |
|
|
179 | compress_fd (int from, int to) |
81 | { |
180 | { |
82 | ssize_t us; |
181 | ssize_t us, cs, len; |
83 | unsigned int cs; |
182 | u8 buf1[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16]; |
84 | u8 buff1[64*1024]; |
183 | u8 buf2[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16]; |
85 | u8 buff2[64*1024]; |
|
|
86 | u8 header[3+2+2]; |
184 | u8 *header; |
87 | |
185 | |
88 | header[0] = 'Z'; |
186 | nr_read = nr_written = 0; |
89 | header[1] = 'V'; |
187 | while ((us = rread (from, &buf1[MAX_HDR_SIZE], blocksize)) > 0) |
90 | |
|
|
91 | for(;;) { |
|
|
92 | us = fread (buff1, 1, blocksize, stdin); |
|
|
93 | |
|
|
94 | if (us < blocksize) |
|
|
95 | { |
188 | { |
96 | if (us == 0) |
189 | cs = lzf_compress (&buf1[MAX_HDR_SIZE], us, &buf2[MAX_HDR_SIZE], us > 4 ? us - 4 : us); |
97 | break; |
|
|
98 | else if (!feof (stdin)) |
|
|
99 | { |
|
|
100 | perror ("compress"); |
|
|
101 | exit (1); |
|
|
102 | } |
|
|
103 | } |
|
|
104 | |
|
|
105 | cs = lzf_compress (buff1, us, buff2, us - 4); |
|
|
106 | |
|
|
107 | if (cs) |
190 | if (cs) |
108 | { |
191 | { |
|
|
192 | header = &buf2[MAX_HDR_SIZE - TYPE1_HDR_SIZE]; |
|
|
193 | header[0] = 'Z'; |
|
|
194 | header[1] = 'V'; |
109 | header[2] = 1; |
195 | header[2] = 1; |
110 | header[3] = cs >> 8; |
196 | header[3] = cs >> 8; |
111 | header[4] = cs & 0xff; |
197 | header[4] = cs & 0xff; |
112 | header[5] = us >> 8; |
198 | header[5] = us >> 8; |
113 | header[6] = us & 0xff; |
199 | header[6] = us & 0xff; |
114 | |
200 | len = cs + TYPE1_HDR_SIZE; |
115 | fwrite (header, 3+2+2, 1, stdout); |
|
|
116 | fwrite (buff2, cs, 1, stdout); |
|
|
117 | } |
201 | } |
118 | else |
202 | else |
119 | { |
203 | { // write uncompressed |
|
|
204 | header = &buf1[MAX_HDR_SIZE - TYPE0_HDR_SIZE]; |
|
|
205 | header[0] = 'Z'; |
|
|
206 | header[1] = 'V'; |
120 | header[2] = 0; |
207 | header[2] = 0; |
121 | header[3] = us >> 8; |
208 | header[3] = us >> 8; |
122 | header[4] = us & 0xff; |
209 | header[4] = us & 0xff; |
123 | |
210 | len = us + TYPE0_HDR_SIZE; |
124 | fwrite (header, 3+2, 1, stdout); |
|
|
125 | fwrite (buff1, us, 1, stdout); |
|
|
126 | } |
211 | } |
127 | } while (!feof (stdin)); |
|
|
128 | } |
|
|
129 | |
212 | |
130 | static void decompress (void) |
213 | if (wwrite (to, header, len) == -1) |
131 | { |
214 | return -1; |
132 | ssize_t us; |
215 | } |
133 | unsigned int cs; |
|
|
134 | u8 buff1[64*1024]; |
|
|
135 | u8 buff2[64*1024]; |
|
|
136 | u8 header[3+2+2]; |
|
|
137 | |
216 | |
138 | for(;;) { |
217 | return 0; |
139 | if (fread (header, 3+2, 1, stdin) != 1) |
218 | } |
|
|
219 | |
|
|
220 | static int |
|
|
221 | uncompress_fd (int from, int to) |
|
|
222 | { |
|
|
223 | u8 header[MAX_HDR_SIZE]; |
|
|
224 | u8 buf1[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16]; |
|
|
225 | u8 buf2[MAX_BLOCKSIZE + MAX_HDR_SIZE + 16]; |
|
|
226 | u8 *p; |
|
|
227 | int l, rd; |
|
|
228 | ssize_t rc, cs, us, bytes, over = 0; |
|
|
229 | |
|
|
230 | nr_read = nr_written = 0; |
|
|
231 | while (1) |
|
|
232 | { |
|
|
233 | rc = rread (from, header + over, MAX_HDR_SIZE - over); |
|
|
234 | if (rc < 0) |
140 | { |
235 | { |
141 | if (feof (stdin)) |
236 | fprintf (stderr, "%s: read error: ", imagename); |
|
|
237 | perror (""); |
|
|
238 | return -1; |
|
|
239 | } |
|
|
240 | |
|
|
241 | rc += over; |
|
|
242 | over = 0; |
|
|
243 | if (!rc || header[0] == 0) |
|
|
244 | return 0; |
|
|
245 | |
|
|
246 | if (rc < MIN_HDR_SIZE || header[0] != 'Z' || header[1] != 'V') |
|
|
247 | { |
|
|
248 | fprintf (stderr, "%s: invalid data stream - magic not found or short header\n", imagename); |
|
|
249 | return -1; |
|
|
250 | } |
|
|
251 | |
|
|
252 | switch (header[2]) |
|
|
253 | { |
|
|
254 | case 0: |
|
|
255 | cs = -1; |
|
|
256 | us = (header[3] << 8) | header[4]; |
|
|
257 | p = &header[TYPE0_HDR_SIZE]; |
142 | break; |
258 | break; |
143 | else |
259 | case 1: |
|
|
260 | if (rc < TYPE1_HDR_SIZE) |
144 | { |
261 | { |
145 | perror ("decompress"); |
262 | goto short_read; |
146 | exit (1); |
|
|
147 | } |
263 | } |
148 | } |
|
|
149 | |
|
|
150 | if (header[0] != 'Z' || header[1] != 'V') |
|
|
151 | { |
|
|
152 | fprintf (stderr, "decompress: invalid stream - no magic number found\n"); |
|
|
153 | exit (1); |
|
|
154 | } |
|
|
155 | |
|
|
156 | cs = (header[3] << 8) | header[4]; |
264 | cs = (header[3] << 8) | header[4]; |
157 | |
|
|
158 | if (header[2] == 1) |
|
|
159 | { |
|
|
160 | if (fread (header+3+2, 2, 1, stdin) != 1) |
|
|
161 | { |
|
|
162 | perror ("decompress"); |
|
|
163 | exit (1); |
|
|
164 | } |
|
|
165 | |
|
|
166 | us = (header[5] << 8) | header[6]; |
265 | us = (header[5] << 8) | header[6]; |
167 | |
266 | p = &header[TYPE1_HDR_SIZE]; |
168 | if (fread (buff1, cs, 1, stdin) != 1) |
267 | break; |
169 | { |
268 | default: |
170 | perror ("decompress"); |
269 | fprintf (stderr, "%s: unknown blocktype\n", imagename); |
171 | exit (1); |
270 | return -1; |
172 | } |
271 | } |
173 | |
272 | |
|
|
273 | bytes = cs == -1 ? us : cs; |
|
|
274 | l = &header[rc] - p; |
|
|
275 | |
|
|
276 | if (l > 0) |
|
|
277 | memcpy (buf1, p, l); |
|
|
278 | |
|
|
279 | if (l > bytes) |
|
|
280 | { |
|
|
281 | over = l - bytes; |
|
|
282 | memmove (header, &p[bytes], over); |
|
|
283 | } |
|
|
284 | |
|
|
285 | p = &buf1[l]; |
|
|
286 | rd = bytes - l; |
|
|
287 | if (rd > 0) |
|
|
288 | if ((rc = rread (from, p, rd)) != rd) |
|
|
289 | goto short_read; |
|
|
290 | |
|
|
291 | if (cs == -1) |
|
|
292 | { |
|
|
293 | if (wwrite (to, buf1, us)) |
|
|
294 | return -1; |
|
|
295 | } |
|
|
296 | else |
|
|
297 | { |
174 | if (lzf_decompress (buff1, cs, buff2, us) != us) |
298 | if (lzf_decompress (buf1, cs, buf2, us) != us) |
175 | { |
299 | { |
176 | fprintf (stderr, "decompress: invalid stream - data corrupted\n"); |
300 | fprintf (stderr, "%s: decompress: invalid stream - data corrupted\n", imagename); |
177 | exit (1); |
301 | return -1; |
178 | } |
302 | } |
179 | |
303 | |
180 | fwrite (buff2, us, 1, stdout); |
304 | if (wwrite (to, buf2, us)) |
181 | } |
305 | return -1; |
182 | else if (header[2] == 0) |
|
|
183 | { |
|
|
184 | if (fread (buff2, cs, 1, stdin) != 1) |
|
|
185 | { |
|
|
186 | perror ("decompress"); |
|
|
187 | exit (1); |
|
|
188 | } |
306 | } |
189 | |
|
|
190 | fwrite (buff2, cs, 1, stdout); |
|
|
191 | } |
307 | } |
192 | else |
308 | |
|
|
309 | return 0; |
|
|
310 | |
|
|
311 | short_read: |
|
|
312 | fprintf (stderr, "%s: short data\n", imagename); |
|
|
313 | return -1; |
|
|
314 | } |
|
|
315 | |
|
|
316 | static int |
|
|
317 | open_out (const char *name) |
|
|
318 | { |
|
|
319 | int fd; |
|
|
320 | int m = O_EXCL; |
|
|
321 | |
|
|
322 | if (force) |
|
|
323 | m = 0; |
|
|
324 | |
|
|
325 | fd = open (name, O_CREAT | O_WRONLY | O_TRUNC | m, 600); |
|
|
326 | return fd; |
|
|
327 | } |
|
|
328 | |
|
|
329 | static int |
|
|
330 | compose_name (const char *fname, char *oname) |
|
|
331 | { |
|
|
332 | char *p; |
|
|
333 | |
|
|
334 | if (mode == compress) |
|
|
335 | { |
|
|
336 | if (strlen (fname) > PATH_MAX - 4) |
193 | { |
337 | { |
194 | fprintf (stderr, "decompress: invalid stream - unknown block type\n"); |
338 | fprintf (stderr, "%s: %s.lzf: name too long", imagename, fname); |
195 | exit (1); |
339 | return -1; |
196 | } |
340 | } |
|
|
341 | |
|
|
342 | strcpy (oname, fname); |
|
|
343 | strcat (oname, ".lzf"); |
197 | } |
344 | } |
|
|
345 | else |
|
|
346 | { |
|
|
347 | if (strlen (fname) > PATH_MAX) |
|
|
348 | { |
|
|
349 | fprintf (stderr, "%s: %s: name too long\n", imagename, fname); |
|
|
350 | return -1; |
|
|
351 | } |
|
|
352 | |
|
|
353 | strcpy (oname, fname); |
|
|
354 | p = &oname[strlen (oname)] - 4; |
|
|
355 | if (p < oname || strcmp (p, ".lzf")) |
|
|
356 | { |
|
|
357 | fprintf (stderr, "%s: %s: unknown suffix\n", imagename, fname); |
|
|
358 | return -1; |
|
|
359 | } |
|
|
360 | |
|
|
361 | *p = 0; |
|
|
362 | } |
|
|
363 | |
|
|
364 | return 0; |
|
|
365 | } |
|
|
366 | |
|
|
367 | static int |
|
|
368 | run_file (const char *fname) |
|
|
369 | { |
|
|
370 | int fd, fd2; |
|
|
371 | int rc; |
|
|
372 | struct stat mystat; |
|
|
373 | char oname[PATH_MAX + 1]; |
|
|
374 | |
|
|
375 | if (mode != lzcat) |
|
|
376 | if (compose_name (fname, oname)) |
|
|
377 | return -1; |
|
|
378 | |
|
|
379 | rc = lstat (fname, &mystat); |
|
|
380 | fd = open (fname, O_RDONLY); |
|
|
381 | if (rc || fd == -1) |
|
|
382 | { |
|
|
383 | fprintf (stderr, "%s: %s: ", imagename, fname); |
|
|
384 | perror (""); |
|
|
385 | return -1; |
|
|
386 | } |
|
|
387 | |
|
|
388 | if (!S_ISREG (mystat.st_mode)) |
|
|
389 | { |
|
|
390 | fprintf (stderr, "%s: %s: not a regular file.\n", imagename, fname); |
|
|
391 | close (fd); |
|
|
392 | return -1; |
|
|
393 | } |
|
|
394 | |
|
|
395 | if (mode == lzcat) |
|
|
396 | { |
|
|
397 | rc = uncompress_fd (fd, 1); |
|
|
398 | close (fd); |
|
|
399 | return rc; |
|
|
400 | } |
|
|
401 | |
|
|
402 | fd2 = open_out (oname); |
|
|
403 | if (fd2 == -1) |
|
|
404 | { |
|
|
405 | fprintf (stderr, "%s: %s: ", imagename, oname); |
|
|
406 | perror (""); |
|
|
407 | close (fd); |
|
|
408 | return -1; |
|
|
409 | } |
|
|
410 | |
|
|
411 | if (mode == compress) |
|
|
412 | { |
|
|
413 | rc = compress_fd (fd, fd2); |
|
|
414 | if (!rc && verbose) |
|
|
415 | fprintf (stderr, "%s: %5.1f%% -- replaced with %s\n", |
|
|
416 | fname, nr_read == 0 ? 0 : 100.0 - nr_written / ((double) nr_read / 100.0), oname); |
|
|
417 | } |
|
|
418 | else |
|
|
419 | { |
|
|
420 | rc = uncompress_fd (fd, fd2); |
|
|
421 | if (!rc && verbose) |
|
|
422 | fprintf (stderr, "%s: %5.1f%% -- replaced with %s\n", |
|
|
423 | fname, nr_written == 0 ? 0 : 100.0 - nr_read / ((double) nr_written / 100.0), oname); |
|
|
424 | } |
|
|
425 | |
|
|
426 | fchmod (fd2, mystat.st_mode); |
|
|
427 | close (fd); |
|
|
428 | close (fd2); |
|
|
429 | |
|
|
430 | if (!rc) |
|
|
431 | unlink (fname); |
|
|
432 | |
|
|
433 | return rc; |
198 | } |
434 | } |
199 | |
435 | |
200 | int |
436 | int |
201 | main (int argc, char *argv[]) |
437 | main (int argc, char *argv[]) |
202 | { |
438 | { |
|
|
439 | char *p = argv[0]; |
203 | int c; |
440 | int optc; |
204 | unsigned int blocksize = 64*1024-1; |
441 | int rc = 0; |
205 | enum { m_compress, m_decompress } mode = m_compress; |
|
|
206 | |
442 | |
|
|
443 | errno = 0; |
|
|
444 | p = getenv ("LZF_BLOCKSIZE"); |
|
|
445 | if (p) |
|
|
446 | { |
|
|
447 | blocksize = strtoul (p, 0, 0); |
|
|
448 | if (errno || !blocksize || blocksize > MAX_BLOCKSIZE) |
|
|
449 | blocksize = BLOCKSIZE; |
|
|
450 | } |
|
|
451 | |
|
|
452 | p = strrchr (argv[0], '/'); |
|
|
453 | imagename = p ? ++p : argv[0]; |
|
|
454 | |
|
|
455 | if (!strncmp (imagename, "un", 2) || !strncmp (imagename, "de", 2)) |
|
|
456 | mode = uncompress; |
|
|
457 | |
|
|
458 | if (strstr (imagename, "cat")) |
|
|
459 | mode = lzcat; |
|
|
460 | |
|
|
461 | #ifdef HAVE_GETOPT_LONG |
|
|
462 | while ((optc = getopt_long (argc, argv, "cdfhvb:", longopts, 0)) != -1) |
|
|
463 | #else |
207 | while ((c = getopt (argc, argv, "cdb:h")) != -1) |
464 | while ((optc = getopt (argc, argv, "cdfhvb:")) != -1) |
|
|
465 | #endif |
|
|
466 | { |
208 | switch (c) |
467 | switch (optc) |
209 | { |
468 | { |
210 | case 'c': |
469 | case 'c': |
211 | mode = m_compress; |
470 | mode = compress; |
212 | break; |
471 | break; |
213 | |
|
|
214 | case 'd': |
472 | case 'd': |
215 | mode = m_decompress; |
473 | mode = uncompress; |
216 | break; |
474 | break; |
217 | |
|
|
218 | case 'b': |
475 | case 'f': |
219 | blocksize = atol (optarg); |
476 | force = 1; |
220 | break; |
477 | break; |
221 | |
|
|
222 | case 'h': |
478 | case 'h': |
223 | usage (0); |
479 | usage (0); |
224 | |
480 | break; |
225 | case ':': |
|
|
226 | fprintf (stderr, "required argument missing, use -h\n"); |
|
|
227 | exit (1); |
|
|
228 | |
|
|
229 | case '?': |
481 | case 'v': |
230 | fprintf (stderr, "unknown option, use -h\n"); |
482 | verbose = 1; |
231 | exit (1); |
483 | break; |
232 | |
484 | case 'b': |
|
|
485 | errno = 0; |
|
|
486 | blocksize = strtoul (optarg, 0, 0); |
|
|
487 | if (errno || !blocksize || blocksize > MAX_BLOCKSIZE) |
|
|
488 | blocksize = BLOCKSIZE; |
|
|
489 | break; |
233 | default: |
490 | default: |
234 | usage (1); |
491 | usage (1); |
|
|
492 | break; |
235 | } |
493 | } |
|
|
494 | } |
236 | |
495 | |
|
|
496 | if (optind == argc) |
|
|
497 | { // stdin stdout |
|
|
498 | if (!force) |
|
|
499 | { |
|
|
500 | if ((mode == uncompress || mode == lzcat) && isatty (0)) |
|
|
501 | { |
|
|
502 | fprintf (stderr, "%s: compressed data not read from a terminal. Use -f to force decompression.\n", imagename); |
|
|
503 | exit (1); |
|
|
504 | } |
|
|
505 | if (mode == compress && isatty (1)) |
|
|
506 | { |
|
|
507 | fprintf (stderr, "%s: compressed data not written to a terminal. Use -f to force compression.\n", imagename); |
|
|
508 | exit (1); |
|
|
509 | } |
|
|
510 | } |
|
|
511 | |
237 | if (mode == m_compress) |
512 | if (mode == compress) |
238 | compress (blocksize); |
513 | rc = compress_fd (0, 1); |
239 | else if (mode == m_decompress) |
|
|
240 | decompress (); |
|
|
241 | else |
514 | else |
242 | abort (); |
515 | rc = uncompress_fd (0, 1); |
243 | |
516 | |
244 | return 0; |
517 | exit (rc ? 1 : 0); |
|
|
518 | } |
|
|
519 | |
|
|
520 | while (optind < argc) |
|
|
521 | rc |= run_file (argv[optind++]); |
|
|
522 | |
|
|
523 | exit (rc ? 1 : 0); |
245 | } |
524 | } |
|
|
525 | |