Deleted Added
full compact
bzip2.c (90067) bzip2.c (146293)
1
2/*-----------------------------------------------------------*/
3/*--- A block-sorting, lossless compressor bzip2.c ---*/
4/*-----------------------------------------------------------*/
5
6/*--
7 This file is a part of bzip2 and/or libbzip2, a program and
8 library for lossless, block-sorting data compression.
9
1
2/*-----------------------------------------------------------*/
3/*--- A block-sorting, lossless compressor bzip2.c ---*/
4/*-----------------------------------------------------------*/
5
6/*--
7 This file is a part of bzip2 and/or libbzip2, a program and
8 library for lossless, block-sorting data compression.
9
10 Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
10 Copyright (C) 1996-2005 Julian R Seward. All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
14 are met:
15
16 1. Redistributions of source code must retain the above copyright
17 notice, this list of conditions and the following disclaimer.
18
19 2. The origin of this software must not be misrepresented; you must
20 not claim that you wrote the original software. If you use this
21 software in a product, an acknowledgment in the product
22 documentation would be appreciated but is not required.
23
24 3. Altered source versions must be plainly marked as such, and must
25 not be misrepresented as being the original software.
26
27 4. The name of the author may not be used to endorse or promote
28 products derived from this software without specific prior written
29 permission.
30
31 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
32 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
35 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
43 Julian Seward, Cambridge, UK.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
14 are met:
15
16 1. Redistributions of source code must retain the above copyright
17 notice, this list of conditions and the following disclaimer.
18
19 2. The origin of this software must not be misrepresented; you must
20 not claim that you wrote the original software. If you use this
21 software in a product, an acknowledgment in the product
22 documentation would be appreciated but is not required.
23
24 3. Altered source versions must be plainly marked as such, and must
25 not be misrepresented as being the original software.
26
27 4. The name of the author may not be used to endorse or promote
28 products derived from this software without specific prior written
29 permission.
30
31 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
32 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
35 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
43 Julian Seward, Cambridge, UK.
44 jseward@acm.org
44 jseward@bzip.org
45 bzip2/libbzip2 version 1.0 of 21 March 2000
46
47 This program is based on (at least) the work of:
48 Mike Burrows
49 David Wheeler
50 Peter Fenwick
51 Alistair Moffat
52 Radford Neal
53 Ian H. Witten
54 Robert Sedgewick
55 Jon L. Bentley
56
57 For more information on these sources, see the manual.
58--*/
59
60
61/*----------------------------------------------------*/
62/*--- IMPORTANT ---*/
63/*----------------------------------------------------*/
64
65/*--
66 WARNING:
67 This program and library (attempts to) compress data by
68 performing several non-trivial transformations on it.
69 Unless you are 100% familiar with *all* the algorithms
70 contained herein, and with the consequences of modifying them,
71 you should NOT meddle with the compression or decompression
72 machinery. Incorrect changes can and very likely *will*
73 lead to disasterous loss of data.
74
75 DISCLAIMER:
76 I TAKE NO RESPONSIBILITY FOR ANY LOSS OF DATA ARISING FROM THE
77 USE OF THIS PROGRAM, HOWSOEVER CAUSED.
78
79 Every compression of a file implies an assumption that the
80 compressed file can be decompressed to reproduce the original.
81 Great efforts in design, coding and testing have been made to
82 ensure that this program works correctly. However, the
83 complexity of the algorithms, and, in particular, the presence
84 of various special cases in the code which occur with very low
85 but non-zero probability make it impossible to rule out the
86 possibility of bugs remaining in the program. DO NOT COMPRESS
87 ANY DATA WITH THIS PROGRAM AND/OR LIBRARY UNLESS YOU ARE PREPARED
88 TO ACCEPT THE POSSIBILITY, HOWEVER SMALL, THAT THE DATA WILL
89 NOT BE RECOVERABLE.
90
91 That is not to say this program is inherently unreliable.
92 Indeed, I very much hope the opposite is true. bzip2/libbzip2
93 has been carefully constructed and extensively tested.
94
95 PATENTS:
96 To the best of my knowledge, bzip2/libbzip2 does not use any
97 patented algorithms. However, I do not have the resources
98 available to carry out a full patent search. Therefore I cannot
99 give any guarantee of the above statement.
100--*/
101
102
103
104/*----------------------------------------------------*/
105/*--- and now for something much more pleasant :-) ---*/
106/*----------------------------------------------------*/
107
108/*---------------------------------------------*/
109/*--
110 Place a 1 beside your platform, and 0 elsewhere.
111--*/
112
113/*--
114 Generic 32-bit Unix.
115 Also works on 64-bit Unix boxes.
116 This is the default.
117--*/
118#define BZ_UNIX 1
119
120/*--
121 Win32, as seen by Jacob Navia's excellent
122 port of (Chris Fraser & David Hanson)'s excellent
123 lcc compiler. Or with MS Visual C.
124 This is selected automatically if compiled by a compiler which
125 defines _WIN32, not including the Cygwin GCC.
126--*/
127#define BZ_LCCWIN32 0
128
129#if defined(_WIN32) && !defined(__CYGWIN__)
130#undef BZ_LCCWIN32
131#define BZ_LCCWIN32 1
132#undef BZ_UNIX
133#define BZ_UNIX 0
134#endif
135
136
137/*---------------------------------------------*/
138/*--
139 Some stuff for all platforms.
140--*/
141
142#include <stdio.h>
143#include <stdlib.h>
144#include <string.h>
145#include <signal.h>
146#include <math.h>
147#include <errno.h>
148#include <ctype.h>
149#include "bzlib.h"
150
151#define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); }
152#define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) ioError(); }
153#define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) ioError(); }
154
155
156/*---------------------------------------------*/
157/*--
158 Platform-specific stuff.
159--*/
160
161#if BZ_UNIX
162# include <fcntl.h>
163# include <sys/types.h>
164# include <utime.h>
165# include <unistd.h>
166# include <sys/stat.h>
167# include <sys/times.h>
168
169# define PATH_SEP '/'
170# define MY_LSTAT lstat
171# define MY_STAT stat
172# define MY_S_ISREG S_ISREG
173# define MY_S_ISDIR S_ISDIR
174
175# define APPEND_FILESPEC(root, name) \
176 root=snocString((root), (name))
177
178# define APPEND_FLAG(root, name) \
179 root=snocString((root), (name))
180
181# define SET_BINARY_MODE(fd) /**/
182
183# ifdef __GNUC__
184# define NORETURN __attribute__ ((noreturn))
185# else
186# define NORETURN /**/
187# endif
188
189# ifdef __DJGPP__
190# include <io.h>
191# include <fcntl.h>
192# undef MY_LSTAT
193# undef MY_STAT
194# define MY_LSTAT stat
195# define MY_STAT stat
196# undef SET_BINARY_MODE
197# define SET_BINARY_MODE(fd) \
198 do { \
199 int retVal = setmode ( fileno ( fd ), \
200 O_BINARY ); \
201 ERROR_IF_MINUS_ONE ( retVal ); \
202 } while ( 0 )
203# endif
204
205# ifdef __CYGWIN__
206# include <io.h>
207# include <fcntl.h>
208# undef SET_BINARY_MODE
209# define SET_BINARY_MODE(fd) \
210 do { \
211 int retVal = setmode ( fileno ( fd ), \
212 O_BINARY ); \
213 ERROR_IF_MINUS_ONE ( retVal ); \
214 } while ( 0 )
215# endif
216#endif /* BZ_UNIX */
217
218
219
220#if BZ_LCCWIN32
221# include <io.h>
222# include <fcntl.h>
223# include <sys\stat.h>
224
225# define NORETURN /**/
226# define PATH_SEP '\\'
227# define MY_LSTAT _stat
228# define MY_STAT _stat
229# define MY_S_ISREG(x) ((x) & _S_IFREG)
230# define MY_S_ISDIR(x) ((x) & _S_IFDIR)
231
232# define APPEND_FLAG(root, name) \
233 root=snocString((root), (name))
234
235# define APPEND_FILESPEC(root, name) \
236 root = snocString ((root), (name))
237
238# define SET_BINARY_MODE(fd) \
239 do { \
240 int retVal = setmode ( fileno ( fd ), \
241 O_BINARY ); \
242 ERROR_IF_MINUS_ONE ( retVal ); \
243 } while ( 0 )
244
245#endif /* BZ_LCCWIN32 */
246
247
248/*---------------------------------------------*/
249/*--
250 Some more stuff for all platforms :-)
251--*/
252
253typedef char Char;
254typedef unsigned char Bool;
255typedef unsigned char UChar;
256typedef int Int32;
257typedef unsigned int UInt32;
258typedef short Int16;
259typedef unsigned short UInt16;
260
261#define True ((Bool)1)
262#define False ((Bool)0)
263
264/*--
265 IntNative is your platform's `native' int size.
266 Only here to avoid probs with 64-bit platforms.
267--*/
268typedef int IntNative;
269
270
271/*---------------------------------------------------*/
272/*--- Misc (file handling) data decls ---*/
273/*---------------------------------------------------*/
274
275Int32 verbosity;
276Bool keepInputFiles, smallMode, deleteOutputOnInterrupt;
277Bool forceOverwrite, testFailsExist, unzFailsExist, noisy;
278Int32 numFileNames, numFilesProcessed, blockSize100k;
279Int32 exitValue;
280
281/*-- source modes; F==file, I==stdin, O==stdout --*/
282#define SM_I2O 1
283#define SM_F2O 2
284#define SM_F2F 3
285
286/*-- operation modes --*/
287#define OM_Z 1
288#define OM_UNZ 2
289#define OM_TEST 3
290
291Int32 opMode;
292Int32 srcMode;
293
294#define FILE_NAME_LEN 1034
295
296Int32 longestFileName;
297Char inName [FILE_NAME_LEN];
298Char outName[FILE_NAME_LEN];
299Char tmpName[FILE_NAME_LEN];
300Char *progName;
301Char progNameReally[FILE_NAME_LEN];
302FILE *outputHandleJustInCase;
303Int32 workFactor;
304
305static void panic ( Char* ) NORETURN;
306static void ioError ( void ) NORETURN;
307static void outOfMemory ( void ) NORETURN;
308static void configError ( void ) NORETURN;
309static void crcError ( void ) NORETURN;
310static void cleanUpAndFail ( Int32 ) NORETURN;
311static void compressedStreamEOF ( void ) NORETURN;
312
313static void copyFileName ( Char*, Char* );
314static void* myMalloc ( Int32 );
315
316
317
318/*---------------------------------------------------*/
319/*--- An implementation of 64-bit ints. Sigh. ---*/
320/*--- Roll on widespread deployment of ANSI C9X ! ---*/
321/*---------------------------------------------------*/
322
323typedef
324 struct { UChar b[8]; }
325 UInt64;
326
327
328static
329void uInt64_from_UInt32s ( UInt64* n, UInt32 lo32, UInt32 hi32 )
330{
331 n->b[7] = (UChar)((hi32 >> 24) & 0xFF);
332 n->b[6] = (UChar)((hi32 >> 16) & 0xFF);
333 n->b[5] = (UChar)((hi32 >> 8) & 0xFF);
334 n->b[4] = (UChar) (hi32 & 0xFF);
335 n->b[3] = (UChar)((lo32 >> 24) & 0xFF);
336 n->b[2] = (UChar)((lo32 >> 16) & 0xFF);
337 n->b[1] = (UChar)((lo32 >> 8) & 0xFF);
338 n->b[0] = (UChar) (lo32 & 0xFF);
339}
340
341
342static
343double uInt64_to_double ( UInt64* n )
344{
345 Int32 i;
346 double base = 1.0;
347 double sum = 0.0;
348 for (i = 0; i < 8; i++) {
349 sum += base * (double)(n->b[i]);
350 base *= 256.0;
351 }
352 return sum;
353}
354
355
356static
357Bool uInt64_isZero ( UInt64* n )
358{
359 Int32 i;
360 for (i = 0; i < 8; i++)
361 if (n->b[i] != 0) return 0;
362 return 1;
363}
364
365
366/* Divide *n by 10, and return the remainder. */
367static
368Int32 uInt64_qrm10 ( UInt64* n )
369{
370 UInt32 rem, tmp;
371 Int32 i;
372 rem = 0;
373 for (i = 7; i >= 0; i--) {
374 tmp = rem * 256 + n->b[i];
375 n->b[i] = tmp / 10;
376 rem = tmp % 10;
377 }
378 return rem;
379}
380
381
382/* ... and the Whole Entire Point of all this UInt64 stuff is
383 so that we can supply the following function.
384*/
385static
386void uInt64_toAscii ( char* outbuf, UInt64* n )
387{
388 Int32 i, q;
389 UChar buf[32];
390 Int32 nBuf = 0;
391 UInt64 n_copy = *n;
392 do {
393 q = uInt64_qrm10 ( &n_copy );
394 buf[nBuf] = q + '0';
395 nBuf++;
396 } while (!uInt64_isZero(&n_copy));
397 outbuf[nBuf] = 0;
398 for (i = 0; i < nBuf; i++)
399 outbuf[i] = buf[nBuf-i-1];
400}
401
402
403/*---------------------------------------------------*/
404/*--- Processing of complete files and streams ---*/
405/*---------------------------------------------------*/
406
407/*---------------------------------------------*/
408static
409Bool myfeof ( FILE* f )
410{
411 Int32 c = fgetc ( f );
412 if (c == EOF) return True;
413 ungetc ( c, f );
414 return False;
415}
416
417
418/*---------------------------------------------*/
419static
420void compressStream ( FILE *stream, FILE *zStream )
421{
422 BZFILE* bzf = NULL;
423 UChar ibuf[5000];
424 Int32 nIbuf;
425 UInt32 nbytes_in_lo32, nbytes_in_hi32;
426 UInt32 nbytes_out_lo32, nbytes_out_hi32;
427 Int32 bzerr, bzerr_dummy, ret;
428
429 SET_BINARY_MODE(stream);
430 SET_BINARY_MODE(zStream);
431
432 if (ferror(stream)) goto errhandler_io;
433 if (ferror(zStream)) goto errhandler_io;
434
435 bzf = BZ2_bzWriteOpen ( &bzerr, zStream,
436 blockSize100k, verbosity, workFactor );
437 if (bzerr != BZ_OK) goto errhandler;
438
439 if (verbosity >= 2) fprintf ( stderr, "\n" );
440
441 while (True) {
442
443 if (myfeof(stream)) break;
444 nIbuf = fread ( ibuf, sizeof(UChar), 5000, stream );
445 if (ferror(stream)) goto errhandler_io;
446 if (nIbuf > 0) BZ2_bzWrite ( &bzerr, bzf, (void*)ibuf, nIbuf );
447 if (bzerr != BZ_OK) goto errhandler;
448
449 }
450
451 BZ2_bzWriteClose64 ( &bzerr, bzf, 0,
452 &nbytes_in_lo32, &nbytes_in_hi32,
453 &nbytes_out_lo32, &nbytes_out_hi32 );
454 if (bzerr != BZ_OK) goto errhandler;
455
456 if (ferror(zStream)) goto errhandler_io;
457 ret = fflush ( zStream );
458 if (ret == EOF) goto errhandler_io;
459 if (zStream != stdout) {
460 ret = fclose ( zStream );
461 outputHandleJustInCase = NULL;
462 if (ret == EOF) goto errhandler_io;
463 }
464 outputHandleJustInCase = NULL;
465 if (ferror(stream)) goto errhandler_io;
466 ret = fclose ( stream );
467 if (ret == EOF) goto errhandler_io;
468
469 if (verbosity >= 1) {
470 if (nbytes_in_lo32 == 0 && nbytes_in_hi32 == 0) {
471 fprintf ( stderr, " no data compressed.\n");
472 } else {
473 Char buf_nin[32], buf_nout[32];
474 UInt64 nbytes_in, nbytes_out;
475 double nbytes_in_d, nbytes_out_d;
476 uInt64_from_UInt32s ( &nbytes_in,
477 nbytes_in_lo32, nbytes_in_hi32 );
478 uInt64_from_UInt32s ( &nbytes_out,
479 nbytes_out_lo32, nbytes_out_hi32 );
480 nbytes_in_d = uInt64_to_double ( &nbytes_in );
481 nbytes_out_d = uInt64_to_double ( &nbytes_out );
482 uInt64_toAscii ( buf_nin, &nbytes_in );
483 uInt64_toAscii ( buf_nout, &nbytes_out );
484 fprintf ( stderr, "%6.3f:1, %6.3f bits/byte, "
485 "%5.2f%% saved, %s in, %s out.\n",
486 nbytes_in_d / nbytes_out_d,
487 (8.0 * nbytes_out_d) / nbytes_in_d,
488 100.0 * (1.0 - nbytes_out_d / nbytes_in_d),
489 buf_nin,
490 buf_nout
491 );
492 }
493 }
494
495 return;
496
497 errhandler:
498 BZ2_bzWriteClose64 ( &bzerr_dummy, bzf, 1,
499 &nbytes_in_lo32, &nbytes_in_hi32,
500 &nbytes_out_lo32, &nbytes_out_hi32 );
501 switch (bzerr) {
502 case BZ_CONFIG_ERROR:
503 configError(); break;
504 case BZ_MEM_ERROR:
505 outOfMemory (); break;
506 case BZ_IO_ERROR:
507 errhandler_io:
508 ioError(); break;
509 default:
510 panic ( "compress:unexpected error" );
511 }
512
513 panic ( "compress:end" );
514 /*notreached*/
515}
516
517
518
519/*---------------------------------------------*/
520static
521Bool uncompressStream ( FILE *zStream, FILE *stream )
522{
523 BZFILE* bzf = NULL;
524 Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i;
525 UChar obuf[5000];
526 UChar unused[BZ_MAX_UNUSED];
527 Int32 nUnused;
45 bzip2/libbzip2 version 1.0 of 21 March 2000
46
47 This program is based on (at least) the work of:
48 Mike Burrows
49 David Wheeler
50 Peter Fenwick
51 Alistair Moffat
52 Radford Neal
53 Ian H. Witten
54 Robert Sedgewick
55 Jon L. Bentley
56
57 For more information on these sources, see the manual.
58--*/
59
60
61/*----------------------------------------------------*/
62/*--- IMPORTANT ---*/
63/*----------------------------------------------------*/
64
65/*--
66 WARNING:
67 This program and library (attempts to) compress data by
68 performing several non-trivial transformations on it.
69 Unless you are 100% familiar with *all* the algorithms
70 contained herein, and with the consequences of modifying them,
71 you should NOT meddle with the compression or decompression
72 machinery. Incorrect changes can and very likely *will*
73 lead to disasterous loss of data.
74
75 DISCLAIMER:
76 I TAKE NO RESPONSIBILITY FOR ANY LOSS OF DATA ARISING FROM THE
77 USE OF THIS PROGRAM, HOWSOEVER CAUSED.
78
79 Every compression of a file implies an assumption that the
80 compressed file can be decompressed to reproduce the original.
81 Great efforts in design, coding and testing have been made to
82 ensure that this program works correctly. However, the
83 complexity of the algorithms, and, in particular, the presence
84 of various special cases in the code which occur with very low
85 but non-zero probability make it impossible to rule out the
86 possibility of bugs remaining in the program. DO NOT COMPRESS
87 ANY DATA WITH THIS PROGRAM AND/OR LIBRARY UNLESS YOU ARE PREPARED
88 TO ACCEPT THE POSSIBILITY, HOWEVER SMALL, THAT THE DATA WILL
89 NOT BE RECOVERABLE.
90
91 That is not to say this program is inherently unreliable.
92 Indeed, I very much hope the opposite is true. bzip2/libbzip2
93 has been carefully constructed and extensively tested.
94
95 PATENTS:
96 To the best of my knowledge, bzip2/libbzip2 does not use any
97 patented algorithms. However, I do not have the resources
98 available to carry out a full patent search. Therefore I cannot
99 give any guarantee of the above statement.
100--*/
101
102
103
104/*----------------------------------------------------*/
105/*--- and now for something much more pleasant :-) ---*/
106/*----------------------------------------------------*/
107
108/*---------------------------------------------*/
109/*--
110 Place a 1 beside your platform, and 0 elsewhere.
111--*/
112
113/*--
114 Generic 32-bit Unix.
115 Also works on 64-bit Unix boxes.
116 This is the default.
117--*/
118#define BZ_UNIX 1
119
120/*--
121 Win32, as seen by Jacob Navia's excellent
122 port of (Chris Fraser & David Hanson)'s excellent
123 lcc compiler. Or with MS Visual C.
124 This is selected automatically if compiled by a compiler which
125 defines _WIN32, not including the Cygwin GCC.
126--*/
127#define BZ_LCCWIN32 0
128
129#if defined(_WIN32) && !defined(__CYGWIN__)
130#undef BZ_LCCWIN32
131#define BZ_LCCWIN32 1
132#undef BZ_UNIX
133#define BZ_UNIX 0
134#endif
135
136
137/*---------------------------------------------*/
138/*--
139 Some stuff for all platforms.
140--*/
141
142#include <stdio.h>
143#include <stdlib.h>
144#include <string.h>
145#include <signal.h>
146#include <math.h>
147#include <errno.h>
148#include <ctype.h>
149#include "bzlib.h"
150
151#define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); }
152#define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) ioError(); }
153#define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) ioError(); }
154
155
156/*---------------------------------------------*/
157/*--
158 Platform-specific stuff.
159--*/
160
161#if BZ_UNIX
162# include <fcntl.h>
163# include <sys/types.h>
164# include <utime.h>
165# include <unistd.h>
166# include <sys/stat.h>
167# include <sys/times.h>
168
169# define PATH_SEP '/'
170# define MY_LSTAT lstat
171# define MY_STAT stat
172# define MY_S_ISREG S_ISREG
173# define MY_S_ISDIR S_ISDIR
174
175# define APPEND_FILESPEC(root, name) \
176 root=snocString((root), (name))
177
178# define APPEND_FLAG(root, name) \
179 root=snocString((root), (name))
180
181# define SET_BINARY_MODE(fd) /**/
182
183# ifdef __GNUC__
184# define NORETURN __attribute__ ((noreturn))
185# else
186# define NORETURN /**/
187# endif
188
189# ifdef __DJGPP__
190# include <io.h>
191# include <fcntl.h>
192# undef MY_LSTAT
193# undef MY_STAT
194# define MY_LSTAT stat
195# define MY_STAT stat
196# undef SET_BINARY_MODE
197# define SET_BINARY_MODE(fd) \
198 do { \
199 int retVal = setmode ( fileno ( fd ), \
200 O_BINARY ); \
201 ERROR_IF_MINUS_ONE ( retVal ); \
202 } while ( 0 )
203# endif
204
205# ifdef __CYGWIN__
206# include <io.h>
207# include <fcntl.h>
208# undef SET_BINARY_MODE
209# define SET_BINARY_MODE(fd) \
210 do { \
211 int retVal = setmode ( fileno ( fd ), \
212 O_BINARY ); \
213 ERROR_IF_MINUS_ONE ( retVal ); \
214 } while ( 0 )
215# endif
216#endif /* BZ_UNIX */
217
218
219
220#if BZ_LCCWIN32
221# include <io.h>
222# include <fcntl.h>
223# include <sys\stat.h>
224
225# define NORETURN /**/
226# define PATH_SEP '\\'
227# define MY_LSTAT _stat
228# define MY_STAT _stat
229# define MY_S_ISREG(x) ((x) & _S_IFREG)
230# define MY_S_ISDIR(x) ((x) & _S_IFDIR)
231
232# define APPEND_FLAG(root, name) \
233 root=snocString((root), (name))
234
235# define APPEND_FILESPEC(root, name) \
236 root = snocString ((root), (name))
237
238# define SET_BINARY_MODE(fd) \
239 do { \
240 int retVal = setmode ( fileno ( fd ), \
241 O_BINARY ); \
242 ERROR_IF_MINUS_ONE ( retVal ); \
243 } while ( 0 )
244
245#endif /* BZ_LCCWIN32 */
246
247
248/*---------------------------------------------*/
249/*--
250 Some more stuff for all platforms :-)
251--*/
252
253typedef char Char;
254typedef unsigned char Bool;
255typedef unsigned char UChar;
256typedef int Int32;
257typedef unsigned int UInt32;
258typedef short Int16;
259typedef unsigned short UInt16;
260
261#define True ((Bool)1)
262#define False ((Bool)0)
263
264/*--
265 IntNative is your platform's `native' int size.
266 Only here to avoid probs with 64-bit platforms.
267--*/
268typedef int IntNative;
269
270
271/*---------------------------------------------------*/
272/*--- Misc (file handling) data decls ---*/
273/*---------------------------------------------------*/
274
275Int32 verbosity;
276Bool keepInputFiles, smallMode, deleteOutputOnInterrupt;
277Bool forceOverwrite, testFailsExist, unzFailsExist, noisy;
278Int32 numFileNames, numFilesProcessed, blockSize100k;
279Int32 exitValue;
280
281/*-- source modes; F==file, I==stdin, O==stdout --*/
282#define SM_I2O 1
283#define SM_F2O 2
284#define SM_F2F 3
285
286/*-- operation modes --*/
287#define OM_Z 1
288#define OM_UNZ 2
289#define OM_TEST 3
290
291Int32 opMode;
292Int32 srcMode;
293
294#define FILE_NAME_LEN 1034
295
296Int32 longestFileName;
297Char inName [FILE_NAME_LEN];
298Char outName[FILE_NAME_LEN];
299Char tmpName[FILE_NAME_LEN];
300Char *progName;
301Char progNameReally[FILE_NAME_LEN];
302FILE *outputHandleJustInCase;
303Int32 workFactor;
304
305static void panic ( Char* ) NORETURN;
306static void ioError ( void ) NORETURN;
307static void outOfMemory ( void ) NORETURN;
308static void configError ( void ) NORETURN;
309static void crcError ( void ) NORETURN;
310static void cleanUpAndFail ( Int32 ) NORETURN;
311static void compressedStreamEOF ( void ) NORETURN;
312
313static void copyFileName ( Char*, Char* );
314static void* myMalloc ( Int32 );
315
316
317
318/*---------------------------------------------------*/
319/*--- An implementation of 64-bit ints. Sigh. ---*/
320/*--- Roll on widespread deployment of ANSI C9X ! ---*/
321/*---------------------------------------------------*/
322
323typedef
324 struct { UChar b[8]; }
325 UInt64;
326
327
328static
329void uInt64_from_UInt32s ( UInt64* n, UInt32 lo32, UInt32 hi32 )
330{
331 n->b[7] = (UChar)((hi32 >> 24) & 0xFF);
332 n->b[6] = (UChar)((hi32 >> 16) & 0xFF);
333 n->b[5] = (UChar)((hi32 >> 8) & 0xFF);
334 n->b[4] = (UChar) (hi32 & 0xFF);
335 n->b[3] = (UChar)((lo32 >> 24) & 0xFF);
336 n->b[2] = (UChar)((lo32 >> 16) & 0xFF);
337 n->b[1] = (UChar)((lo32 >> 8) & 0xFF);
338 n->b[0] = (UChar) (lo32 & 0xFF);
339}
340
341
342static
343double uInt64_to_double ( UInt64* n )
344{
345 Int32 i;
346 double base = 1.0;
347 double sum = 0.0;
348 for (i = 0; i < 8; i++) {
349 sum += base * (double)(n->b[i]);
350 base *= 256.0;
351 }
352 return sum;
353}
354
355
356static
357Bool uInt64_isZero ( UInt64* n )
358{
359 Int32 i;
360 for (i = 0; i < 8; i++)
361 if (n->b[i] != 0) return 0;
362 return 1;
363}
364
365
366/* Divide *n by 10, and return the remainder. */
367static
368Int32 uInt64_qrm10 ( UInt64* n )
369{
370 UInt32 rem, tmp;
371 Int32 i;
372 rem = 0;
373 for (i = 7; i >= 0; i--) {
374 tmp = rem * 256 + n->b[i];
375 n->b[i] = tmp / 10;
376 rem = tmp % 10;
377 }
378 return rem;
379}
380
381
382/* ... and the Whole Entire Point of all this UInt64 stuff is
383 so that we can supply the following function.
384*/
385static
386void uInt64_toAscii ( char* outbuf, UInt64* n )
387{
388 Int32 i, q;
389 UChar buf[32];
390 Int32 nBuf = 0;
391 UInt64 n_copy = *n;
392 do {
393 q = uInt64_qrm10 ( &n_copy );
394 buf[nBuf] = q + '0';
395 nBuf++;
396 } while (!uInt64_isZero(&n_copy));
397 outbuf[nBuf] = 0;
398 for (i = 0; i < nBuf; i++)
399 outbuf[i] = buf[nBuf-i-1];
400}
401
402
403/*---------------------------------------------------*/
404/*--- Processing of complete files and streams ---*/
405/*---------------------------------------------------*/
406
407/*---------------------------------------------*/
408static
409Bool myfeof ( FILE* f )
410{
411 Int32 c = fgetc ( f );
412 if (c == EOF) return True;
413 ungetc ( c, f );
414 return False;
415}
416
417
418/*---------------------------------------------*/
419static
420void compressStream ( FILE *stream, FILE *zStream )
421{
422 BZFILE* bzf = NULL;
423 UChar ibuf[5000];
424 Int32 nIbuf;
425 UInt32 nbytes_in_lo32, nbytes_in_hi32;
426 UInt32 nbytes_out_lo32, nbytes_out_hi32;
427 Int32 bzerr, bzerr_dummy, ret;
428
429 SET_BINARY_MODE(stream);
430 SET_BINARY_MODE(zStream);
431
432 if (ferror(stream)) goto errhandler_io;
433 if (ferror(zStream)) goto errhandler_io;
434
435 bzf = BZ2_bzWriteOpen ( &bzerr, zStream,
436 blockSize100k, verbosity, workFactor );
437 if (bzerr != BZ_OK) goto errhandler;
438
439 if (verbosity >= 2) fprintf ( stderr, "\n" );
440
441 while (True) {
442
443 if (myfeof(stream)) break;
444 nIbuf = fread ( ibuf, sizeof(UChar), 5000, stream );
445 if (ferror(stream)) goto errhandler_io;
446 if (nIbuf > 0) BZ2_bzWrite ( &bzerr, bzf, (void*)ibuf, nIbuf );
447 if (bzerr != BZ_OK) goto errhandler;
448
449 }
450
451 BZ2_bzWriteClose64 ( &bzerr, bzf, 0,
452 &nbytes_in_lo32, &nbytes_in_hi32,
453 &nbytes_out_lo32, &nbytes_out_hi32 );
454 if (bzerr != BZ_OK) goto errhandler;
455
456 if (ferror(zStream)) goto errhandler_io;
457 ret = fflush ( zStream );
458 if (ret == EOF) goto errhandler_io;
459 if (zStream != stdout) {
460 ret = fclose ( zStream );
461 outputHandleJustInCase = NULL;
462 if (ret == EOF) goto errhandler_io;
463 }
464 outputHandleJustInCase = NULL;
465 if (ferror(stream)) goto errhandler_io;
466 ret = fclose ( stream );
467 if (ret == EOF) goto errhandler_io;
468
469 if (verbosity >= 1) {
470 if (nbytes_in_lo32 == 0 && nbytes_in_hi32 == 0) {
471 fprintf ( stderr, " no data compressed.\n");
472 } else {
473 Char buf_nin[32], buf_nout[32];
474 UInt64 nbytes_in, nbytes_out;
475 double nbytes_in_d, nbytes_out_d;
476 uInt64_from_UInt32s ( &nbytes_in,
477 nbytes_in_lo32, nbytes_in_hi32 );
478 uInt64_from_UInt32s ( &nbytes_out,
479 nbytes_out_lo32, nbytes_out_hi32 );
480 nbytes_in_d = uInt64_to_double ( &nbytes_in );
481 nbytes_out_d = uInt64_to_double ( &nbytes_out );
482 uInt64_toAscii ( buf_nin, &nbytes_in );
483 uInt64_toAscii ( buf_nout, &nbytes_out );
484 fprintf ( stderr, "%6.3f:1, %6.3f bits/byte, "
485 "%5.2f%% saved, %s in, %s out.\n",
486 nbytes_in_d / nbytes_out_d,
487 (8.0 * nbytes_out_d) / nbytes_in_d,
488 100.0 * (1.0 - nbytes_out_d / nbytes_in_d),
489 buf_nin,
490 buf_nout
491 );
492 }
493 }
494
495 return;
496
497 errhandler:
498 BZ2_bzWriteClose64 ( &bzerr_dummy, bzf, 1,
499 &nbytes_in_lo32, &nbytes_in_hi32,
500 &nbytes_out_lo32, &nbytes_out_hi32 );
501 switch (bzerr) {
502 case BZ_CONFIG_ERROR:
503 configError(); break;
504 case BZ_MEM_ERROR:
505 outOfMemory (); break;
506 case BZ_IO_ERROR:
507 errhandler_io:
508 ioError(); break;
509 default:
510 panic ( "compress:unexpected error" );
511 }
512
513 panic ( "compress:end" );
514 /*notreached*/
515}
516
517
518
519/*---------------------------------------------*/
520static
521Bool uncompressStream ( FILE *zStream, FILE *stream )
522{
523 BZFILE* bzf = NULL;
524 Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i;
525 UChar obuf[5000];
526 UChar unused[BZ_MAX_UNUSED];
527 Int32 nUnused;
528 void* unusedTmpV;
528 UChar* unusedTmp;
529
530 nUnused = 0;
531 streamNo = 0;
532
533 SET_BINARY_MODE(stream);
534 SET_BINARY_MODE(zStream);
535
536 if (ferror(stream)) goto errhandler_io;
537 if (ferror(zStream)) goto errhandler_io;
538
539 while (True) {
540
541 bzf = BZ2_bzReadOpen (
542 &bzerr, zStream, verbosity,
543 (int)smallMode, unused, nUnused
544 );
545 if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
546 streamNo++;
547
548 while (bzerr == BZ_OK) {
549 nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
550 if (bzerr == BZ_DATA_ERROR_MAGIC) goto trycat;
551 if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0)
552 fwrite ( obuf, sizeof(UChar), nread, stream );
553 if (ferror(stream)) goto errhandler_io;
554 }
555 if (bzerr != BZ_STREAM_END) goto errhandler;
556
529 UChar* unusedTmp;
530
531 nUnused = 0;
532 streamNo = 0;
533
534 SET_BINARY_MODE(stream);
535 SET_BINARY_MODE(zStream);
536
537 if (ferror(stream)) goto errhandler_io;
538 if (ferror(zStream)) goto errhandler_io;
539
540 while (True) {
541
542 bzf = BZ2_bzReadOpen (
543 &bzerr, zStream, verbosity,
544 (int)smallMode, unused, nUnused
545 );
546 if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
547 streamNo++;
548
549 while (bzerr == BZ_OK) {
550 nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
551 if (bzerr == BZ_DATA_ERROR_MAGIC) goto trycat;
552 if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0)
553 fwrite ( obuf, sizeof(UChar), nread, stream );
554 if (ferror(stream)) goto errhandler_io;
555 }
556 if (bzerr != BZ_STREAM_END) goto errhandler;
557
557 BZ2_bzReadGetUnused ( &bzerr, bzf, (void**)(&unusedTmp), &nUnused );
558 BZ2_bzReadGetUnused ( &bzerr, bzf, &unusedTmpV, &nUnused );
558 if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
559
559 if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
560
561 unusedTmp = (UChar*)unusedTmpV;
560 for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
561
562 BZ2_bzReadClose ( &bzerr, bzf );
563 if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
564
565 if (nUnused == 0 && myfeof(zStream)) break;
566 }
567
568 closeok:
569 if (ferror(zStream)) goto errhandler_io;
570 ret = fclose ( zStream );
571 if (ret == EOF) goto errhandler_io;
572
573 if (ferror(stream)) goto errhandler_io;
574 ret = fflush ( stream );
575 if (ret != 0) goto errhandler_io;
576 if (stream != stdout) {
577 ret = fclose ( stream );
578 outputHandleJustInCase = NULL;
579 if (ret == EOF) goto errhandler_io;
580 }
581 outputHandleJustInCase = NULL;
582 if (verbosity >= 2) fprintf ( stderr, "\n " );
583 return True;
584
585 trycat:
586 if (forceOverwrite) {
587 rewind(zStream);
588 while (True) {
589 if (myfeof(zStream)) break;
590 nread = fread ( obuf, sizeof(UChar), 5000, zStream );
591 if (ferror(zStream)) goto errhandler_io;
592 if (nread > 0) fwrite ( obuf, sizeof(UChar), nread, stream );
593 if (ferror(stream)) goto errhandler_io;
594 }
595 goto closeok;
596 }
597
598 errhandler:
599 BZ2_bzReadClose ( &bzerr_dummy, bzf );
600 switch (bzerr) {
601 case BZ_CONFIG_ERROR:
602 configError(); break;
603 case BZ_IO_ERROR:
604 errhandler_io:
605 ioError(); break;
606 case BZ_DATA_ERROR:
607 crcError();
608 case BZ_MEM_ERROR:
609 outOfMemory();
610 case BZ_UNEXPECTED_EOF:
611 compressedStreamEOF();
612 case BZ_DATA_ERROR_MAGIC:
613 if (zStream != stdin) fclose(zStream);
614 if (stream != stdout) fclose(stream);
615 if (streamNo == 1) {
616 return False;
617 } else {
618 if (noisy)
619 fprintf ( stderr,
620 "\n%s: %s: trailing garbage after EOF ignored\n",
621 progName, inName );
622 return True;
623 }
624 default:
625 panic ( "decompress:unexpected error" );
626 }
627
628 panic ( "decompress:end" );
629 return True; /*notreached*/
630}
631
632
633/*---------------------------------------------*/
634static
635Bool testStream ( FILE *zStream )
636{
637 BZFILE* bzf = NULL;
638 Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i;
639 UChar obuf[5000];
640 UChar unused[BZ_MAX_UNUSED];
641 Int32 nUnused;
562 for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
563
564 BZ2_bzReadClose ( &bzerr, bzf );
565 if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" );
566
567 if (nUnused == 0 && myfeof(zStream)) break;
568 }
569
570 closeok:
571 if (ferror(zStream)) goto errhandler_io;
572 ret = fclose ( zStream );
573 if (ret == EOF) goto errhandler_io;
574
575 if (ferror(stream)) goto errhandler_io;
576 ret = fflush ( stream );
577 if (ret != 0) goto errhandler_io;
578 if (stream != stdout) {
579 ret = fclose ( stream );
580 outputHandleJustInCase = NULL;
581 if (ret == EOF) goto errhandler_io;
582 }
583 outputHandleJustInCase = NULL;
584 if (verbosity >= 2) fprintf ( stderr, "\n " );
585 return True;
586
587 trycat:
588 if (forceOverwrite) {
589 rewind(zStream);
590 while (True) {
591 if (myfeof(zStream)) break;
592 nread = fread ( obuf, sizeof(UChar), 5000, zStream );
593 if (ferror(zStream)) goto errhandler_io;
594 if (nread > 0) fwrite ( obuf, sizeof(UChar), nread, stream );
595 if (ferror(stream)) goto errhandler_io;
596 }
597 goto closeok;
598 }
599
600 errhandler:
601 BZ2_bzReadClose ( &bzerr_dummy, bzf );
602 switch (bzerr) {
603 case BZ_CONFIG_ERROR:
604 configError(); break;
605 case BZ_IO_ERROR:
606 errhandler_io:
607 ioError(); break;
608 case BZ_DATA_ERROR:
609 crcError();
610 case BZ_MEM_ERROR:
611 outOfMemory();
612 case BZ_UNEXPECTED_EOF:
613 compressedStreamEOF();
614 case BZ_DATA_ERROR_MAGIC:
615 if (zStream != stdin) fclose(zStream);
616 if (stream != stdout) fclose(stream);
617 if (streamNo == 1) {
618 return False;
619 } else {
620 if (noisy)
621 fprintf ( stderr,
622 "\n%s: %s: trailing garbage after EOF ignored\n",
623 progName, inName );
624 return True;
625 }
626 default:
627 panic ( "decompress:unexpected error" );
628 }
629
630 panic ( "decompress:end" );
631 return True; /*notreached*/
632}
633
634
635/*---------------------------------------------*/
636static
637Bool testStream ( FILE *zStream )
638{
639 BZFILE* bzf = NULL;
640 Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i;
641 UChar obuf[5000];
642 UChar unused[BZ_MAX_UNUSED];
643 Int32 nUnused;
644 void* unusedTmpV;
642 UChar* unusedTmp;
643
644 nUnused = 0;
645 streamNo = 0;
646
647 SET_BINARY_MODE(zStream);
648 if (ferror(zStream)) goto errhandler_io;
649
650 while (True) {
651
652 bzf = BZ2_bzReadOpen (
653 &bzerr, zStream, verbosity,
654 (int)smallMode, unused, nUnused
655 );
656 if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
657 streamNo++;
658
659 while (bzerr == BZ_OK) {
660 nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
661 if (bzerr == BZ_DATA_ERROR_MAGIC) goto errhandler;
662 }
663 if (bzerr != BZ_STREAM_END) goto errhandler;
664
645 UChar* unusedTmp;
646
647 nUnused = 0;
648 streamNo = 0;
649
650 SET_BINARY_MODE(zStream);
651 if (ferror(zStream)) goto errhandler_io;
652
653 while (True) {
654
655 bzf = BZ2_bzReadOpen (
656 &bzerr, zStream, verbosity,
657 (int)smallMode, unused, nUnused
658 );
659 if (bzf == NULL || bzerr != BZ_OK) goto errhandler;
660 streamNo++;
661
662 while (bzerr == BZ_OK) {
663 nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 );
664 if (bzerr == BZ_DATA_ERROR_MAGIC) goto errhandler;
665 }
666 if (bzerr != BZ_STREAM_END) goto errhandler;
667
665 BZ2_bzReadGetUnused ( &bzerr, bzf, (void**)(&unusedTmp), &nUnused );
668 BZ2_bzReadGetUnused ( &bzerr, bzf, &unusedTmpV, &nUnused );
666 if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
667
669 if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
670
671 unusedTmp = (UChar*)unusedTmpV;
668 for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
669
670 BZ2_bzReadClose ( &bzerr, bzf );
671 if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
672 if (nUnused == 0 && myfeof(zStream)) break;
673
674 }
675
676 if (ferror(zStream)) goto errhandler_io;
677 ret = fclose ( zStream );
678 if (ret == EOF) goto errhandler_io;
679
680 if (verbosity >= 2) fprintf ( stderr, "\n " );
681 return True;
682
683 errhandler:
684 BZ2_bzReadClose ( &bzerr_dummy, bzf );
685 if (verbosity == 0)
686 fprintf ( stderr, "%s: %s: ", progName, inName );
687 switch (bzerr) {
688 case BZ_CONFIG_ERROR:
689 configError(); break;
690 case BZ_IO_ERROR:
691 errhandler_io:
692 ioError(); break;
693 case BZ_DATA_ERROR:
694 fprintf ( stderr,
695 "data integrity (CRC) error in data\n" );
696 return False;
697 case BZ_MEM_ERROR:
698 outOfMemory();
699 case BZ_UNEXPECTED_EOF:
700 fprintf ( stderr,
701 "file ends unexpectedly\n" );
702 return False;
703 case BZ_DATA_ERROR_MAGIC:
704 if (zStream != stdin) fclose(zStream);
705 if (streamNo == 1) {
706 fprintf ( stderr,
707 "bad magic number (file not created by bzip2)\n" );
708 return False;
709 } else {
710 if (noisy)
711 fprintf ( stderr,
712 "trailing garbage after EOF ignored\n" );
713 return True;
714 }
715 default:
716 panic ( "test:unexpected error" );
717 }
718
719 panic ( "test:end" );
720 return True; /*notreached*/
721}
722
723
724/*---------------------------------------------------*/
725/*--- Error [non-] handling grunge ---*/
726/*---------------------------------------------------*/
727
728/*---------------------------------------------*/
729static
730void setExit ( Int32 v )
731{
732 if (v > exitValue) exitValue = v;
733}
734
735
736/*---------------------------------------------*/
737static
738void cadvise ( void )
739{
740 if (noisy)
741 fprintf (
742 stderr,
743 "\nIt is possible that the compressed file(s) have become corrupted.\n"
744 "You can use the -tvv option to test integrity of such files.\n\n"
745 "You can use the `bzip2recover' program to attempt to recover\n"
746 "data from undamaged sections of corrupted files.\n\n"
747 );
748}
749
750
751/*---------------------------------------------*/
752static
753void showFileNames ( void )
754{
755 if (noisy)
756 fprintf (
757 stderr,
758 "\tInput file = %s, output file = %s\n",
759 inName, outName
760 );
761}
762
763
764/*---------------------------------------------*/
765static
766void cleanUpAndFail ( Int32 ec )
767{
768 IntNative retVal;
769 struct MY_STAT statBuf;
770
771 if ( srcMode == SM_F2F
772 && opMode != OM_TEST
773 && deleteOutputOnInterrupt ) {
774
775 /* Check whether input file still exists. Delete output file
776 only if input exists to avoid loss of data. Joerg Prante, 5
777 January 2002. (JRS 06-Jan-2002: other changes in 1.0.2 mean
778 this is less likely to happen. But to be ultra-paranoid, we
779 do the check anyway.) */
780 retVal = MY_STAT ( inName, &statBuf );
781 if (retVal == 0) {
782 if (noisy)
783 fprintf ( stderr,
784 "%s: Deleting output file %s, if it exists.\n",
785 progName, outName );
786 if (outputHandleJustInCase != NULL)
787 fclose ( outputHandleJustInCase );
788 retVal = remove ( outName );
789 if (retVal != 0)
790 fprintf ( stderr,
791 "%s: WARNING: deletion of output file "
792 "(apparently) failed.\n",
793 progName );
794 } else {
795 fprintf ( stderr,
796 "%s: WARNING: deletion of output file suppressed\n",
797 progName );
798 fprintf ( stderr,
799 "%s: since input file no longer exists. Output file\n",
800 progName );
801 fprintf ( stderr,
802 "%s: `%s' may be incomplete.\n",
803 progName, outName );
804 fprintf ( stderr,
805 "%s: I suggest doing an integrity test (bzip2 -tv)"
806 " of it.\n",
807 progName );
808 }
809 }
810
811 if (noisy && numFileNames > 0 && numFilesProcessed < numFileNames) {
812 fprintf ( stderr,
813 "%s: WARNING: some files have not been processed:\n"
814 "%s: %d specified on command line, %d not processed yet.\n\n",
815 progName, progName,
816 numFileNames, numFileNames - numFilesProcessed );
817 }
818 setExit(ec);
819 exit(exitValue);
820}
821
822
823/*---------------------------------------------*/
824static
825void panic ( Char* s )
826{
827 fprintf ( stderr,
828 "\n%s: PANIC -- internal consistency error:\n"
829 "\t%s\n"
830 "\tThis is a BUG. Please report it to me at:\n"
672 for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i];
673
674 BZ2_bzReadClose ( &bzerr, bzf );
675 if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" );
676 if (nUnused == 0 && myfeof(zStream)) break;
677
678 }
679
680 if (ferror(zStream)) goto errhandler_io;
681 ret = fclose ( zStream );
682 if (ret == EOF) goto errhandler_io;
683
684 if (verbosity >= 2) fprintf ( stderr, "\n " );
685 return True;
686
687 errhandler:
688 BZ2_bzReadClose ( &bzerr_dummy, bzf );
689 if (verbosity == 0)
690 fprintf ( stderr, "%s: %s: ", progName, inName );
691 switch (bzerr) {
692 case BZ_CONFIG_ERROR:
693 configError(); break;
694 case BZ_IO_ERROR:
695 errhandler_io:
696 ioError(); break;
697 case BZ_DATA_ERROR:
698 fprintf ( stderr,
699 "data integrity (CRC) error in data\n" );
700 return False;
701 case BZ_MEM_ERROR:
702 outOfMemory();
703 case BZ_UNEXPECTED_EOF:
704 fprintf ( stderr,
705 "file ends unexpectedly\n" );
706 return False;
707 case BZ_DATA_ERROR_MAGIC:
708 if (zStream != stdin) fclose(zStream);
709 if (streamNo == 1) {
710 fprintf ( stderr,
711 "bad magic number (file not created by bzip2)\n" );
712 return False;
713 } else {
714 if (noisy)
715 fprintf ( stderr,
716 "trailing garbage after EOF ignored\n" );
717 return True;
718 }
719 default:
720 panic ( "test:unexpected error" );
721 }
722
723 panic ( "test:end" );
724 return True; /*notreached*/
725}
726
727
728/*---------------------------------------------------*/
729/*--- Error [non-] handling grunge ---*/
730/*---------------------------------------------------*/
731
732/*---------------------------------------------*/
733static
734void setExit ( Int32 v )
735{
736 if (v > exitValue) exitValue = v;
737}
738
739
740/*---------------------------------------------*/
741static
742void cadvise ( void )
743{
744 if (noisy)
745 fprintf (
746 stderr,
747 "\nIt is possible that the compressed file(s) have become corrupted.\n"
748 "You can use the -tvv option to test integrity of such files.\n\n"
749 "You can use the `bzip2recover' program to attempt to recover\n"
750 "data from undamaged sections of corrupted files.\n\n"
751 );
752}
753
754
755/*---------------------------------------------*/
756static
757void showFileNames ( void )
758{
759 if (noisy)
760 fprintf (
761 stderr,
762 "\tInput file = %s, output file = %s\n",
763 inName, outName
764 );
765}
766
767
768/*---------------------------------------------*/
769static
770void cleanUpAndFail ( Int32 ec )
771{
772 IntNative retVal;
773 struct MY_STAT statBuf;
774
775 if ( srcMode == SM_F2F
776 && opMode != OM_TEST
777 && deleteOutputOnInterrupt ) {
778
779 /* Check whether input file still exists. Delete output file
780 only if input exists to avoid loss of data. Joerg Prante, 5
781 January 2002. (JRS 06-Jan-2002: other changes in 1.0.2 mean
782 this is less likely to happen. But to be ultra-paranoid, we
783 do the check anyway.) */
784 retVal = MY_STAT ( inName, &statBuf );
785 if (retVal == 0) {
786 if (noisy)
787 fprintf ( stderr,
788 "%s: Deleting output file %s, if it exists.\n",
789 progName, outName );
790 if (outputHandleJustInCase != NULL)
791 fclose ( outputHandleJustInCase );
792 retVal = remove ( outName );
793 if (retVal != 0)
794 fprintf ( stderr,
795 "%s: WARNING: deletion of output file "
796 "(apparently) failed.\n",
797 progName );
798 } else {
799 fprintf ( stderr,
800 "%s: WARNING: deletion of output file suppressed\n",
801 progName );
802 fprintf ( stderr,
803 "%s: since input file no longer exists. Output file\n",
804 progName );
805 fprintf ( stderr,
806 "%s: `%s' may be incomplete.\n",
807 progName, outName );
808 fprintf ( stderr,
809 "%s: I suggest doing an integrity test (bzip2 -tv)"
810 " of it.\n",
811 progName );
812 }
813 }
814
815 if (noisy && numFileNames > 0 && numFilesProcessed < numFileNames) {
816 fprintf ( stderr,
817 "%s: WARNING: some files have not been processed:\n"
818 "%s: %d specified on command line, %d not processed yet.\n\n",
819 progName, progName,
820 numFileNames, numFileNames - numFilesProcessed );
821 }
822 setExit(ec);
823 exit(exitValue);
824}
825
826
827/*---------------------------------------------*/
828static
829void panic ( Char* s )
830{
831 fprintf ( stderr,
832 "\n%s: PANIC -- internal consistency error:\n"
833 "\t%s\n"
834 "\tThis is a BUG. Please report it to me at:\n"
831 "\tjseward@acm.org\n",
835 "\tjseward@bzip.org\n",
832 progName, s );
833 showFileNames();
834 cleanUpAndFail( 3 );
835}
836
837
838/*---------------------------------------------*/
839static
840void crcError ( void )
841{
842 fprintf ( stderr,
843 "\n%s: Data integrity error when decompressing.\n",
844 progName );
845 showFileNames();
846 cadvise();
847 cleanUpAndFail( 2 );
848}
849
850
851/*---------------------------------------------*/
852static
853void compressedStreamEOF ( void )
854{
855 if (noisy) {
856 fprintf ( stderr,
857 "\n%s: Compressed file ends unexpectedly;\n\t"
858 "perhaps it is corrupted? *Possible* reason follows.\n",
859 progName );
860 perror ( progName );
861 showFileNames();
862 cadvise();
863 }
864 cleanUpAndFail( 2 );
865}
866
867
868/*---------------------------------------------*/
869static
870void ioError ( void )
871{
872 fprintf ( stderr,
873 "\n%s: I/O or other error, bailing out. "
874 "Possible reason follows.\n",
875 progName );
876 perror ( progName );
877 showFileNames();
878 cleanUpAndFail( 1 );
879}
880
881
882/*---------------------------------------------*/
883static
884void mySignalCatcher ( IntNative n )
885{
886 fprintf ( stderr,
887 "\n%s: Control-C or similar caught, quitting.\n",
888 progName );
889 cleanUpAndFail(1);
890}
891
892
893/*---------------------------------------------*/
894static
895void mySIGSEGVorSIGBUScatcher ( IntNative n )
896{
897 if (opMode == OM_Z)
898 fprintf (
899 stderr,
900 "\n%s: Caught a SIGSEGV or SIGBUS whilst compressing.\n"
901 "\n"
902 " Possible causes are (most likely first):\n"
903 " (1) This computer has unreliable memory or cache hardware\n"
904 " (a surprisingly common problem; try a different machine.)\n"
905 " (2) A bug in the compiler used to create this executable\n"
906 " (unlikely, if you didn't compile bzip2 yourself.)\n"
907 " (3) A real bug in bzip2 -- I hope this should never be the case.\n"
908 " The user's manual, Section 4.3, has more info on (1) and (2).\n"
909 " \n"
910 " If you suspect this is a bug in bzip2, or are unsure about (1)\n"
836 progName, s );
837 showFileNames();
838 cleanUpAndFail( 3 );
839}
840
841
842/*---------------------------------------------*/
843static
844void crcError ( void )
845{
846 fprintf ( stderr,
847 "\n%s: Data integrity error when decompressing.\n",
848 progName );
849 showFileNames();
850 cadvise();
851 cleanUpAndFail( 2 );
852}
853
854
855/*---------------------------------------------*/
856static
857void compressedStreamEOF ( void )
858{
859 if (noisy) {
860 fprintf ( stderr,
861 "\n%s: Compressed file ends unexpectedly;\n\t"
862 "perhaps it is corrupted? *Possible* reason follows.\n",
863 progName );
864 perror ( progName );
865 showFileNames();
866 cadvise();
867 }
868 cleanUpAndFail( 2 );
869}
870
871
872/*---------------------------------------------*/
873static
874void ioError ( void )
875{
876 fprintf ( stderr,
877 "\n%s: I/O or other error, bailing out. "
878 "Possible reason follows.\n",
879 progName );
880 perror ( progName );
881 showFileNames();
882 cleanUpAndFail( 1 );
883}
884
885
886/*---------------------------------------------*/
887static
888void mySignalCatcher ( IntNative n )
889{
890 fprintf ( stderr,
891 "\n%s: Control-C or similar caught, quitting.\n",
892 progName );
893 cleanUpAndFail(1);
894}
895
896
897/*---------------------------------------------*/
898static
899void mySIGSEGVorSIGBUScatcher ( IntNative n )
900{
901 if (opMode == OM_Z)
902 fprintf (
903 stderr,
904 "\n%s: Caught a SIGSEGV or SIGBUS whilst compressing.\n"
905 "\n"
906 " Possible causes are (most likely first):\n"
907 " (1) This computer has unreliable memory or cache hardware\n"
908 " (a surprisingly common problem; try a different machine.)\n"
909 " (2) A bug in the compiler used to create this executable\n"
910 " (unlikely, if you didn't compile bzip2 yourself.)\n"
911 " (3) A real bug in bzip2 -- I hope this should never be the case.\n"
912 " The user's manual, Section 4.3, has more info on (1) and (2).\n"
913 " \n"
914 " If you suspect this is a bug in bzip2, or are unsure about (1)\n"
911 " or (2), feel free to report it to me at: jseward@acm.org.\n"
915 " or (2), feel free to report it to me at: jseward@bzip.org.\n"
912 " Section 4.3 of the user's manual describes the info a useful\n"
913 " bug report should have. If the manual is available on your\n"
914 " system, please try and read it before mailing me. If you don't\n"
915 " have the manual or can't be bothered to read it, mail me anyway.\n"
916 "\n",
917 progName );
918 else
919 fprintf (
920 stderr,
921 "\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing.\n"
922 "\n"
923 " Possible causes are (most likely first):\n"
924 " (1) The compressed data is corrupted, and bzip2's usual checks\n"
925 " failed to detect this. Try bzip2 -tvv my_file.bz2.\n"
926 " (2) This computer has unreliable memory or cache hardware\n"
927 " (a surprisingly common problem; try a different machine.)\n"
928 " (3) A bug in the compiler used to create this executable\n"
929 " (unlikely, if you didn't compile bzip2 yourself.)\n"
930 " (4) A real bug in bzip2 -- I hope this should never be the case.\n"
931 " The user's manual, Section 4.3, has more info on (2) and (3).\n"
932 " \n"
933 " If you suspect this is a bug in bzip2, or are unsure about (2)\n"
916 " Section 4.3 of the user's manual describes the info a useful\n"
917 " bug report should have. If the manual is available on your\n"
918 " system, please try and read it before mailing me. If you don't\n"
919 " have the manual or can't be bothered to read it, mail me anyway.\n"
920 "\n",
921 progName );
922 else
923 fprintf (
924 stderr,
925 "\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing.\n"
926 "\n"
927 " Possible causes are (most likely first):\n"
928 " (1) The compressed data is corrupted, and bzip2's usual checks\n"
929 " failed to detect this. Try bzip2 -tvv my_file.bz2.\n"
930 " (2) This computer has unreliable memory or cache hardware\n"
931 " (a surprisingly common problem; try a different machine.)\n"
932 " (3) A bug in the compiler used to create this executable\n"
933 " (unlikely, if you didn't compile bzip2 yourself.)\n"
934 " (4) A real bug in bzip2 -- I hope this should never be the case.\n"
935 " The user's manual, Section 4.3, has more info on (2) and (3).\n"
936 " \n"
937 " If you suspect this is a bug in bzip2, or are unsure about (2)\n"
934 " or (3), feel free to report it to me at: jseward@acm.org.\n"
938 " or (3), feel free to report it to me at: jseward@bzip.org.\n"
935 " Section 4.3 of the user's manual describes the info a useful\n"
936 " bug report should have. If the manual is available on your\n"
937 " system, please try and read it before mailing me. If you don't\n"
938 " have the manual or can't be bothered to read it, mail me anyway.\n"
939 "\n",
940 progName );
941
942 showFileNames();
943 if (opMode == OM_Z)
944 cleanUpAndFail( 3 ); else
945 { cadvise(); cleanUpAndFail( 2 ); }
946}
947
948
949/*---------------------------------------------*/
950static
951void outOfMemory ( void )
952{
953 fprintf ( stderr,
954 "\n%s: couldn't allocate enough memory\n",
955 progName );
956 showFileNames();
957 cleanUpAndFail(1);
958}
959
960
961/*---------------------------------------------*/
962static
963void configError ( void )
964{
965 fprintf ( stderr,
966 "bzip2: I'm not configured correctly for this platform!\n"
967 "\tI require Int32, Int16 and Char to have sizes\n"
968 "\tof 4, 2 and 1 bytes to run properly, and they don't.\n"
969 "\tProbably you can fix this by defining them correctly,\n"
970 "\tand recompiling. Bye!\n" );
971 setExit(3);
972 exit(exitValue);
973}
974
975
976/*---------------------------------------------------*/
977/*--- The main driver machinery ---*/
978/*---------------------------------------------------*/
979
980/* All rather crufty. The main problem is that input files
981 are stat()d multiple times before use. This should be
982 cleaned up.
983*/
984
985/*---------------------------------------------*/
986static
987void pad ( Char *s )
988{
989 Int32 i;
990 if ( (Int32)strlen(s) >= longestFileName ) return;
991 for (i = 1; i <= longestFileName - (Int32)strlen(s); i++)
992 fprintf ( stderr, " " );
993}
994
995
996/*---------------------------------------------*/
997static
998void copyFileName ( Char* to, Char* from )
999{
1000 if ( strlen(from) > FILE_NAME_LEN-10 ) {
1001 fprintf (
1002 stderr,
1003 "bzip2: file name\n`%s'\n"
1004 "is suspiciously (more than %d chars) long.\n"
1005 "Try using a reasonable file name instead. Sorry! :-)\n",
1006 from, FILE_NAME_LEN-10
1007 );
1008 setExit(1);
1009 exit(exitValue);
1010 }
1011
1012 strncpy(to,from,FILE_NAME_LEN-10);
1013 to[FILE_NAME_LEN-10]='\0';
1014}
1015
1016
1017/*---------------------------------------------*/
1018static
1019Bool fileExists ( Char* name )
1020{
1021 FILE *tmp = fopen ( name, "rb" );
1022 Bool exists = (tmp != NULL);
1023 if (tmp != NULL) fclose ( tmp );
1024 return exists;
1025}
1026
1027
1028/*---------------------------------------------*/
1029/* Open an output file safely with O_EXCL and good permissions.
1030 This avoids a race condition in versions < 1.0.2, in which
1031 the file was first opened and then had its interim permissions
1032 set safely. We instead use open() to create the file with
1033 the interim permissions required. (--- --- rw-).
1034
1035 For non-Unix platforms, if we are not worrying about
1036 security issues, simple this simply behaves like fopen.
1037*/
1038FILE* fopen_output_safely ( Char* name, const char* mode )
1039{
1040# if BZ_UNIX
1041 FILE* fp;
1042 IntNative fh;
1043 fh = open(name, O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR);
1044 if (fh == -1) return NULL;
1045 fp = fdopen(fh, mode);
1046 if (fp == NULL) close(fh);
1047 return fp;
1048# else
1049 return fopen(name, mode);
1050# endif
1051}
1052
1053
1054/*---------------------------------------------*/
1055/*--
1056 if in doubt, return True
1057--*/
1058static
1059Bool notAStandardFile ( Char* name )
1060{
1061 IntNative i;
1062 struct MY_STAT statBuf;
1063
1064 i = MY_LSTAT ( name, &statBuf );
1065 if (i != 0) return True;
1066 if (MY_S_ISREG(statBuf.st_mode)) return False;
1067 return True;
1068}
1069
1070
1071/*---------------------------------------------*/
1072/*--
1073 rac 11/21/98 see if file has hard links to it
1074--*/
1075static
1076Int32 countHardLinks ( Char* name )
1077{
1078 IntNative i;
1079 struct MY_STAT statBuf;
1080
1081 i = MY_LSTAT ( name, &statBuf );
1082 if (i != 0) return 0;
1083 return (statBuf.st_nlink - 1);
1084}
1085
1086
1087/*---------------------------------------------*/
1088/* Copy modification date, access date, permissions and owner from the
1089 source to destination file. We have to copy this meta-info off
1090 into fileMetaInfo before starting to compress / decompress it,
1091 because doing it afterwards means we get the wrong access time.
1092
1093 To complicate matters, in compress() and decompress() below, the
1094 sequence of tests preceding the call to saveInputFileMetaInfo()
1095 involves calling fileExists(), which in turn establishes its result
1096 by attempting to fopen() the file, and if successful, immediately
1097 fclose()ing it again. So we have to assume that the fopen() call
1098 does not cause the access time field to be updated.
1099
1100 Reading of the man page for stat() (man 2 stat) on RedHat 7.2 seems
1101 to imply that merely doing open() will not affect the access time.
1102 Therefore we merely need to hope that the C library only does
1103 open() as a result of fopen(), and not any kind of read()-ahead
1104 cleverness.
1105
1106 It sounds pretty fragile to me. Whether this carries across
1107 robustly to arbitrary Unix-like platforms (or even works robustly
1108 on this one, RedHat 7.2) is unknown to me. Nevertheless ...
1109*/
1110#if BZ_UNIX
1111static
1112struct MY_STAT fileMetaInfo;
1113#endif
1114
1115static
1116void saveInputFileMetaInfo ( Char *srcName )
1117{
1118# if BZ_UNIX
1119 IntNative retVal;
1120 /* Note use of stat here, not lstat. */
1121 retVal = MY_STAT( srcName, &fileMetaInfo );
1122 ERROR_IF_NOT_ZERO ( retVal );
1123# endif
1124}
1125
1126
1127static
1128void applySavedMetaInfoToOutputFile ( Char *dstName )
1129{
1130# if BZ_UNIX
1131 IntNative retVal;
1132 struct utimbuf uTimBuf;
1133
1134 uTimBuf.actime = fileMetaInfo.st_atime;
1135 uTimBuf.modtime = fileMetaInfo.st_mtime;
1136
1137 retVal = chmod ( dstName, fileMetaInfo.st_mode );
1138 ERROR_IF_NOT_ZERO ( retVal );
1139
1140 retVal = utime ( dstName, &uTimBuf );
1141 ERROR_IF_NOT_ZERO ( retVal );
1142
1143 retVal = chown ( dstName, fileMetaInfo.st_uid, fileMetaInfo.st_gid );
1144 /* chown() will in many cases return with EPERM, which can
1145 be safely ignored.
1146 */
1147# endif
1148}
1149
1150
1151/*---------------------------------------------*/
1152static
1153Bool containsDubiousChars ( Char* name )
1154{
1155# if BZ_UNIX
1156 /* On unix, files can contain any characters and the file expansion
1157 * is performed by the shell.
1158 */
1159 return False;
1160# else /* ! BZ_UNIX */
1161 /* On non-unix (Win* platforms), wildcard characters are not allowed in
1162 * filenames.
1163 */
1164 for (; *name != '\0'; name++)
1165 if (*name == '?' || *name == '*') return True;
1166 return False;
1167# endif /* BZ_UNIX */
1168}
1169
1170
1171/*---------------------------------------------*/
1172#define BZ_N_SUFFIX_PAIRS 4
1173
1174Char* zSuffix[BZ_N_SUFFIX_PAIRS]
1175 = { ".bz2", ".bz", ".tbz2", ".tbz" };
1176Char* unzSuffix[BZ_N_SUFFIX_PAIRS]
1177 = { "", "", ".tar", ".tar" };
1178
1179static
1180Bool hasSuffix ( Char* s, Char* suffix )
1181{
1182 Int32 ns = strlen(s);
1183 Int32 nx = strlen(suffix);
1184 if (ns < nx) return False;
1185 if (strcmp(s + ns - nx, suffix) == 0) return True;
1186 return False;
1187}
1188
1189static
1190Bool mapSuffix ( Char* name,
1191 Char* oldSuffix, Char* newSuffix )
1192{
1193 if (!hasSuffix(name,oldSuffix)) return False;
1194 name[strlen(name)-strlen(oldSuffix)] = 0;
1195 strcat ( name, newSuffix );
1196 return True;
1197}
1198
1199
1200/*---------------------------------------------*/
1201static
1202void compress ( Char *name )
1203{
1204 FILE *inStr;
1205 FILE *outStr;
1206 Int32 n, i;
1207 struct MY_STAT statBuf;
1208
1209 deleteOutputOnInterrupt = False;
1210
1211 if (name == NULL && srcMode != SM_I2O)
1212 panic ( "compress: bad modes\n" );
1213
1214 switch (srcMode) {
1215 case SM_I2O:
1216 copyFileName ( inName, "(stdin)" );
1217 copyFileName ( outName, "(stdout)" );
1218 break;
1219 case SM_F2F:
1220 copyFileName ( inName, name );
1221 copyFileName ( outName, name );
1222 strcat ( outName, ".bz2" );
1223 break;
1224 case SM_F2O:
1225 copyFileName ( inName, name );
1226 copyFileName ( outName, "(stdout)" );
1227 break;
1228 }
1229
1230 if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
1231 if (noisy)
1232 fprintf ( stderr, "%s: There are no files matching `%s'.\n",
1233 progName, inName );
1234 setExit(1);
1235 return;
1236 }
1237 if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
1238 fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
1239 progName, inName, strerror(errno) );
1240 setExit(1);
1241 return;
1242 }
1243 for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++) {
1244 if (hasSuffix(inName, zSuffix[i])) {
1245 if (noisy)
1246 fprintf ( stderr,
1247 "%s: Input file %s already has %s suffix.\n",
1248 progName, inName, zSuffix[i] );
1249 setExit(1);
1250 return;
1251 }
1252 }
1253 if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
1254 MY_STAT(inName, &statBuf);
1255 if ( MY_S_ISDIR(statBuf.st_mode) ) {
1256 fprintf( stderr,
1257 "%s: Input file %s is a directory.\n",
1258 progName,inName);
1259 setExit(1);
1260 return;
1261 }
1262 }
1263 if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
1264 if (noisy)
1265 fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
1266 progName, inName );
1267 setExit(1);
1268 return;
1269 }
1270 if ( srcMode == SM_F2F && fileExists ( outName ) ) {
1271 if (forceOverwrite) {
1272 remove(outName);
1273 } else {
1274 fprintf ( stderr, "%s: Output file %s already exists.\n",
1275 progName, outName );
1276 setExit(1);
1277 return;
1278 }
1279 }
1280 if ( srcMode == SM_F2F && !forceOverwrite &&
1281 (n=countHardLinks ( inName )) > 0) {
1282 fprintf ( stderr, "%s: Input file %s has %d other link%s.\n",
1283 progName, inName, n, n > 1 ? "s" : "" );
1284 setExit(1);
1285 return;
1286 }
1287
1288 if ( srcMode == SM_F2F ) {
1289 /* Save the file's meta-info before we open it. Doing it later
1290 means we mess up the access times. */
1291 saveInputFileMetaInfo ( inName );
1292 }
1293
1294 switch ( srcMode ) {
1295
1296 case SM_I2O:
1297 inStr = stdin;
1298 outStr = stdout;
1299 if ( isatty ( fileno ( stdout ) ) ) {
1300 fprintf ( stderr,
1301 "%s: I won't write compressed data to a terminal.\n",
1302 progName );
1303 fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
1304 progName, progName );
1305 setExit(1);
1306 return;
1307 };
1308 break;
1309
1310 case SM_F2O:
1311 inStr = fopen ( inName, "rb" );
1312 outStr = stdout;
1313 if ( isatty ( fileno ( stdout ) ) ) {
1314 fprintf ( stderr,
1315 "%s: I won't write compressed data to a terminal.\n",
1316 progName );
1317 fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
1318 progName, progName );
1319 if ( inStr != NULL ) fclose ( inStr );
1320 setExit(1);
1321 return;
1322 };
1323 if ( inStr == NULL ) {
1324 fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
1325 progName, inName, strerror(errno) );
1326 setExit(1);
1327 return;
1328 };
1329 break;
1330
1331 case SM_F2F:
1332 inStr = fopen ( inName, "rb" );
1333 outStr = fopen_output_safely ( outName, "wb" );
1334 if ( outStr == NULL) {
1335 fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
1336 progName, outName, strerror(errno) );
1337 if ( inStr != NULL ) fclose ( inStr );
1338 setExit(1);
1339 return;
1340 }
1341 if ( inStr == NULL ) {
1342 fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
1343 progName, inName, strerror(errno) );
1344 if ( outStr != NULL ) fclose ( outStr );
1345 setExit(1);
1346 return;
1347 };
1348 break;
1349
1350 default:
1351 panic ( "compress: bad srcMode" );
1352 break;
1353 }
1354
1355 if (verbosity >= 1) {
1356 fprintf ( stderr, " %s: ", inName );
1357 pad ( inName );
1358 fflush ( stderr );
1359 }
1360
1361 /*--- Now the input and output handles are sane. Do the Biz. ---*/
1362 outputHandleJustInCase = outStr;
1363 deleteOutputOnInterrupt = True;
1364 compressStream ( inStr, outStr );
1365 outputHandleJustInCase = NULL;
1366
1367 /*--- If there was an I/O error, we won't get here. ---*/
1368 if ( srcMode == SM_F2F ) {
1369 applySavedMetaInfoToOutputFile ( outName );
1370 deleteOutputOnInterrupt = False;
1371 if ( !keepInputFiles ) {
1372 IntNative retVal = remove ( inName );
1373 ERROR_IF_NOT_ZERO ( retVal );
1374 }
1375 }
1376
1377 deleteOutputOnInterrupt = False;
1378}
1379
1380
1381/*---------------------------------------------*/
1382static
1383void uncompress ( Char *name )
1384{
1385 FILE *inStr;
1386 FILE *outStr;
1387 Int32 n, i;
1388 Bool magicNumberOK;
1389 Bool cantGuess;
1390 struct MY_STAT statBuf;
1391
1392 deleteOutputOnInterrupt = False;
1393
1394 if (name == NULL && srcMode != SM_I2O)
1395 panic ( "uncompress: bad modes\n" );
1396
1397 cantGuess = False;
1398 switch (srcMode) {
1399 case SM_I2O:
1400 copyFileName ( inName, "(stdin)" );
1401 copyFileName ( outName, "(stdout)" );
1402 break;
1403 case SM_F2F:
1404 copyFileName ( inName, name );
1405 copyFileName ( outName, name );
1406 for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++)
1407 if (mapSuffix(outName,zSuffix[i],unzSuffix[i]))
1408 goto zzz;
1409 cantGuess = True;
1410 strcat ( outName, ".out" );
1411 break;
1412 case SM_F2O:
1413 copyFileName ( inName, name );
1414 copyFileName ( outName, "(stdout)" );
1415 break;
1416 }
1417
1418 zzz:
1419 if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
1420 if (noisy)
1421 fprintf ( stderr, "%s: There are no files matching `%s'.\n",
1422 progName, inName );
1423 setExit(1);
1424 return;
1425 }
1426 if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
1427 fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
1428 progName, inName, strerror(errno) );
1429 setExit(1);
1430 return;
1431 }
1432 if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
1433 MY_STAT(inName, &statBuf);
1434 if ( MY_S_ISDIR(statBuf.st_mode) ) {
1435 fprintf( stderr,
1436 "%s: Input file %s is a directory.\n",
1437 progName,inName);
1438 setExit(1);
1439 return;
1440 }
1441 }
1442 if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
1443 if (noisy)
1444 fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
1445 progName, inName );
1446 setExit(1);
1447 return;
1448 }
1449 if ( /* srcMode == SM_F2F implied && */ cantGuess ) {
1450 if (noisy)
1451 fprintf ( stderr,
1452 "%s: Can't guess original name for %s -- using %s\n",
1453 progName, inName, outName );
1454 /* just a warning, no return */
1455 }
1456 if ( srcMode == SM_F2F && fileExists ( outName ) ) {
1457 if (forceOverwrite) {
1458 remove(outName);
1459 } else {
1460 fprintf ( stderr, "%s: Output file %s already exists.\n",
1461 progName, outName );
1462 setExit(1);
1463 return;
1464 }
1465 }
1466 if ( srcMode == SM_F2F && !forceOverwrite &&
1467 (n=countHardLinks ( inName ) ) > 0) {
1468 fprintf ( stderr, "%s: Input file %s has %d other link%s.\n",
1469 progName, inName, n, n > 1 ? "s" : "" );
1470 setExit(1);
1471 return;
1472 }
1473
1474 if ( srcMode == SM_F2F ) {
1475 /* Save the file's meta-info before we open it. Doing it later
1476 means we mess up the access times. */
1477 saveInputFileMetaInfo ( inName );
1478 }
1479
1480 switch ( srcMode ) {
1481
1482 case SM_I2O:
1483 inStr = stdin;
1484 outStr = stdout;
1485 if ( isatty ( fileno ( stdin ) ) ) {
1486 fprintf ( stderr,
1487 "%s: I won't read compressed data from a terminal.\n",
1488 progName );
1489 fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
1490 progName, progName );
1491 setExit(1);
1492 return;
1493 };
1494 break;
1495
1496 case SM_F2O:
1497 inStr = fopen ( inName, "rb" );
1498 outStr = stdout;
1499 if ( inStr == NULL ) {
1500 fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
1501 progName, inName, strerror(errno) );
1502 if ( inStr != NULL ) fclose ( inStr );
1503 setExit(1);
1504 return;
1505 };
1506 break;
1507
1508 case SM_F2F:
1509 inStr = fopen ( inName, "rb" );
1510 outStr = fopen_output_safely ( outName, "wb" );
1511 if ( outStr == NULL) {
1512 fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
1513 progName, outName, strerror(errno) );
1514 if ( inStr != NULL ) fclose ( inStr );
1515 setExit(1);
1516 return;
1517 }
1518 if ( inStr == NULL ) {
1519 fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
1520 progName, inName, strerror(errno) );
1521 if ( outStr != NULL ) fclose ( outStr );
1522 setExit(1);
1523 return;
1524 };
1525 break;
1526
1527 default:
1528 panic ( "uncompress: bad srcMode" );
1529 break;
1530 }
1531
1532 if (verbosity >= 1) {
1533 fprintf ( stderr, " %s: ", inName );
1534 pad ( inName );
1535 fflush ( stderr );
1536 }
1537
1538 /*--- Now the input and output handles are sane. Do the Biz. ---*/
1539 outputHandleJustInCase = outStr;
1540 deleteOutputOnInterrupt = True;
1541 magicNumberOK = uncompressStream ( inStr, outStr );
1542 outputHandleJustInCase = NULL;
1543
1544 /*--- If there was an I/O error, we won't get here. ---*/
1545 if ( magicNumberOK ) {
1546 if ( srcMode == SM_F2F ) {
1547 applySavedMetaInfoToOutputFile ( outName );
1548 deleteOutputOnInterrupt = False;
1549 if ( !keepInputFiles ) {
1550 IntNative retVal = remove ( inName );
1551 ERROR_IF_NOT_ZERO ( retVal );
1552 }
1553 }
1554 } else {
1555 unzFailsExist = True;
1556 deleteOutputOnInterrupt = False;
1557 if ( srcMode == SM_F2F ) {
1558 IntNative retVal = remove ( outName );
1559 ERROR_IF_NOT_ZERO ( retVal );
1560 }
1561 }
1562 deleteOutputOnInterrupt = False;
1563
1564 if ( magicNumberOK ) {
1565 if (verbosity >= 1)
1566 fprintf ( stderr, "done\n" );
1567 } else {
1568 setExit(2);
1569 if (verbosity >= 1)
1570 fprintf ( stderr, "not a bzip2 file.\n" ); else
1571 fprintf ( stderr,
1572 "%s: %s is not a bzip2 file.\n",
1573 progName, inName );
1574 }
1575
1576}
1577
1578
1579/*---------------------------------------------*/
1580static
1581void testf ( Char *name )
1582{
1583 FILE *inStr;
1584 Bool allOK;
1585 struct MY_STAT statBuf;
1586
1587 deleteOutputOnInterrupt = False;
1588
1589 if (name == NULL && srcMode != SM_I2O)
1590 panic ( "testf: bad modes\n" );
1591
1592 copyFileName ( outName, "(none)" );
1593 switch (srcMode) {
1594 case SM_I2O: copyFileName ( inName, "(stdin)" ); break;
1595 case SM_F2F: copyFileName ( inName, name ); break;
1596 case SM_F2O: copyFileName ( inName, name ); break;
1597 }
1598
1599 if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
1600 if (noisy)
1601 fprintf ( stderr, "%s: There are no files matching `%s'.\n",
1602 progName, inName );
1603 setExit(1);
1604 return;
1605 }
1606 if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
1607 fprintf ( stderr, "%s: Can't open input %s: %s.\n",
1608 progName, inName, strerror(errno) );
1609 setExit(1);
1610 return;
1611 }
1612 if ( srcMode != SM_I2O ) {
1613 MY_STAT(inName, &statBuf);
1614 if ( MY_S_ISDIR(statBuf.st_mode) ) {
1615 fprintf( stderr,
1616 "%s: Input file %s is a directory.\n",
1617 progName,inName);
1618 setExit(1);
1619 return;
1620 }
1621 }
1622
1623 switch ( srcMode ) {
1624
1625 case SM_I2O:
1626 if ( isatty ( fileno ( stdin ) ) ) {
1627 fprintf ( stderr,
1628 "%s: I won't read compressed data from a terminal.\n",
1629 progName );
1630 fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
1631 progName, progName );
1632 setExit(1);
1633 return;
1634 };
1635 inStr = stdin;
1636 break;
1637
1638 case SM_F2O: case SM_F2F:
1639 inStr = fopen ( inName, "rb" );
1640 if ( inStr == NULL ) {
1641 fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
1642 progName, inName, strerror(errno) );
1643 setExit(1);
1644 return;
1645 };
1646 break;
1647
1648 default:
1649 panic ( "testf: bad srcMode" );
1650 break;
1651 }
1652
1653 if (verbosity >= 1) {
1654 fprintf ( stderr, " %s: ", inName );
1655 pad ( inName );
1656 fflush ( stderr );
1657 }
1658
1659 /*--- Now the input handle is sane. Do the Biz. ---*/
1660 outputHandleJustInCase = NULL;
1661 allOK = testStream ( inStr );
1662
1663 if (allOK && verbosity >= 1) fprintf ( stderr, "ok\n" );
1664 if (!allOK) testFailsExist = True;
1665}
1666
1667
1668/*---------------------------------------------*/
1669static
1670void license ( void )
1671{
1672 fprintf ( stderr,
1673
1674 "bzip2, a block-sorting file compressor. "
1675 "Version %s.\n"
1676 " \n"
939 " Section 4.3 of the user's manual describes the info a useful\n"
940 " bug report should have. If the manual is available on your\n"
941 " system, please try and read it before mailing me. If you don't\n"
942 " have the manual or can't be bothered to read it, mail me anyway.\n"
943 "\n",
944 progName );
945
946 showFileNames();
947 if (opMode == OM_Z)
948 cleanUpAndFail( 3 ); else
949 { cadvise(); cleanUpAndFail( 2 ); }
950}
951
952
953/*---------------------------------------------*/
954static
955void outOfMemory ( void )
956{
957 fprintf ( stderr,
958 "\n%s: couldn't allocate enough memory\n",
959 progName );
960 showFileNames();
961 cleanUpAndFail(1);
962}
963
964
965/*---------------------------------------------*/
966static
967void configError ( void )
968{
969 fprintf ( stderr,
970 "bzip2: I'm not configured correctly for this platform!\n"
971 "\tI require Int32, Int16 and Char to have sizes\n"
972 "\tof 4, 2 and 1 bytes to run properly, and they don't.\n"
973 "\tProbably you can fix this by defining them correctly,\n"
974 "\tand recompiling. Bye!\n" );
975 setExit(3);
976 exit(exitValue);
977}
978
979
980/*---------------------------------------------------*/
981/*--- The main driver machinery ---*/
982/*---------------------------------------------------*/
983
984/* All rather crufty. The main problem is that input files
985 are stat()d multiple times before use. This should be
986 cleaned up.
987*/
988
989/*---------------------------------------------*/
990static
991void pad ( Char *s )
992{
993 Int32 i;
994 if ( (Int32)strlen(s) >= longestFileName ) return;
995 for (i = 1; i <= longestFileName - (Int32)strlen(s); i++)
996 fprintf ( stderr, " " );
997}
998
999
1000/*---------------------------------------------*/
1001static
1002void copyFileName ( Char* to, Char* from )
1003{
1004 if ( strlen(from) > FILE_NAME_LEN-10 ) {
1005 fprintf (
1006 stderr,
1007 "bzip2: file name\n`%s'\n"
1008 "is suspiciously (more than %d chars) long.\n"
1009 "Try using a reasonable file name instead. Sorry! :-)\n",
1010 from, FILE_NAME_LEN-10
1011 );
1012 setExit(1);
1013 exit(exitValue);
1014 }
1015
1016 strncpy(to,from,FILE_NAME_LEN-10);
1017 to[FILE_NAME_LEN-10]='\0';
1018}
1019
1020
1021/*---------------------------------------------*/
1022static
1023Bool fileExists ( Char* name )
1024{
1025 FILE *tmp = fopen ( name, "rb" );
1026 Bool exists = (tmp != NULL);
1027 if (tmp != NULL) fclose ( tmp );
1028 return exists;
1029}
1030
1031
1032/*---------------------------------------------*/
1033/* Open an output file safely with O_EXCL and good permissions.
1034 This avoids a race condition in versions < 1.0.2, in which
1035 the file was first opened and then had its interim permissions
1036 set safely. We instead use open() to create the file with
1037 the interim permissions required. (--- --- rw-).
1038
1039 For non-Unix platforms, if we are not worrying about
1040 security issues, simple this simply behaves like fopen.
1041*/
1042FILE* fopen_output_safely ( Char* name, const char* mode )
1043{
1044# if BZ_UNIX
1045 FILE* fp;
1046 IntNative fh;
1047 fh = open(name, O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR);
1048 if (fh == -1) return NULL;
1049 fp = fdopen(fh, mode);
1050 if (fp == NULL) close(fh);
1051 return fp;
1052# else
1053 return fopen(name, mode);
1054# endif
1055}
1056
1057
1058/*---------------------------------------------*/
1059/*--
1060 if in doubt, return True
1061--*/
1062static
1063Bool notAStandardFile ( Char* name )
1064{
1065 IntNative i;
1066 struct MY_STAT statBuf;
1067
1068 i = MY_LSTAT ( name, &statBuf );
1069 if (i != 0) return True;
1070 if (MY_S_ISREG(statBuf.st_mode)) return False;
1071 return True;
1072}
1073
1074
1075/*---------------------------------------------*/
1076/*--
1077 rac 11/21/98 see if file has hard links to it
1078--*/
1079static
1080Int32 countHardLinks ( Char* name )
1081{
1082 IntNative i;
1083 struct MY_STAT statBuf;
1084
1085 i = MY_LSTAT ( name, &statBuf );
1086 if (i != 0) return 0;
1087 return (statBuf.st_nlink - 1);
1088}
1089
1090
1091/*---------------------------------------------*/
1092/* Copy modification date, access date, permissions and owner from the
1093 source to destination file. We have to copy this meta-info off
1094 into fileMetaInfo before starting to compress / decompress it,
1095 because doing it afterwards means we get the wrong access time.
1096
1097 To complicate matters, in compress() and decompress() below, the
1098 sequence of tests preceding the call to saveInputFileMetaInfo()
1099 involves calling fileExists(), which in turn establishes its result
1100 by attempting to fopen() the file, and if successful, immediately
1101 fclose()ing it again. So we have to assume that the fopen() call
1102 does not cause the access time field to be updated.
1103
1104 Reading of the man page for stat() (man 2 stat) on RedHat 7.2 seems
1105 to imply that merely doing open() will not affect the access time.
1106 Therefore we merely need to hope that the C library only does
1107 open() as a result of fopen(), and not any kind of read()-ahead
1108 cleverness.
1109
1110 It sounds pretty fragile to me. Whether this carries across
1111 robustly to arbitrary Unix-like platforms (or even works robustly
1112 on this one, RedHat 7.2) is unknown to me. Nevertheless ...
1113*/
1114#if BZ_UNIX
1115static
1116struct MY_STAT fileMetaInfo;
1117#endif
1118
1119static
1120void saveInputFileMetaInfo ( Char *srcName )
1121{
1122# if BZ_UNIX
1123 IntNative retVal;
1124 /* Note use of stat here, not lstat. */
1125 retVal = MY_STAT( srcName, &fileMetaInfo );
1126 ERROR_IF_NOT_ZERO ( retVal );
1127# endif
1128}
1129
1130
1131static
1132void applySavedMetaInfoToOutputFile ( Char *dstName )
1133{
1134# if BZ_UNIX
1135 IntNative retVal;
1136 struct utimbuf uTimBuf;
1137
1138 uTimBuf.actime = fileMetaInfo.st_atime;
1139 uTimBuf.modtime = fileMetaInfo.st_mtime;
1140
1141 retVal = chmod ( dstName, fileMetaInfo.st_mode );
1142 ERROR_IF_NOT_ZERO ( retVal );
1143
1144 retVal = utime ( dstName, &uTimBuf );
1145 ERROR_IF_NOT_ZERO ( retVal );
1146
1147 retVal = chown ( dstName, fileMetaInfo.st_uid, fileMetaInfo.st_gid );
1148 /* chown() will in many cases return with EPERM, which can
1149 be safely ignored.
1150 */
1151# endif
1152}
1153
1154
1155/*---------------------------------------------*/
1156static
1157Bool containsDubiousChars ( Char* name )
1158{
1159# if BZ_UNIX
1160 /* On unix, files can contain any characters and the file expansion
1161 * is performed by the shell.
1162 */
1163 return False;
1164# else /* ! BZ_UNIX */
1165 /* On non-unix (Win* platforms), wildcard characters are not allowed in
1166 * filenames.
1167 */
1168 for (; *name != '\0'; name++)
1169 if (*name == '?' || *name == '*') return True;
1170 return False;
1171# endif /* BZ_UNIX */
1172}
1173
1174
1175/*---------------------------------------------*/
1176#define BZ_N_SUFFIX_PAIRS 4
1177
1178Char* zSuffix[BZ_N_SUFFIX_PAIRS]
1179 = { ".bz2", ".bz", ".tbz2", ".tbz" };
1180Char* unzSuffix[BZ_N_SUFFIX_PAIRS]
1181 = { "", "", ".tar", ".tar" };
1182
1183static
1184Bool hasSuffix ( Char* s, Char* suffix )
1185{
1186 Int32 ns = strlen(s);
1187 Int32 nx = strlen(suffix);
1188 if (ns < nx) return False;
1189 if (strcmp(s + ns - nx, suffix) == 0) return True;
1190 return False;
1191}
1192
1193static
1194Bool mapSuffix ( Char* name,
1195 Char* oldSuffix, Char* newSuffix )
1196{
1197 if (!hasSuffix(name,oldSuffix)) return False;
1198 name[strlen(name)-strlen(oldSuffix)] = 0;
1199 strcat ( name, newSuffix );
1200 return True;
1201}
1202
1203
1204/*---------------------------------------------*/
1205static
1206void compress ( Char *name )
1207{
1208 FILE *inStr;
1209 FILE *outStr;
1210 Int32 n, i;
1211 struct MY_STAT statBuf;
1212
1213 deleteOutputOnInterrupt = False;
1214
1215 if (name == NULL && srcMode != SM_I2O)
1216 panic ( "compress: bad modes\n" );
1217
1218 switch (srcMode) {
1219 case SM_I2O:
1220 copyFileName ( inName, "(stdin)" );
1221 copyFileName ( outName, "(stdout)" );
1222 break;
1223 case SM_F2F:
1224 copyFileName ( inName, name );
1225 copyFileName ( outName, name );
1226 strcat ( outName, ".bz2" );
1227 break;
1228 case SM_F2O:
1229 copyFileName ( inName, name );
1230 copyFileName ( outName, "(stdout)" );
1231 break;
1232 }
1233
1234 if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
1235 if (noisy)
1236 fprintf ( stderr, "%s: There are no files matching `%s'.\n",
1237 progName, inName );
1238 setExit(1);
1239 return;
1240 }
1241 if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
1242 fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
1243 progName, inName, strerror(errno) );
1244 setExit(1);
1245 return;
1246 }
1247 for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++) {
1248 if (hasSuffix(inName, zSuffix[i])) {
1249 if (noisy)
1250 fprintf ( stderr,
1251 "%s: Input file %s already has %s suffix.\n",
1252 progName, inName, zSuffix[i] );
1253 setExit(1);
1254 return;
1255 }
1256 }
1257 if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
1258 MY_STAT(inName, &statBuf);
1259 if ( MY_S_ISDIR(statBuf.st_mode) ) {
1260 fprintf( stderr,
1261 "%s: Input file %s is a directory.\n",
1262 progName,inName);
1263 setExit(1);
1264 return;
1265 }
1266 }
1267 if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
1268 if (noisy)
1269 fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
1270 progName, inName );
1271 setExit(1);
1272 return;
1273 }
1274 if ( srcMode == SM_F2F && fileExists ( outName ) ) {
1275 if (forceOverwrite) {
1276 remove(outName);
1277 } else {
1278 fprintf ( stderr, "%s: Output file %s already exists.\n",
1279 progName, outName );
1280 setExit(1);
1281 return;
1282 }
1283 }
1284 if ( srcMode == SM_F2F && !forceOverwrite &&
1285 (n=countHardLinks ( inName )) > 0) {
1286 fprintf ( stderr, "%s: Input file %s has %d other link%s.\n",
1287 progName, inName, n, n > 1 ? "s" : "" );
1288 setExit(1);
1289 return;
1290 }
1291
1292 if ( srcMode == SM_F2F ) {
1293 /* Save the file's meta-info before we open it. Doing it later
1294 means we mess up the access times. */
1295 saveInputFileMetaInfo ( inName );
1296 }
1297
1298 switch ( srcMode ) {
1299
1300 case SM_I2O:
1301 inStr = stdin;
1302 outStr = stdout;
1303 if ( isatty ( fileno ( stdout ) ) ) {
1304 fprintf ( stderr,
1305 "%s: I won't write compressed data to a terminal.\n",
1306 progName );
1307 fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
1308 progName, progName );
1309 setExit(1);
1310 return;
1311 };
1312 break;
1313
1314 case SM_F2O:
1315 inStr = fopen ( inName, "rb" );
1316 outStr = stdout;
1317 if ( isatty ( fileno ( stdout ) ) ) {
1318 fprintf ( stderr,
1319 "%s: I won't write compressed data to a terminal.\n",
1320 progName );
1321 fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
1322 progName, progName );
1323 if ( inStr != NULL ) fclose ( inStr );
1324 setExit(1);
1325 return;
1326 };
1327 if ( inStr == NULL ) {
1328 fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
1329 progName, inName, strerror(errno) );
1330 setExit(1);
1331 return;
1332 };
1333 break;
1334
1335 case SM_F2F:
1336 inStr = fopen ( inName, "rb" );
1337 outStr = fopen_output_safely ( outName, "wb" );
1338 if ( outStr == NULL) {
1339 fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
1340 progName, outName, strerror(errno) );
1341 if ( inStr != NULL ) fclose ( inStr );
1342 setExit(1);
1343 return;
1344 }
1345 if ( inStr == NULL ) {
1346 fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
1347 progName, inName, strerror(errno) );
1348 if ( outStr != NULL ) fclose ( outStr );
1349 setExit(1);
1350 return;
1351 };
1352 break;
1353
1354 default:
1355 panic ( "compress: bad srcMode" );
1356 break;
1357 }
1358
1359 if (verbosity >= 1) {
1360 fprintf ( stderr, " %s: ", inName );
1361 pad ( inName );
1362 fflush ( stderr );
1363 }
1364
1365 /*--- Now the input and output handles are sane. Do the Biz. ---*/
1366 outputHandleJustInCase = outStr;
1367 deleteOutputOnInterrupt = True;
1368 compressStream ( inStr, outStr );
1369 outputHandleJustInCase = NULL;
1370
1371 /*--- If there was an I/O error, we won't get here. ---*/
1372 if ( srcMode == SM_F2F ) {
1373 applySavedMetaInfoToOutputFile ( outName );
1374 deleteOutputOnInterrupt = False;
1375 if ( !keepInputFiles ) {
1376 IntNative retVal = remove ( inName );
1377 ERROR_IF_NOT_ZERO ( retVal );
1378 }
1379 }
1380
1381 deleteOutputOnInterrupt = False;
1382}
1383
1384
1385/*---------------------------------------------*/
1386static
1387void uncompress ( Char *name )
1388{
1389 FILE *inStr;
1390 FILE *outStr;
1391 Int32 n, i;
1392 Bool magicNumberOK;
1393 Bool cantGuess;
1394 struct MY_STAT statBuf;
1395
1396 deleteOutputOnInterrupt = False;
1397
1398 if (name == NULL && srcMode != SM_I2O)
1399 panic ( "uncompress: bad modes\n" );
1400
1401 cantGuess = False;
1402 switch (srcMode) {
1403 case SM_I2O:
1404 copyFileName ( inName, "(stdin)" );
1405 copyFileName ( outName, "(stdout)" );
1406 break;
1407 case SM_F2F:
1408 copyFileName ( inName, name );
1409 copyFileName ( outName, name );
1410 for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++)
1411 if (mapSuffix(outName,zSuffix[i],unzSuffix[i]))
1412 goto zzz;
1413 cantGuess = True;
1414 strcat ( outName, ".out" );
1415 break;
1416 case SM_F2O:
1417 copyFileName ( inName, name );
1418 copyFileName ( outName, "(stdout)" );
1419 break;
1420 }
1421
1422 zzz:
1423 if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
1424 if (noisy)
1425 fprintf ( stderr, "%s: There are no files matching `%s'.\n",
1426 progName, inName );
1427 setExit(1);
1428 return;
1429 }
1430 if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
1431 fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
1432 progName, inName, strerror(errno) );
1433 setExit(1);
1434 return;
1435 }
1436 if ( srcMode == SM_F2F || srcMode == SM_F2O ) {
1437 MY_STAT(inName, &statBuf);
1438 if ( MY_S_ISDIR(statBuf.st_mode) ) {
1439 fprintf( stderr,
1440 "%s: Input file %s is a directory.\n",
1441 progName,inName);
1442 setExit(1);
1443 return;
1444 }
1445 }
1446 if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) {
1447 if (noisy)
1448 fprintf ( stderr, "%s: Input file %s is not a normal file.\n",
1449 progName, inName );
1450 setExit(1);
1451 return;
1452 }
1453 if ( /* srcMode == SM_F2F implied && */ cantGuess ) {
1454 if (noisy)
1455 fprintf ( stderr,
1456 "%s: Can't guess original name for %s -- using %s\n",
1457 progName, inName, outName );
1458 /* just a warning, no return */
1459 }
1460 if ( srcMode == SM_F2F && fileExists ( outName ) ) {
1461 if (forceOverwrite) {
1462 remove(outName);
1463 } else {
1464 fprintf ( stderr, "%s: Output file %s already exists.\n",
1465 progName, outName );
1466 setExit(1);
1467 return;
1468 }
1469 }
1470 if ( srcMode == SM_F2F && !forceOverwrite &&
1471 (n=countHardLinks ( inName ) ) > 0) {
1472 fprintf ( stderr, "%s: Input file %s has %d other link%s.\n",
1473 progName, inName, n, n > 1 ? "s" : "" );
1474 setExit(1);
1475 return;
1476 }
1477
1478 if ( srcMode == SM_F2F ) {
1479 /* Save the file's meta-info before we open it. Doing it later
1480 means we mess up the access times. */
1481 saveInputFileMetaInfo ( inName );
1482 }
1483
1484 switch ( srcMode ) {
1485
1486 case SM_I2O:
1487 inStr = stdin;
1488 outStr = stdout;
1489 if ( isatty ( fileno ( stdin ) ) ) {
1490 fprintf ( stderr,
1491 "%s: I won't read compressed data from a terminal.\n",
1492 progName );
1493 fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
1494 progName, progName );
1495 setExit(1);
1496 return;
1497 };
1498 break;
1499
1500 case SM_F2O:
1501 inStr = fopen ( inName, "rb" );
1502 outStr = stdout;
1503 if ( inStr == NULL ) {
1504 fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
1505 progName, inName, strerror(errno) );
1506 if ( inStr != NULL ) fclose ( inStr );
1507 setExit(1);
1508 return;
1509 };
1510 break;
1511
1512 case SM_F2F:
1513 inStr = fopen ( inName, "rb" );
1514 outStr = fopen_output_safely ( outName, "wb" );
1515 if ( outStr == NULL) {
1516 fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
1517 progName, outName, strerror(errno) );
1518 if ( inStr != NULL ) fclose ( inStr );
1519 setExit(1);
1520 return;
1521 }
1522 if ( inStr == NULL ) {
1523 fprintf ( stderr, "%s: Can't open input file %s: %s.\n",
1524 progName, inName, strerror(errno) );
1525 if ( outStr != NULL ) fclose ( outStr );
1526 setExit(1);
1527 return;
1528 };
1529 break;
1530
1531 default:
1532 panic ( "uncompress: bad srcMode" );
1533 break;
1534 }
1535
1536 if (verbosity >= 1) {
1537 fprintf ( stderr, " %s: ", inName );
1538 pad ( inName );
1539 fflush ( stderr );
1540 }
1541
1542 /*--- Now the input and output handles are sane. Do the Biz. ---*/
1543 outputHandleJustInCase = outStr;
1544 deleteOutputOnInterrupt = True;
1545 magicNumberOK = uncompressStream ( inStr, outStr );
1546 outputHandleJustInCase = NULL;
1547
1548 /*--- If there was an I/O error, we won't get here. ---*/
1549 if ( magicNumberOK ) {
1550 if ( srcMode == SM_F2F ) {
1551 applySavedMetaInfoToOutputFile ( outName );
1552 deleteOutputOnInterrupt = False;
1553 if ( !keepInputFiles ) {
1554 IntNative retVal = remove ( inName );
1555 ERROR_IF_NOT_ZERO ( retVal );
1556 }
1557 }
1558 } else {
1559 unzFailsExist = True;
1560 deleteOutputOnInterrupt = False;
1561 if ( srcMode == SM_F2F ) {
1562 IntNative retVal = remove ( outName );
1563 ERROR_IF_NOT_ZERO ( retVal );
1564 }
1565 }
1566 deleteOutputOnInterrupt = False;
1567
1568 if ( magicNumberOK ) {
1569 if (verbosity >= 1)
1570 fprintf ( stderr, "done\n" );
1571 } else {
1572 setExit(2);
1573 if (verbosity >= 1)
1574 fprintf ( stderr, "not a bzip2 file.\n" ); else
1575 fprintf ( stderr,
1576 "%s: %s is not a bzip2 file.\n",
1577 progName, inName );
1578 }
1579
1580}
1581
1582
1583/*---------------------------------------------*/
1584static
1585void testf ( Char *name )
1586{
1587 FILE *inStr;
1588 Bool allOK;
1589 struct MY_STAT statBuf;
1590
1591 deleteOutputOnInterrupt = False;
1592
1593 if (name == NULL && srcMode != SM_I2O)
1594 panic ( "testf: bad modes\n" );
1595
1596 copyFileName ( outName, "(none)" );
1597 switch (srcMode) {
1598 case SM_I2O: copyFileName ( inName, "(stdin)" ); break;
1599 case SM_F2F: copyFileName ( inName, name ); break;
1600 case SM_F2O: copyFileName ( inName, name ); break;
1601 }
1602
1603 if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
1604 if (noisy)
1605 fprintf ( stderr, "%s: There are no files matching `%s'.\n",
1606 progName, inName );
1607 setExit(1);
1608 return;
1609 }
1610 if ( srcMode != SM_I2O && !fileExists ( inName ) ) {
1611 fprintf ( stderr, "%s: Can't open input %s: %s.\n",
1612 progName, inName, strerror(errno) );
1613 setExit(1);
1614 return;
1615 }
1616 if ( srcMode != SM_I2O ) {
1617 MY_STAT(inName, &statBuf);
1618 if ( MY_S_ISDIR(statBuf.st_mode) ) {
1619 fprintf( stderr,
1620 "%s: Input file %s is a directory.\n",
1621 progName,inName);
1622 setExit(1);
1623 return;
1624 }
1625 }
1626
1627 switch ( srcMode ) {
1628
1629 case SM_I2O:
1630 if ( isatty ( fileno ( stdin ) ) ) {
1631 fprintf ( stderr,
1632 "%s: I won't read compressed data from a terminal.\n",
1633 progName );
1634 fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
1635 progName, progName );
1636 setExit(1);
1637 return;
1638 };
1639 inStr = stdin;
1640 break;
1641
1642 case SM_F2O: case SM_F2F:
1643 inStr = fopen ( inName, "rb" );
1644 if ( inStr == NULL ) {
1645 fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
1646 progName, inName, strerror(errno) );
1647 setExit(1);
1648 return;
1649 };
1650 break;
1651
1652 default:
1653 panic ( "testf: bad srcMode" );
1654 break;
1655 }
1656
1657 if (verbosity >= 1) {
1658 fprintf ( stderr, " %s: ", inName );
1659 pad ( inName );
1660 fflush ( stderr );
1661 }
1662
1663 /*--- Now the input handle is sane. Do the Biz. ---*/
1664 outputHandleJustInCase = NULL;
1665 allOK = testStream ( inStr );
1666
1667 if (allOK && verbosity >= 1) fprintf ( stderr, "ok\n" );
1668 if (!allOK) testFailsExist = True;
1669}
1670
1671
1672/*---------------------------------------------*/
1673static
1674void license ( void )
1675{
1676 fprintf ( stderr,
1677
1678 "bzip2, a block-sorting file compressor. "
1679 "Version %s.\n"
1680 " \n"
1677 " Copyright (C) 1996-2002 by Julian Seward.\n"
1681 " Copyright (C) 1996-2005 by Julian Seward.\n"
1678 " \n"
1679 " This program is free software; you can redistribute it and/or modify\n"
1680 " it under the terms set out in the LICENSE file, which is included\n"
1681 " in the bzip2-1.0 source distribution.\n"
1682 " \n"
1683 " This program is distributed in the hope that it will be useful,\n"
1684 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1685 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1686 " LICENSE file for more details.\n"
1687 " \n",
1688 BZ2_bzlibVersion()
1689 );
1690}
1691
1692
1693/*---------------------------------------------*/
1694static
1695void usage ( Char *fullProgName )
1696{
1697 fprintf (
1698 stderr,
1699 "bzip2, a block-sorting file compressor. "
1700 "Version %s.\n"
1701 "\n usage: %s [flags and input files in any order]\n"
1702 "\n"
1703 " -h --help print this message\n"
1704 " -d --decompress force decompression\n"
1705 " -z --compress force compression\n"
1706 " -k --keep keep (don't delete) input files\n"
1707 " -f --force overwrite existing output files\n"
1708 " -t --test test compressed file integrity\n"
1709 " -c --stdout output to standard out\n"
1710 " -q --quiet suppress noncritical error messages\n"
1711 " -v --verbose be verbose (a 2nd -v gives more)\n"
1712 " -L --license display software version & license\n"
1713 " -V --version display software version & license\n"
1714 " -s --small use less memory (at most 2500k)\n"
1715 " -1 .. -9 set block size to 100k .. 900k\n"
1716 " --fast alias for -1\n"
1717 " --best alias for -9\n"
1718 "\n"
1719 " If invoked as `bzip2', default action is to compress.\n"
1720 " as `bunzip2', default action is to decompress.\n"
1721 " as `bzcat', default action is to decompress to stdout.\n"
1722 "\n"
1723 " If no file names are given, bzip2 compresses or decompresses\n"
1724 " from standard input to standard output. You can combine\n"
1725 " short flags, so `-v -4' means the same as -v4 or -4v, &c.\n"
1726# if BZ_UNIX
1727 "\n"
1728# endif
1729 ,
1730
1731 BZ2_bzlibVersion(),
1732 fullProgName
1733 );
1734}
1735
1736
1737/*---------------------------------------------*/
1738static
1739void redundant ( Char* flag )
1740{
1741 fprintf (
1742 stderr,
1743 "%s: %s is redundant in versions 0.9.5 and above\n",
1744 progName, flag );
1745}
1746
1747
1748/*---------------------------------------------*/
1749/*--
1750 All the garbage from here to main() is purely to
1751 implement a linked list of command-line arguments,
1752 into which main() copies argv[1 .. argc-1].
1753
1754 The purpose of this exercise is to facilitate
1755 the expansion of wildcard characters * and ? in
1756 filenames for OSs which don't know how to do it
1757 themselves, like MSDOS, Windows 95 and NT.
1758
1759 The actual Dirty Work is done by the platform-
1760 specific macro APPEND_FILESPEC.
1761--*/
1762
1763typedef
1764 struct zzzz {
1765 Char *name;
1766 struct zzzz *link;
1767 }
1768 Cell;
1769
1770
1771/*---------------------------------------------*/
1772static
1773void *myMalloc ( Int32 n )
1774{
1775 void* p;
1776
1777 p = malloc ( (size_t)n );
1778 if (p == NULL) outOfMemory ();
1779 return p;
1780}
1781
1782
1783/*---------------------------------------------*/
1784static
1785Cell *mkCell ( void )
1786{
1787 Cell *c;
1788
1789 c = (Cell*) myMalloc ( sizeof ( Cell ) );
1790 c->name = NULL;
1791 c->link = NULL;
1792 return c;
1793}
1794
1795
1796/*---------------------------------------------*/
1797static
1798Cell *snocString ( Cell *root, Char *name )
1799{
1800 if (root == NULL) {
1801 Cell *tmp = mkCell();
1802 tmp->name = (Char*) myMalloc ( 5 + strlen(name) );
1803 strcpy ( tmp->name, name );
1804 return tmp;
1805 } else {
1806 Cell *tmp = root;
1807 while (tmp->link != NULL) tmp = tmp->link;
1808 tmp->link = snocString ( tmp->link, name );
1809 return root;
1810 }
1811}
1812
1813
1814/*---------------------------------------------*/
1815static
1816void addFlagsFromEnvVar ( Cell** argList, Char* varName )
1817{
1818 Int32 i, j, k;
1819 Char *envbase, *p;
1820
1821 envbase = getenv(varName);
1822 if (envbase != NULL) {
1823 p = envbase;
1824 i = 0;
1825 while (True) {
1826 if (p[i] == 0) break;
1827 p += i;
1828 i = 0;
1829 while (isspace((Int32)(p[0]))) p++;
1830 while (p[i] != 0 && !isspace((Int32)(p[i]))) i++;
1831 if (i > 0) {
1832 k = i; if (k > FILE_NAME_LEN-10) k = FILE_NAME_LEN-10;
1833 for (j = 0; j < k; j++) tmpName[j] = p[j];
1834 tmpName[k] = 0;
1835 APPEND_FLAG(*argList, tmpName);
1836 }
1837 }
1838 }
1839}
1840
1841
1842/*---------------------------------------------*/
1843#define ISFLAG(s) (strcmp(aa->name, (s))==0)
1844
1845IntNative main ( IntNative argc, Char *argv[] )
1846{
1847 Int32 i, j;
1848 Char *tmp;
1849 Cell *argList;
1850 Cell *aa;
1851 Bool decode;
1852
1853 /*-- Be really really really paranoid :-) --*/
1854 if (sizeof(Int32) != 4 || sizeof(UInt32) != 4 ||
1855 sizeof(Int16) != 2 || sizeof(UInt16) != 2 ||
1856 sizeof(Char) != 1 || sizeof(UChar) != 1)
1857 configError();
1858
1859 /*-- Initialise --*/
1860 outputHandleJustInCase = NULL;
1861 smallMode = False;
1862 keepInputFiles = False;
1863 forceOverwrite = False;
1864 noisy = True;
1865 verbosity = 0;
1866 blockSize100k = 9;
1867 testFailsExist = False;
1868 unzFailsExist = False;
1869 numFileNames = 0;
1870 numFilesProcessed = 0;
1871 workFactor = 30;
1872 deleteOutputOnInterrupt = False;
1873 exitValue = 0;
1874 i = j = 0; /* avoid bogus warning from egcs-1.1.X */
1875
1876 /*-- Set up signal handlers for mem access errors --*/
1877 signal (SIGSEGV, mySIGSEGVorSIGBUScatcher);
1878# if BZ_UNIX
1879# ifndef __DJGPP__
1880 signal (SIGBUS, mySIGSEGVorSIGBUScatcher);
1881# endif
1882# endif
1883
1884 copyFileName ( inName, "(none)" );
1885 copyFileName ( outName, "(none)" );
1886
1887 copyFileName ( progNameReally, argv[0] );
1888 progName = &progNameReally[0];
1889 for (tmp = &progNameReally[0]; *tmp != '\0'; tmp++)
1890 if (*tmp == PATH_SEP) progName = tmp + 1;
1891
1892
1893 /*-- Copy flags from env var BZIP2, and
1894 expand filename wildcards in arg list.
1895 --*/
1896 argList = NULL;
1897 addFlagsFromEnvVar ( &argList, "BZIP2" );
1898 addFlagsFromEnvVar ( &argList, "BZIP" );
1899 for (i = 1; i <= argc-1; i++)
1900 APPEND_FILESPEC(argList, argv[i]);
1901
1902
1903 /*-- Find the length of the longest filename --*/
1904 longestFileName = 7;
1905 numFileNames = 0;
1906 decode = True;
1907 for (aa = argList; aa != NULL; aa = aa->link) {
1908 if (ISFLAG("--")) { decode = False; continue; }
1909 if (aa->name[0] == '-' && decode) continue;
1910 numFileNames++;
1911 if (longestFileName < (Int32)strlen(aa->name) )
1912 longestFileName = (Int32)strlen(aa->name);
1913 }
1914
1915
1916 /*-- Determine source modes; flag handling may change this too. --*/
1917 if (numFileNames == 0)
1918 srcMode = SM_I2O; else srcMode = SM_F2F;
1919
1920
1921 /*-- Determine what to do (compress/uncompress/test/cat). --*/
1922 /*-- Note that subsequent flag handling may change this. --*/
1923 opMode = OM_Z;
1924
1925 if ( (strstr ( progName, "unzip" ) != 0) ||
1926 (strstr ( progName, "UNZIP" ) != 0) )
1927 opMode = OM_UNZ;
1928
1929 if ( (strstr ( progName, "z2cat" ) != 0) ||
1930 (strstr ( progName, "Z2CAT" ) != 0) ||
1931 (strstr ( progName, "zcat" ) != 0) ||
1932 (strstr ( progName, "ZCAT" ) != 0) ) {
1933 opMode = OM_UNZ;
1934 srcMode = (numFileNames == 0) ? SM_I2O : SM_F2O;
1935 }
1936
1937
1938 /*-- Look at the flags. --*/
1939 for (aa = argList; aa != NULL; aa = aa->link) {
1940 if (ISFLAG("--")) break;
1941 if (aa->name[0] == '-' && aa->name[1] != '-') {
1942 for (j = 1; aa->name[j] != '\0'; j++) {
1943 switch (aa->name[j]) {
1944 case 'c': srcMode = SM_F2O; break;
1945 case 'd': opMode = OM_UNZ; break;
1946 case 'z': opMode = OM_Z; break;
1947 case 'f': forceOverwrite = True; break;
1948 case 't': opMode = OM_TEST; break;
1949 case 'k': keepInputFiles = True; break;
1950 case 's': smallMode = True; break;
1951 case 'q': noisy = False; break;
1952 case '1': blockSize100k = 1; break;
1953 case '2': blockSize100k = 2; break;
1954 case '3': blockSize100k = 3; break;
1955 case '4': blockSize100k = 4; break;
1956 case '5': blockSize100k = 5; break;
1957 case '6': blockSize100k = 6; break;
1958 case '7': blockSize100k = 7; break;
1959 case '8': blockSize100k = 8; break;
1960 case '9': blockSize100k = 9; break;
1961 case 'V':
1962 case 'L': license(); break;
1963 case 'v': verbosity++; break;
1964 case 'h': usage ( progName );
1965 exit ( 0 );
1966 break;
1967 default: fprintf ( stderr, "%s: Bad flag `%s'\n",
1968 progName, aa->name );
1969 usage ( progName );
1970 exit ( 1 );
1971 break;
1972 }
1973 }
1974 }
1975 }
1976
1977 /*-- And again ... --*/
1978 for (aa = argList; aa != NULL; aa = aa->link) {
1979 if (ISFLAG("--")) break;
1980 if (ISFLAG("--stdout")) srcMode = SM_F2O; else
1981 if (ISFLAG("--decompress")) opMode = OM_UNZ; else
1982 if (ISFLAG("--compress")) opMode = OM_Z; else
1983 if (ISFLAG("--force")) forceOverwrite = True; else
1984 if (ISFLAG("--test")) opMode = OM_TEST; else
1985 if (ISFLAG("--keep")) keepInputFiles = True; else
1986 if (ISFLAG("--small")) smallMode = True; else
1987 if (ISFLAG("--quiet")) noisy = False; else
1988 if (ISFLAG("--version")) license(); else
1989 if (ISFLAG("--license")) license(); else
1990 if (ISFLAG("--exponential")) workFactor = 1; else
1991 if (ISFLAG("--repetitive-best")) redundant(aa->name); else
1992 if (ISFLAG("--repetitive-fast")) redundant(aa->name); else
1993 if (ISFLAG("--fast")) blockSize100k = 1; else
1994 if (ISFLAG("--best")) blockSize100k = 9; else
1995 if (ISFLAG("--verbose")) verbosity++; else
1996 if (ISFLAG("--help")) { usage ( progName ); exit ( 0 ); }
1997 else
1998 if (strncmp ( aa->name, "--", 2) == 0) {
1999 fprintf ( stderr, "%s: Bad flag `%s'\n", progName, aa->name );
2000 usage ( progName );
2001 exit ( 1 );
2002 }
2003 }
2004
2005 if (verbosity > 4) verbosity = 4;
2006 if (opMode == OM_Z && smallMode && blockSize100k > 2)
2007 blockSize100k = 2;
2008
2009 if (opMode == OM_TEST && srcMode == SM_F2O) {
2010 fprintf ( stderr, "%s: -c and -t cannot be used together.\n",
2011 progName );
2012 exit ( 1 );
2013 }
2014
2015 if (srcMode == SM_F2O && numFileNames == 0)
2016 srcMode = SM_I2O;
2017
2018 if (opMode != OM_Z) blockSize100k = 0;
2019
2020 if (srcMode == SM_F2F) {
2021 signal (SIGINT, mySignalCatcher);
2022 signal (SIGTERM, mySignalCatcher);
2023# if BZ_UNIX
2024 signal (SIGHUP, mySignalCatcher);
2025# endif
2026 }
2027
2028 if (opMode == OM_Z) {
2029 if (srcMode == SM_I2O) {
2030 compress ( NULL );
2031 } else {
2032 decode = True;
2033 for (aa = argList; aa != NULL; aa = aa->link) {
2034 if (ISFLAG("--")) { decode = False; continue; }
2035 if (aa->name[0] == '-' && decode) continue;
2036 numFilesProcessed++;
2037 compress ( aa->name );
2038 }
2039 }
2040 }
2041 else
2042
2043 if (opMode == OM_UNZ) {
2044 unzFailsExist = False;
2045 if (srcMode == SM_I2O) {
2046 uncompress ( NULL );
2047 } else {
2048 decode = True;
2049 for (aa = argList; aa != NULL; aa = aa->link) {
2050 if (ISFLAG("--")) { decode = False; continue; }
2051 if (aa->name[0] == '-' && decode) continue;
2052 numFilesProcessed++;
2053 uncompress ( aa->name );
2054 }
2055 }
2056 if (unzFailsExist) {
2057 setExit(2);
2058 exit(exitValue);
2059 }
2060 }
2061
2062 else {
2063 testFailsExist = False;
2064 if (srcMode == SM_I2O) {
2065 testf ( NULL );
2066 } else {
2067 decode = True;
2068 for (aa = argList; aa != NULL; aa = aa->link) {
2069 if (ISFLAG("--")) { decode = False; continue; }
2070 if (aa->name[0] == '-' && decode) continue;
2071 numFilesProcessed++;
2072 testf ( aa->name );
2073 }
2074 }
2075 if (testFailsExist && noisy) {
2076 fprintf ( stderr,
2077 "\n"
2078 "You can use the `bzip2recover' program to attempt to recover\n"
2079 "data from undamaged sections of corrupted files.\n\n"
2080 );
2081 setExit(2);
2082 exit(exitValue);
2083 }
2084 }
2085
2086 /* Free the argument list memory to mollify leak detectors
2087 (eg) Purify, Checker. Serves no other useful purpose.
2088 */
2089 aa = argList;
2090 while (aa != NULL) {
2091 Cell* aa2 = aa->link;
2092 if (aa->name != NULL) free(aa->name);
2093 free(aa);
2094 aa = aa2;
2095 }
2096
2097 return exitValue;
2098}
2099
2100
2101/*-----------------------------------------------------------*/
2102/*--- end bzip2.c ---*/
2103/*-----------------------------------------------------------*/
1682 " \n"
1683 " This program is free software; you can redistribute it and/or modify\n"
1684 " it under the terms set out in the LICENSE file, which is included\n"
1685 " in the bzip2-1.0 source distribution.\n"
1686 " \n"
1687 " This program is distributed in the hope that it will be useful,\n"
1688 " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1689 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1690 " LICENSE file for more details.\n"
1691 " \n",
1692 BZ2_bzlibVersion()
1693 );
1694}
1695
1696
1697/*---------------------------------------------*/
1698static
1699void usage ( Char *fullProgName )
1700{
1701 fprintf (
1702 stderr,
1703 "bzip2, a block-sorting file compressor. "
1704 "Version %s.\n"
1705 "\n usage: %s [flags and input files in any order]\n"
1706 "\n"
1707 " -h --help print this message\n"
1708 " -d --decompress force decompression\n"
1709 " -z --compress force compression\n"
1710 " -k --keep keep (don't delete) input files\n"
1711 " -f --force overwrite existing output files\n"
1712 " -t --test test compressed file integrity\n"
1713 " -c --stdout output to standard out\n"
1714 " -q --quiet suppress noncritical error messages\n"
1715 " -v --verbose be verbose (a 2nd -v gives more)\n"
1716 " -L --license display software version & license\n"
1717 " -V --version display software version & license\n"
1718 " -s --small use less memory (at most 2500k)\n"
1719 " -1 .. -9 set block size to 100k .. 900k\n"
1720 " --fast alias for -1\n"
1721 " --best alias for -9\n"
1722 "\n"
1723 " If invoked as `bzip2', default action is to compress.\n"
1724 " as `bunzip2', default action is to decompress.\n"
1725 " as `bzcat', default action is to decompress to stdout.\n"
1726 "\n"
1727 " If no file names are given, bzip2 compresses or decompresses\n"
1728 " from standard input to standard output. You can combine\n"
1729 " short flags, so `-v -4' means the same as -v4 or -4v, &c.\n"
1730# if BZ_UNIX
1731 "\n"
1732# endif
1733 ,
1734
1735 BZ2_bzlibVersion(),
1736 fullProgName
1737 );
1738}
1739
1740
1741/*---------------------------------------------*/
1742static
1743void redundant ( Char* flag )
1744{
1745 fprintf (
1746 stderr,
1747 "%s: %s is redundant in versions 0.9.5 and above\n",
1748 progName, flag );
1749}
1750
1751
1752/*---------------------------------------------*/
1753/*--
1754 All the garbage from here to main() is purely to
1755 implement a linked list of command-line arguments,
1756 into which main() copies argv[1 .. argc-1].
1757
1758 The purpose of this exercise is to facilitate
1759 the expansion of wildcard characters * and ? in
1760 filenames for OSs which don't know how to do it
1761 themselves, like MSDOS, Windows 95 and NT.
1762
1763 The actual Dirty Work is done by the platform-
1764 specific macro APPEND_FILESPEC.
1765--*/
1766
1767typedef
1768 struct zzzz {
1769 Char *name;
1770 struct zzzz *link;
1771 }
1772 Cell;
1773
1774
1775/*---------------------------------------------*/
1776static
1777void *myMalloc ( Int32 n )
1778{
1779 void* p;
1780
1781 p = malloc ( (size_t)n );
1782 if (p == NULL) outOfMemory ();
1783 return p;
1784}
1785
1786
1787/*---------------------------------------------*/
1788static
1789Cell *mkCell ( void )
1790{
1791 Cell *c;
1792
1793 c = (Cell*) myMalloc ( sizeof ( Cell ) );
1794 c->name = NULL;
1795 c->link = NULL;
1796 return c;
1797}
1798
1799
1800/*---------------------------------------------*/
1801static
1802Cell *snocString ( Cell *root, Char *name )
1803{
1804 if (root == NULL) {
1805 Cell *tmp = mkCell();
1806 tmp->name = (Char*) myMalloc ( 5 + strlen(name) );
1807 strcpy ( tmp->name, name );
1808 return tmp;
1809 } else {
1810 Cell *tmp = root;
1811 while (tmp->link != NULL) tmp = tmp->link;
1812 tmp->link = snocString ( tmp->link, name );
1813 return root;
1814 }
1815}
1816
1817
1818/*---------------------------------------------*/
1819static
1820void addFlagsFromEnvVar ( Cell** argList, Char* varName )
1821{
1822 Int32 i, j, k;
1823 Char *envbase, *p;
1824
1825 envbase = getenv(varName);
1826 if (envbase != NULL) {
1827 p = envbase;
1828 i = 0;
1829 while (True) {
1830 if (p[i] == 0) break;
1831 p += i;
1832 i = 0;
1833 while (isspace((Int32)(p[0]))) p++;
1834 while (p[i] != 0 && !isspace((Int32)(p[i]))) i++;
1835 if (i > 0) {
1836 k = i; if (k > FILE_NAME_LEN-10) k = FILE_NAME_LEN-10;
1837 for (j = 0; j < k; j++) tmpName[j] = p[j];
1838 tmpName[k] = 0;
1839 APPEND_FLAG(*argList, tmpName);
1840 }
1841 }
1842 }
1843}
1844
1845
1846/*---------------------------------------------*/
1847#define ISFLAG(s) (strcmp(aa->name, (s))==0)
1848
1849IntNative main ( IntNative argc, Char *argv[] )
1850{
1851 Int32 i, j;
1852 Char *tmp;
1853 Cell *argList;
1854 Cell *aa;
1855 Bool decode;
1856
1857 /*-- Be really really really paranoid :-) --*/
1858 if (sizeof(Int32) != 4 || sizeof(UInt32) != 4 ||
1859 sizeof(Int16) != 2 || sizeof(UInt16) != 2 ||
1860 sizeof(Char) != 1 || sizeof(UChar) != 1)
1861 configError();
1862
1863 /*-- Initialise --*/
1864 outputHandleJustInCase = NULL;
1865 smallMode = False;
1866 keepInputFiles = False;
1867 forceOverwrite = False;
1868 noisy = True;
1869 verbosity = 0;
1870 blockSize100k = 9;
1871 testFailsExist = False;
1872 unzFailsExist = False;
1873 numFileNames = 0;
1874 numFilesProcessed = 0;
1875 workFactor = 30;
1876 deleteOutputOnInterrupt = False;
1877 exitValue = 0;
1878 i = j = 0; /* avoid bogus warning from egcs-1.1.X */
1879
1880 /*-- Set up signal handlers for mem access errors --*/
1881 signal (SIGSEGV, mySIGSEGVorSIGBUScatcher);
1882# if BZ_UNIX
1883# ifndef __DJGPP__
1884 signal (SIGBUS, mySIGSEGVorSIGBUScatcher);
1885# endif
1886# endif
1887
1888 copyFileName ( inName, "(none)" );
1889 copyFileName ( outName, "(none)" );
1890
1891 copyFileName ( progNameReally, argv[0] );
1892 progName = &progNameReally[0];
1893 for (tmp = &progNameReally[0]; *tmp != '\0'; tmp++)
1894 if (*tmp == PATH_SEP) progName = tmp + 1;
1895
1896
1897 /*-- Copy flags from env var BZIP2, and
1898 expand filename wildcards in arg list.
1899 --*/
1900 argList = NULL;
1901 addFlagsFromEnvVar ( &argList, "BZIP2" );
1902 addFlagsFromEnvVar ( &argList, "BZIP" );
1903 for (i = 1; i <= argc-1; i++)
1904 APPEND_FILESPEC(argList, argv[i]);
1905
1906
1907 /*-- Find the length of the longest filename --*/
1908 longestFileName = 7;
1909 numFileNames = 0;
1910 decode = True;
1911 for (aa = argList; aa != NULL; aa = aa->link) {
1912 if (ISFLAG("--")) { decode = False; continue; }
1913 if (aa->name[0] == '-' && decode) continue;
1914 numFileNames++;
1915 if (longestFileName < (Int32)strlen(aa->name) )
1916 longestFileName = (Int32)strlen(aa->name);
1917 }
1918
1919
1920 /*-- Determine source modes; flag handling may change this too. --*/
1921 if (numFileNames == 0)
1922 srcMode = SM_I2O; else srcMode = SM_F2F;
1923
1924
1925 /*-- Determine what to do (compress/uncompress/test/cat). --*/
1926 /*-- Note that subsequent flag handling may change this. --*/
1927 opMode = OM_Z;
1928
1929 if ( (strstr ( progName, "unzip" ) != 0) ||
1930 (strstr ( progName, "UNZIP" ) != 0) )
1931 opMode = OM_UNZ;
1932
1933 if ( (strstr ( progName, "z2cat" ) != 0) ||
1934 (strstr ( progName, "Z2CAT" ) != 0) ||
1935 (strstr ( progName, "zcat" ) != 0) ||
1936 (strstr ( progName, "ZCAT" ) != 0) ) {
1937 opMode = OM_UNZ;
1938 srcMode = (numFileNames == 0) ? SM_I2O : SM_F2O;
1939 }
1940
1941
1942 /*-- Look at the flags. --*/
1943 for (aa = argList; aa != NULL; aa = aa->link) {
1944 if (ISFLAG("--")) break;
1945 if (aa->name[0] == '-' && aa->name[1] != '-') {
1946 for (j = 1; aa->name[j] != '\0'; j++) {
1947 switch (aa->name[j]) {
1948 case 'c': srcMode = SM_F2O; break;
1949 case 'd': opMode = OM_UNZ; break;
1950 case 'z': opMode = OM_Z; break;
1951 case 'f': forceOverwrite = True; break;
1952 case 't': opMode = OM_TEST; break;
1953 case 'k': keepInputFiles = True; break;
1954 case 's': smallMode = True; break;
1955 case 'q': noisy = False; break;
1956 case '1': blockSize100k = 1; break;
1957 case '2': blockSize100k = 2; break;
1958 case '3': blockSize100k = 3; break;
1959 case '4': blockSize100k = 4; break;
1960 case '5': blockSize100k = 5; break;
1961 case '6': blockSize100k = 6; break;
1962 case '7': blockSize100k = 7; break;
1963 case '8': blockSize100k = 8; break;
1964 case '9': blockSize100k = 9; break;
1965 case 'V':
1966 case 'L': license(); break;
1967 case 'v': verbosity++; break;
1968 case 'h': usage ( progName );
1969 exit ( 0 );
1970 break;
1971 default: fprintf ( stderr, "%s: Bad flag `%s'\n",
1972 progName, aa->name );
1973 usage ( progName );
1974 exit ( 1 );
1975 break;
1976 }
1977 }
1978 }
1979 }
1980
1981 /*-- And again ... --*/
1982 for (aa = argList; aa != NULL; aa = aa->link) {
1983 if (ISFLAG("--")) break;
1984 if (ISFLAG("--stdout")) srcMode = SM_F2O; else
1985 if (ISFLAG("--decompress")) opMode = OM_UNZ; else
1986 if (ISFLAG("--compress")) opMode = OM_Z; else
1987 if (ISFLAG("--force")) forceOverwrite = True; else
1988 if (ISFLAG("--test")) opMode = OM_TEST; else
1989 if (ISFLAG("--keep")) keepInputFiles = True; else
1990 if (ISFLAG("--small")) smallMode = True; else
1991 if (ISFLAG("--quiet")) noisy = False; else
1992 if (ISFLAG("--version")) license(); else
1993 if (ISFLAG("--license")) license(); else
1994 if (ISFLAG("--exponential")) workFactor = 1; else
1995 if (ISFLAG("--repetitive-best")) redundant(aa->name); else
1996 if (ISFLAG("--repetitive-fast")) redundant(aa->name); else
1997 if (ISFLAG("--fast")) blockSize100k = 1; else
1998 if (ISFLAG("--best")) blockSize100k = 9; else
1999 if (ISFLAG("--verbose")) verbosity++; else
2000 if (ISFLAG("--help")) { usage ( progName ); exit ( 0 ); }
2001 else
2002 if (strncmp ( aa->name, "--", 2) == 0) {
2003 fprintf ( stderr, "%s: Bad flag `%s'\n", progName, aa->name );
2004 usage ( progName );
2005 exit ( 1 );
2006 }
2007 }
2008
2009 if (verbosity > 4) verbosity = 4;
2010 if (opMode == OM_Z && smallMode && blockSize100k > 2)
2011 blockSize100k = 2;
2012
2013 if (opMode == OM_TEST && srcMode == SM_F2O) {
2014 fprintf ( stderr, "%s: -c and -t cannot be used together.\n",
2015 progName );
2016 exit ( 1 );
2017 }
2018
2019 if (srcMode == SM_F2O && numFileNames == 0)
2020 srcMode = SM_I2O;
2021
2022 if (opMode != OM_Z) blockSize100k = 0;
2023
2024 if (srcMode == SM_F2F) {
2025 signal (SIGINT, mySignalCatcher);
2026 signal (SIGTERM, mySignalCatcher);
2027# if BZ_UNIX
2028 signal (SIGHUP, mySignalCatcher);
2029# endif
2030 }
2031
2032 if (opMode == OM_Z) {
2033 if (srcMode == SM_I2O) {
2034 compress ( NULL );
2035 } else {
2036 decode = True;
2037 for (aa = argList; aa != NULL; aa = aa->link) {
2038 if (ISFLAG("--")) { decode = False; continue; }
2039 if (aa->name[0] == '-' && decode) continue;
2040 numFilesProcessed++;
2041 compress ( aa->name );
2042 }
2043 }
2044 }
2045 else
2046
2047 if (opMode == OM_UNZ) {
2048 unzFailsExist = False;
2049 if (srcMode == SM_I2O) {
2050 uncompress ( NULL );
2051 } else {
2052 decode = True;
2053 for (aa = argList; aa != NULL; aa = aa->link) {
2054 if (ISFLAG("--")) { decode = False; continue; }
2055 if (aa->name[0] == '-' && decode) continue;
2056 numFilesProcessed++;
2057 uncompress ( aa->name );
2058 }
2059 }
2060 if (unzFailsExist) {
2061 setExit(2);
2062 exit(exitValue);
2063 }
2064 }
2065
2066 else {
2067 testFailsExist = False;
2068 if (srcMode == SM_I2O) {
2069 testf ( NULL );
2070 } else {
2071 decode = True;
2072 for (aa = argList; aa != NULL; aa = aa->link) {
2073 if (ISFLAG("--")) { decode = False; continue; }
2074 if (aa->name[0] == '-' && decode) continue;
2075 numFilesProcessed++;
2076 testf ( aa->name );
2077 }
2078 }
2079 if (testFailsExist && noisy) {
2080 fprintf ( stderr,
2081 "\n"
2082 "You can use the `bzip2recover' program to attempt to recover\n"
2083 "data from undamaged sections of corrupted files.\n\n"
2084 );
2085 setExit(2);
2086 exit(exitValue);
2087 }
2088 }
2089
2090 /* Free the argument list memory to mollify leak detectors
2091 (eg) Purify, Checker. Serves no other useful purpose.
2092 */
2093 aa = argList;
2094 while (aa != NULL) {
2095 Cell* aa2 = aa->link;
2096 if (aa->name != NULL) free(aa->name);
2097 free(aa);
2098 aa = aa2;
2099 }
2100
2101 return exitValue;
2102}
2103
2104
2105/*-----------------------------------------------------------*/
2106/*--- end bzip2.c ---*/
2107/*-----------------------------------------------------------*/