Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2108 lines
60 KiB

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