Chris@4: Chris@4: /*-----------------------------------------------------------*/ Chris@4: /*--- A block-sorting, lossless compressor bzip2.c ---*/ Chris@4: /*-----------------------------------------------------------*/ Chris@4: Chris@4: /* ------------------------------------------------------------------ Chris@4: This file is part of bzip2/libbzip2, a program and library for Chris@4: lossless, block-sorting data compression. Chris@4: Chris@4: bzip2/libbzip2 version 1.0.6 of 6 September 2010 Chris@4: Copyright (C) 1996-2010 Julian Seward Chris@4: Chris@4: Please read the WARNING, DISCLAIMER and PATENTS sections in the Chris@4: README file. Chris@4: Chris@4: This program is released under the terms of the license contained Chris@4: in the file LICENSE. Chris@4: ------------------------------------------------------------------ */ Chris@4: Chris@4: Chris@4: /* Place a 1 beside your platform, and 0 elsewhere. Chris@4: Generic 32-bit Unix. Chris@4: Also works on 64-bit Unix boxes. Chris@4: This is the default. Chris@4: */ Chris@4: #define BZ_UNIX 1 Chris@4: Chris@4: /*-- Chris@4: Win32, as seen by Jacob Navia's excellent Chris@4: port of (Chris Fraser & David Hanson)'s excellent Chris@4: lcc compiler. Or with MS Visual C. Chris@4: This is selected automatically if compiled by a compiler which Chris@4: defines _WIN32, not including the Cygwin GCC. Chris@4: --*/ Chris@4: #define BZ_LCCWIN32 0 Chris@4: Chris@4: #if defined(_WIN32) && !defined(__CYGWIN__) Chris@4: #undef BZ_LCCWIN32 Chris@4: #define BZ_LCCWIN32 1 Chris@4: #undef BZ_UNIX Chris@4: #define BZ_UNIX 0 Chris@4: #endif Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: /*-- Chris@4: Some stuff for all platforms. Chris@4: --*/ Chris@4: Chris@4: #include Chris@4: #include Chris@4: #include Chris@4: #include Chris@4: #include Chris@4: #include Chris@4: #include Chris@4: #include "bzlib.h" Chris@4: Chris@4: #define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); } Chris@4: #define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) ioError(); } Chris@4: #define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) ioError(); } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: /*-- Chris@4: Platform-specific stuff. Chris@4: --*/ Chris@4: Chris@4: #if BZ_UNIX Chris@4: # include Chris@4: # include Chris@4: # include Chris@4: # include Chris@4: # include Chris@4: # include Chris@4: Chris@4: # define PATH_SEP '/' Chris@4: # define MY_LSTAT lstat Chris@4: # define MY_STAT stat Chris@4: # define MY_S_ISREG S_ISREG Chris@4: # define MY_S_ISDIR S_ISDIR Chris@4: Chris@4: # define APPEND_FILESPEC(root, name) \ Chris@4: root=snocString((root), (name)) Chris@4: Chris@4: # define APPEND_FLAG(root, name) \ Chris@4: root=snocString((root), (name)) Chris@4: Chris@4: # define SET_BINARY_MODE(fd) /**/ Chris@4: Chris@4: # ifdef __GNUC__ Chris@4: # define NORETURN __attribute__ ((noreturn)) Chris@4: # else Chris@4: # define NORETURN /**/ Chris@4: # endif Chris@4: Chris@4: # ifdef __DJGPP__ Chris@4: # include Chris@4: # include Chris@4: # undef MY_LSTAT Chris@4: # undef MY_STAT Chris@4: # define MY_LSTAT stat Chris@4: # define MY_STAT stat Chris@4: # undef SET_BINARY_MODE Chris@4: # define SET_BINARY_MODE(fd) \ Chris@4: do { \ Chris@4: int retVal = setmode ( fileno ( fd ), \ Chris@4: O_BINARY ); \ Chris@4: ERROR_IF_MINUS_ONE ( retVal ); \ Chris@4: } while ( 0 ) Chris@4: # endif Chris@4: Chris@4: # ifdef __CYGWIN__ Chris@4: # include Chris@4: # include Chris@4: # undef SET_BINARY_MODE Chris@4: # define SET_BINARY_MODE(fd) \ Chris@4: do { \ Chris@4: int retVal = setmode ( fileno ( fd ), \ Chris@4: O_BINARY ); \ Chris@4: ERROR_IF_MINUS_ONE ( retVal ); \ Chris@4: } while ( 0 ) Chris@4: # endif Chris@4: #endif /* BZ_UNIX */ Chris@4: Chris@4: Chris@4: Chris@4: #if BZ_LCCWIN32 Chris@4: # include Chris@4: # include Chris@4: # include Chris@4: Chris@4: # define NORETURN /**/ Chris@4: # define PATH_SEP '\\' Chris@4: # define MY_LSTAT _stat Chris@4: # define MY_STAT _stat Chris@4: # define MY_S_ISREG(x) ((x) & _S_IFREG) Chris@4: # define MY_S_ISDIR(x) ((x) & _S_IFDIR) Chris@4: Chris@4: # define APPEND_FLAG(root, name) \ Chris@4: root=snocString((root), (name)) Chris@4: Chris@4: # define APPEND_FILESPEC(root, name) \ Chris@4: root = snocString ((root), (name)) Chris@4: Chris@4: # define SET_BINARY_MODE(fd) \ Chris@4: do { \ Chris@4: int retVal = setmode ( fileno ( fd ), \ Chris@4: O_BINARY ); \ Chris@4: ERROR_IF_MINUS_ONE ( retVal ); \ Chris@4: } while ( 0 ) Chris@4: Chris@4: #endif /* BZ_LCCWIN32 */ Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: /*-- Chris@4: Some more stuff for all platforms :-) Chris@4: --*/ Chris@4: Chris@4: typedef char Char; Chris@4: typedef unsigned char Bool; Chris@4: typedef unsigned char UChar; Chris@4: typedef int Int32; Chris@4: typedef unsigned int UInt32; Chris@4: typedef short Int16; Chris@4: typedef unsigned short UInt16; Chris@4: Chris@4: #define True ((Bool)1) Chris@4: #define False ((Bool)0) Chris@4: Chris@4: /*-- Chris@4: IntNative is your platform's `native' int size. Chris@4: Only here to avoid probs with 64-bit platforms. Chris@4: --*/ Chris@4: typedef int IntNative; Chris@4: Chris@4: Chris@4: /*---------------------------------------------------*/ Chris@4: /*--- Misc (file handling) data decls ---*/ Chris@4: /*---------------------------------------------------*/ Chris@4: Chris@4: Int32 verbosity; Chris@4: Bool keepInputFiles, smallMode, deleteOutputOnInterrupt; Chris@4: Bool forceOverwrite, testFailsExist, unzFailsExist, noisy; Chris@4: Int32 numFileNames, numFilesProcessed, blockSize100k; Chris@4: Int32 exitValue; Chris@4: Chris@4: /*-- source modes; F==file, I==stdin, O==stdout --*/ Chris@4: #define SM_I2O 1 Chris@4: #define SM_F2O 2 Chris@4: #define SM_F2F 3 Chris@4: Chris@4: /*-- operation modes --*/ Chris@4: #define OM_Z 1 Chris@4: #define OM_UNZ 2 Chris@4: #define OM_TEST 3 Chris@4: Chris@4: Int32 opMode; Chris@4: Int32 srcMode; Chris@4: Chris@4: #define FILE_NAME_LEN 1034 Chris@4: Chris@4: Int32 longestFileName; Chris@4: Char inName [FILE_NAME_LEN]; Chris@4: Char outName[FILE_NAME_LEN]; Chris@4: Char tmpName[FILE_NAME_LEN]; Chris@4: Char *progName; Chris@4: Char progNameReally[FILE_NAME_LEN]; Chris@4: FILE *outputHandleJustInCase; Chris@4: Int32 workFactor; Chris@4: Chris@4: static void panic ( const Char* ) NORETURN; Chris@4: static void ioError ( void ) NORETURN; Chris@4: static void outOfMemory ( void ) NORETURN; Chris@4: static void configError ( void ) NORETURN; Chris@4: static void crcError ( void ) NORETURN; Chris@4: static void cleanUpAndFail ( Int32 ) NORETURN; Chris@4: static void compressedStreamEOF ( void ) NORETURN; Chris@4: Chris@4: static void copyFileName ( Char*, Char* ); Chris@4: static void* myMalloc ( Int32 ); Chris@4: static void applySavedFileAttrToOutputFile ( IntNative fd ); Chris@4: Chris@4: Chris@4: Chris@4: /*---------------------------------------------------*/ Chris@4: /*--- An implementation of 64-bit ints. Sigh. ---*/ Chris@4: /*--- Roll on widespread deployment of ANSI C9X ! ---*/ Chris@4: /*---------------------------------------------------*/ Chris@4: Chris@4: typedef Chris@4: struct { UChar b[8]; } Chris@4: UInt64; Chris@4: Chris@4: Chris@4: static Chris@4: void uInt64_from_UInt32s ( UInt64* n, UInt32 lo32, UInt32 hi32 ) Chris@4: { Chris@4: n->b[7] = (UChar)((hi32 >> 24) & 0xFF); Chris@4: n->b[6] = (UChar)((hi32 >> 16) & 0xFF); Chris@4: n->b[5] = (UChar)((hi32 >> 8) & 0xFF); Chris@4: n->b[4] = (UChar) (hi32 & 0xFF); Chris@4: n->b[3] = (UChar)((lo32 >> 24) & 0xFF); Chris@4: n->b[2] = (UChar)((lo32 >> 16) & 0xFF); Chris@4: n->b[1] = (UChar)((lo32 >> 8) & 0xFF); Chris@4: n->b[0] = (UChar) (lo32 & 0xFF); Chris@4: } Chris@4: Chris@4: Chris@4: static Chris@4: double uInt64_to_double ( UInt64* n ) Chris@4: { Chris@4: Int32 i; Chris@4: double base = 1.0; Chris@4: double sum = 0.0; Chris@4: for (i = 0; i < 8; i++) { Chris@4: sum += base * (double)(n->b[i]); Chris@4: base *= 256.0; Chris@4: } Chris@4: return sum; Chris@4: } Chris@4: Chris@4: Chris@4: static Chris@4: Bool uInt64_isZero ( UInt64* n ) Chris@4: { Chris@4: Int32 i; Chris@4: for (i = 0; i < 8; i++) Chris@4: if (n->b[i] != 0) return 0; Chris@4: return 1; Chris@4: } Chris@4: Chris@4: Chris@4: /* Divide *n by 10, and return the remainder. */ Chris@4: static Chris@4: Int32 uInt64_qrm10 ( UInt64* n ) Chris@4: { Chris@4: UInt32 rem, tmp; Chris@4: Int32 i; Chris@4: rem = 0; Chris@4: for (i = 7; i >= 0; i--) { Chris@4: tmp = rem * 256 + n->b[i]; Chris@4: n->b[i] = tmp / 10; Chris@4: rem = tmp % 10; Chris@4: } Chris@4: return rem; Chris@4: } Chris@4: Chris@4: Chris@4: /* ... and the Whole Entire Point of all this UInt64 stuff is Chris@4: so that we can supply the following function. Chris@4: */ Chris@4: static Chris@4: void uInt64_toAscii ( char* outbuf, UInt64* n ) Chris@4: { Chris@4: Int32 i, q; Chris@4: UChar buf[32]; Chris@4: Int32 nBuf = 0; Chris@4: UInt64 n_copy = *n; Chris@4: do { Chris@4: q = uInt64_qrm10 ( &n_copy ); Chris@4: buf[nBuf] = q + '0'; Chris@4: nBuf++; Chris@4: } while (!uInt64_isZero(&n_copy)); Chris@4: outbuf[nBuf] = 0; Chris@4: for (i = 0; i < nBuf; i++) Chris@4: outbuf[i] = buf[nBuf-i-1]; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------------*/ Chris@4: /*--- Processing of complete files and streams ---*/ Chris@4: /*---------------------------------------------------*/ Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: Bool myfeof ( FILE* f ) Chris@4: { Chris@4: Int32 c = fgetc ( f ); Chris@4: if (c == EOF) return True; Chris@4: ungetc ( c, f ); Chris@4: return False; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void compressStream ( FILE *stream, FILE *zStream ) Chris@4: { Chris@4: BZFILE* bzf = NULL; Chris@4: UChar ibuf[5000]; Chris@4: Int32 nIbuf; Chris@4: UInt32 nbytes_in_lo32, nbytes_in_hi32; Chris@4: UInt32 nbytes_out_lo32, nbytes_out_hi32; Chris@4: Int32 bzerr, bzerr_dummy, ret; Chris@4: Chris@4: SET_BINARY_MODE(stream); Chris@4: SET_BINARY_MODE(zStream); Chris@4: Chris@4: if (ferror(stream)) goto errhandler_io; Chris@4: if (ferror(zStream)) goto errhandler_io; Chris@4: Chris@4: bzf = BZ2_bzWriteOpen ( &bzerr, zStream, Chris@4: blockSize100k, verbosity, workFactor ); Chris@4: if (bzerr != BZ_OK) goto errhandler; Chris@4: Chris@4: if (verbosity >= 2) fprintf ( stderr, "\n" ); Chris@4: Chris@4: while (True) { Chris@4: Chris@4: if (myfeof(stream)) break; Chris@4: nIbuf = fread ( ibuf, sizeof(UChar), 5000, stream ); Chris@4: if (ferror(stream)) goto errhandler_io; Chris@4: if (nIbuf > 0) BZ2_bzWrite ( &bzerr, bzf, (void*)ibuf, nIbuf ); Chris@4: if (bzerr != BZ_OK) goto errhandler; Chris@4: Chris@4: } Chris@4: Chris@4: BZ2_bzWriteClose64 ( &bzerr, bzf, 0, Chris@4: &nbytes_in_lo32, &nbytes_in_hi32, Chris@4: &nbytes_out_lo32, &nbytes_out_hi32 ); Chris@4: if (bzerr != BZ_OK) goto errhandler; Chris@4: Chris@4: if (ferror(zStream)) goto errhandler_io; Chris@4: ret = fflush ( zStream ); Chris@4: if (ret == EOF) goto errhandler_io; Chris@4: if (zStream != stdout) { Chris@4: Int32 fd = fileno ( zStream ); Chris@4: if (fd < 0) goto errhandler_io; Chris@4: applySavedFileAttrToOutputFile ( fd ); Chris@4: ret = fclose ( zStream ); Chris@4: outputHandleJustInCase = NULL; Chris@4: if (ret == EOF) goto errhandler_io; Chris@4: } Chris@4: outputHandleJustInCase = NULL; Chris@4: if (ferror(stream)) goto errhandler_io; Chris@4: ret = fclose ( stream ); Chris@4: if (ret == EOF) goto errhandler_io; Chris@4: Chris@4: if (verbosity >= 1) { Chris@4: if (nbytes_in_lo32 == 0 && nbytes_in_hi32 == 0) { Chris@4: fprintf ( stderr, " no data compressed.\n"); Chris@4: } else { Chris@4: Char buf_nin[32], buf_nout[32]; Chris@4: UInt64 nbytes_in, nbytes_out; Chris@4: double nbytes_in_d, nbytes_out_d; Chris@4: uInt64_from_UInt32s ( &nbytes_in, Chris@4: nbytes_in_lo32, nbytes_in_hi32 ); Chris@4: uInt64_from_UInt32s ( &nbytes_out, Chris@4: nbytes_out_lo32, nbytes_out_hi32 ); Chris@4: nbytes_in_d = uInt64_to_double ( &nbytes_in ); Chris@4: nbytes_out_d = uInt64_to_double ( &nbytes_out ); Chris@4: uInt64_toAscii ( buf_nin, &nbytes_in ); Chris@4: uInt64_toAscii ( buf_nout, &nbytes_out ); Chris@4: fprintf ( stderr, "%6.3f:1, %6.3f bits/byte, " Chris@4: "%5.2f%% saved, %s in, %s out.\n", Chris@4: nbytes_in_d / nbytes_out_d, Chris@4: (8.0 * nbytes_out_d) / nbytes_in_d, Chris@4: 100.0 * (1.0 - nbytes_out_d / nbytes_in_d), Chris@4: buf_nin, Chris@4: buf_nout Chris@4: ); Chris@4: } Chris@4: } Chris@4: Chris@4: return; Chris@4: Chris@4: errhandler: Chris@4: BZ2_bzWriteClose64 ( &bzerr_dummy, bzf, 1, Chris@4: &nbytes_in_lo32, &nbytes_in_hi32, Chris@4: &nbytes_out_lo32, &nbytes_out_hi32 ); Chris@4: switch (bzerr) { Chris@4: case BZ_CONFIG_ERROR: Chris@4: configError(); break; Chris@4: case BZ_MEM_ERROR: Chris@4: outOfMemory (); break; Chris@4: case BZ_IO_ERROR: Chris@4: errhandler_io: Chris@4: ioError(); break; Chris@4: default: Chris@4: panic ( "compress:unexpected error" ); Chris@4: } Chris@4: Chris@4: panic ( "compress:end" ); Chris@4: /*notreached*/ Chris@4: } Chris@4: Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: Bool uncompressStream ( FILE *zStream, FILE *stream ) Chris@4: { Chris@4: BZFILE* bzf = NULL; Chris@4: Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i; Chris@4: UChar obuf[5000]; Chris@4: UChar unused[BZ_MAX_UNUSED]; Chris@4: Int32 nUnused; Chris@4: void* unusedTmpV; Chris@4: UChar* unusedTmp; Chris@4: Chris@4: nUnused = 0; Chris@4: streamNo = 0; Chris@4: Chris@4: SET_BINARY_MODE(stream); Chris@4: SET_BINARY_MODE(zStream); Chris@4: Chris@4: if (ferror(stream)) goto errhandler_io; Chris@4: if (ferror(zStream)) goto errhandler_io; Chris@4: Chris@4: while (True) { Chris@4: Chris@4: bzf = BZ2_bzReadOpen ( Chris@4: &bzerr, zStream, verbosity, Chris@4: (int)smallMode, unused, nUnused Chris@4: ); Chris@4: if (bzf == NULL || bzerr != BZ_OK) goto errhandler; Chris@4: streamNo++; Chris@4: Chris@4: while (bzerr == BZ_OK) { Chris@4: nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 ); Chris@4: if (bzerr == BZ_DATA_ERROR_MAGIC) goto trycat; Chris@4: if ((bzerr == BZ_OK || bzerr == BZ_STREAM_END) && nread > 0) Chris@4: fwrite ( obuf, sizeof(UChar), nread, stream ); Chris@4: if (ferror(stream)) goto errhandler_io; Chris@4: } Chris@4: if (bzerr != BZ_STREAM_END) goto errhandler; Chris@4: Chris@4: BZ2_bzReadGetUnused ( &bzerr, bzf, &unusedTmpV, &nUnused ); Chris@4: if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" ); Chris@4: Chris@4: unusedTmp = (UChar*)unusedTmpV; Chris@4: for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i]; Chris@4: Chris@4: BZ2_bzReadClose ( &bzerr, bzf ); Chris@4: if (bzerr != BZ_OK) panic ( "decompress:bzReadGetUnused" ); Chris@4: Chris@4: if (nUnused == 0 && myfeof(zStream)) break; Chris@4: } Chris@4: Chris@4: closeok: Chris@4: if (ferror(zStream)) goto errhandler_io; Chris@4: if (stream != stdout) { Chris@4: Int32 fd = fileno ( stream ); Chris@4: if (fd < 0) goto errhandler_io; Chris@4: applySavedFileAttrToOutputFile ( fd ); Chris@4: } Chris@4: ret = fclose ( zStream ); Chris@4: if (ret == EOF) goto errhandler_io; Chris@4: Chris@4: if (ferror(stream)) goto errhandler_io; Chris@4: ret = fflush ( stream ); Chris@4: if (ret != 0) goto errhandler_io; Chris@4: if (stream != stdout) { Chris@4: ret = fclose ( stream ); Chris@4: outputHandleJustInCase = NULL; Chris@4: if (ret == EOF) goto errhandler_io; Chris@4: } Chris@4: outputHandleJustInCase = NULL; Chris@4: if (verbosity >= 2) fprintf ( stderr, "\n " ); Chris@4: return True; Chris@4: Chris@4: trycat: Chris@4: if (forceOverwrite) { Chris@4: rewind(zStream); Chris@4: while (True) { Chris@4: if (myfeof(zStream)) break; Chris@4: nread = fread ( obuf, sizeof(UChar), 5000, zStream ); Chris@4: if (ferror(zStream)) goto errhandler_io; Chris@4: if (nread > 0) fwrite ( obuf, sizeof(UChar), nread, stream ); Chris@4: if (ferror(stream)) goto errhandler_io; Chris@4: } Chris@4: goto closeok; Chris@4: } Chris@4: Chris@4: errhandler: Chris@4: BZ2_bzReadClose ( &bzerr_dummy, bzf ); Chris@4: switch (bzerr) { Chris@4: case BZ_CONFIG_ERROR: Chris@4: configError(); break; Chris@4: case BZ_IO_ERROR: Chris@4: errhandler_io: Chris@4: ioError(); break; Chris@4: case BZ_DATA_ERROR: Chris@4: crcError(); Chris@4: case BZ_MEM_ERROR: Chris@4: outOfMemory(); Chris@4: case BZ_UNEXPECTED_EOF: Chris@4: compressedStreamEOF(); Chris@4: case BZ_DATA_ERROR_MAGIC: Chris@4: if (zStream != stdin) fclose(zStream); Chris@4: if (stream != stdout) fclose(stream); Chris@4: if (streamNo == 1) { Chris@4: return False; Chris@4: } else { Chris@4: if (noisy) Chris@4: fprintf ( stderr, Chris@4: "\n%s: %s: trailing garbage after EOF ignored\n", Chris@4: progName, inName ); Chris@4: return True; Chris@4: } Chris@4: default: Chris@4: panic ( "decompress:unexpected error" ); Chris@4: } Chris@4: Chris@4: panic ( "decompress:end" ); Chris@4: return True; /*notreached*/ Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: Bool testStream ( FILE *zStream ) Chris@4: { Chris@4: BZFILE* bzf = NULL; Chris@4: Int32 bzerr, bzerr_dummy, ret, nread, streamNo, i; Chris@4: UChar obuf[5000]; Chris@4: UChar unused[BZ_MAX_UNUSED]; Chris@4: Int32 nUnused; Chris@4: void* unusedTmpV; Chris@4: UChar* unusedTmp; Chris@4: Chris@4: nUnused = 0; Chris@4: streamNo = 0; Chris@4: Chris@4: SET_BINARY_MODE(zStream); Chris@4: if (ferror(zStream)) goto errhandler_io; Chris@4: Chris@4: while (True) { Chris@4: Chris@4: bzf = BZ2_bzReadOpen ( Chris@4: &bzerr, zStream, verbosity, Chris@4: (int)smallMode, unused, nUnused Chris@4: ); Chris@4: if (bzf == NULL || bzerr != BZ_OK) goto errhandler; Chris@4: streamNo++; Chris@4: Chris@4: while (bzerr == BZ_OK) { Chris@4: nread = BZ2_bzRead ( &bzerr, bzf, obuf, 5000 ); Chris@4: if (bzerr == BZ_DATA_ERROR_MAGIC) goto errhandler; Chris@4: } Chris@4: if (bzerr != BZ_STREAM_END) goto errhandler; Chris@4: Chris@4: BZ2_bzReadGetUnused ( &bzerr, bzf, &unusedTmpV, &nUnused ); Chris@4: if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" ); Chris@4: Chris@4: unusedTmp = (UChar*)unusedTmpV; Chris@4: for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i]; Chris@4: Chris@4: BZ2_bzReadClose ( &bzerr, bzf ); Chris@4: if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" ); Chris@4: if (nUnused == 0 && myfeof(zStream)) break; Chris@4: Chris@4: } Chris@4: Chris@4: if (ferror(zStream)) goto errhandler_io; Chris@4: ret = fclose ( zStream ); Chris@4: if (ret == EOF) goto errhandler_io; Chris@4: Chris@4: if (verbosity >= 2) fprintf ( stderr, "\n " ); Chris@4: return True; Chris@4: Chris@4: errhandler: Chris@4: BZ2_bzReadClose ( &bzerr_dummy, bzf ); Chris@4: if (verbosity == 0) Chris@4: fprintf ( stderr, "%s: %s: ", progName, inName ); Chris@4: switch (bzerr) { Chris@4: case BZ_CONFIG_ERROR: Chris@4: configError(); break; Chris@4: case BZ_IO_ERROR: Chris@4: errhandler_io: Chris@4: ioError(); break; Chris@4: case BZ_DATA_ERROR: Chris@4: fprintf ( stderr, Chris@4: "data integrity (CRC) error in data\n" ); Chris@4: return False; Chris@4: case BZ_MEM_ERROR: Chris@4: outOfMemory(); Chris@4: case BZ_UNEXPECTED_EOF: Chris@4: fprintf ( stderr, Chris@4: "file ends unexpectedly\n" ); Chris@4: return False; Chris@4: case BZ_DATA_ERROR_MAGIC: Chris@4: if (zStream != stdin) fclose(zStream); Chris@4: if (streamNo == 1) { Chris@4: fprintf ( stderr, Chris@4: "bad magic number (file not created by bzip2)\n" ); Chris@4: return False; Chris@4: } else { Chris@4: if (noisy) Chris@4: fprintf ( stderr, Chris@4: "trailing garbage after EOF ignored\n" ); Chris@4: return True; Chris@4: } Chris@4: default: Chris@4: panic ( "test:unexpected error" ); Chris@4: } Chris@4: Chris@4: panic ( "test:end" ); Chris@4: return True; /*notreached*/ Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------------*/ Chris@4: /*--- Error [non-] handling grunge ---*/ Chris@4: /*---------------------------------------------------*/ Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void setExit ( Int32 v ) Chris@4: { Chris@4: if (v > exitValue) exitValue = v; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void cadvise ( void ) Chris@4: { Chris@4: if (noisy) Chris@4: fprintf ( Chris@4: stderr, Chris@4: "\nIt is possible that the compressed file(s) have become corrupted.\n" Chris@4: "You can use the -tvv option to test integrity of such files.\n\n" Chris@4: "You can use the `bzip2recover' program to attempt to recover\n" Chris@4: "data from undamaged sections of corrupted files.\n\n" Chris@4: ); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void showFileNames ( void ) Chris@4: { Chris@4: if (noisy) Chris@4: fprintf ( Chris@4: stderr, Chris@4: "\tInput file = %s, output file = %s\n", Chris@4: inName, outName Chris@4: ); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void cleanUpAndFail ( Int32 ec ) Chris@4: { Chris@4: IntNative retVal; Chris@4: struct MY_STAT statBuf; Chris@4: Chris@4: if ( srcMode == SM_F2F Chris@4: && opMode != OM_TEST Chris@4: && deleteOutputOnInterrupt ) { Chris@4: Chris@4: /* Check whether input file still exists. Delete output file Chris@4: only if input exists to avoid loss of data. Joerg Prante, 5 Chris@4: January 2002. (JRS 06-Jan-2002: other changes in 1.0.2 mean Chris@4: this is less likely to happen. But to be ultra-paranoid, we Chris@4: do the check anyway.) */ Chris@4: retVal = MY_STAT ( inName, &statBuf ); Chris@4: if (retVal == 0) { Chris@4: if (noisy) Chris@4: fprintf ( stderr, Chris@4: "%s: Deleting output file %s, if it exists.\n", Chris@4: progName, outName ); Chris@4: if (outputHandleJustInCase != NULL) Chris@4: fclose ( outputHandleJustInCase ); Chris@4: retVal = remove ( outName ); Chris@4: if (retVal != 0) Chris@4: fprintf ( stderr, Chris@4: "%s: WARNING: deletion of output file " Chris@4: "(apparently) failed.\n", Chris@4: progName ); Chris@4: } else { Chris@4: fprintf ( stderr, Chris@4: "%s: WARNING: deletion of output file suppressed\n", Chris@4: progName ); Chris@4: fprintf ( stderr, Chris@4: "%s: since input file no longer exists. Output file\n", Chris@4: progName ); Chris@4: fprintf ( stderr, Chris@4: "%s: `%s' may be incomplete.\n", Chris@4: progName, outName ); Chris@4: fprintf ( stderr, Chris@4: "%s: I suggest doing an integrity test (bzip2 -tv)" Chris@4: " of it.\n", Chris@4: progName ); Chris@4: } Chris@4: } Chris@4: Chris@4: if (noisy && numFileNames > 0 && numFilesProcessed < numFileNames) { Chris@4: fprintf ( stderr, Chris@4: "%s: WARNING: some files have not been processed:\n" Chris@4: "%s: %d specified on command line, %d not processed yet.\n\n", Chris@4: progName, progName, Chris@4: numFileNames, numFileNames - numFilesProcessed ); Chris@4: } Chris@4: setExit(ec); Chris@4: exit(exitValue); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void panic ( const Char* s ) Chris@4: { Chris@4: fprintf ( stderr, Chris@4: "\n%s: PANIC -- internal consistency error:\n" Chris@4: "\t%s\n" Chris@4: "\tThis is a BUG. Please report it to me at:\n" Chris@4: "\tjseward@bzip.org\n", Chris@4: progName, s ); Chris@4: showFileNames(); Chris@4: cleanUpAndFail( 3 ); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void crcError ( void ) Chris@4: { Chris@4: fprintf ( stderr, Chris@4: "\n%s: Data integrity error when decompressing.\n", Chris@4: progName ); Chris@4: showFileNames(); Chris@4: cadvise(); Chris@4: cleanUpAndFail( 2 ); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void compressedStreamEOF ( void ) Chris@4: { Chris@4: if (noisy) { Chris@4: fprintf ( stderr, Chris@4: "\n%s: Compressed file ends unexpectedly;\n\t" Chris@4: "perhaps it is corrupted? *Possible* reason follows.\n", Chris@4: progName ); Chris@4: perror ( progName ); Chris@4: showFileNames(); Chris@4: cadvise(); Chris@4: } Chris@4: cleanUpAndFail( 2 ); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void ioError ( void ) Chris@4: { Chris@4: fprintf ( stderr, Chris@4: "\n%s: I/O or other error, bailing out. " Chris@4: "Possible reason follows.\n", Chris@4: progName ); Chris@4: perror ( progName ); Chris@4: showFileNames(); Chris@4: cleanUpAndFail( 1 ); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void mySignalCatcher ( IntNative n ) Chris@4: { Chris@4: fprintf ( stderr, Chris@4: "\n%s: Control-C or similar caught, quitting.\n", Chris@4: progName ); Chris@4: cleanUpAndFail(1); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void mySIGSEGVorSIGBUScatcher ( IntNative n ) Chris@4: { Chris@4: if (opMode == OM_Z) Chris@4: fprintf ( Chris@4: stderr, Chris@4: "\n%s: Caught a SIGSEGV or SIGBUS whilst compressing.\n" Chris@4: "\n" Chris@4: " Possible causes are (most likely first):\n" Chris@4: " (1) This computer has unreliable memory or cache hardware\n" Chris@4: " (a surprisingly common problem; try a different machine.)\n" Chris@4: " (2) A bug in the compiler used to create this executable\n" Chris@4: " (unlikely, if you didn't compile bzip2 yourself.)\n" Chris@4: " (3) A real bug in bzip2 -- I hope this should never be the case.\n" Chris@4: " The user's manual, Section 4.3, has more info on (1) and (2).\n" Chris@4: " \n" Chris@4: " If you suspect this is a bug in bzip2, or are unsure about (1)\n" Chris@4: " or (2), feel free to report it to me at: jseward@bzip.org.\n" Chris@4: " Section 4.3 of the user's manual describes the info a useful\n" Chris@4: " bug report should have. If the manual is available on your\n" Chris@4: " system, please try and read it before mailing me. If you don't\n" Chris@4: " have the manual or can't be bothered to read it, mail me anyway.\n" Chris@4: "\n", Chris@4: progName ); Chris@4: else Chris@4: fprintf ( Chris@4: stderr, Chris@4: "\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing.\n" Chris@4: "\n" Chris@4: " Possible causes are (most likely first):\n" Chris@4: " (1) The compressed data is corrupted, and bzip2's usual checks\n" Chris@4: " failed to detect this. Try bzip2 -tvv my_file.bz2.\n" Chris@4: " (2) This computer has unreliable memory or cache hardware\n" Chris@4: " (a surprisingly common problem; try a different machine.)\n" Chris@4: " (3) A bug in the compiler used to create this executable\n" Chris@4: " (unlikely, if you didn't compile bzip2 yourself.)\n" Chris@4: " (4) A real bug in bzip2 -- I hope this should never be the case.\n" Chris@4: " The user's manual, Section 4.3, has more info on (2) and (3).\n" Chris@4: " \n" Chris@4: " If you suspect this is a bug in bzip2, or are unsure about (2)\n" Chris@4: " or (3), feel free to report it to me at: jseward@bzip.org.\n" Chris@4: " Section 4.3 of the user's manual describes the info a useful\n" Chris@4: " bug report should have. If the manual is available on your\n" Chris@4: " system, please try and read it before mailing me. If you don't\n" Chris@4: " have the manual or can't be bothered to read it, mail me anyway.\n" Chris@4: "\n", Chris@4: progName ); Chris@4: Chris@4: showFileNames(); Chris@4: if (opMode == OM_Z) Chris@4: cleanUpAndFail( 3 ); else Chris@4: { cadvise(); cleanUpAndFail( 2 ); } Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void outOfMemory ( void ) Chris@4: { Chris@4: fprintf ( stderr, Chris@4: "\n%s: couldn't allocate enough memory\n", Chris@4: progName ); Chris@4: showFileNames(); Chris@4: cleanUpAndFail(1); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void configError ( void ) Chris@4: { Chris@4: fprintf ( stderr, Chris@4: "bzip2: I'm not configured correctly for this platform!\n" Chris@4: "\tI require Int32, Int16 and Char to have sizes\n" Chris@4: "\tof 4, 2 and 1 bytes to run properly, and they don't.\n" Chris@4: "\tProbably you can fix this by defining them correctly,\n" Chris@4: "\tand recompiling. Bye!\n" ); Chris@4: setExit(3); Chris@4: exit(exitValue); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------------*/ Chris@4: /*--- The main driver machinery ---*/ Chris@4: /*---------------------------------------------------*/ Chris@4: Chris@4: /* All rather crufty. The main problem is that input files Chris@4: are stat()d multiple times before use. This should be Chris@4: cleaned up. Chris@4: */ Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void pad ( Char *s ) Chris@4: { Chris@4: Int32 i; Chris@4: if ( (Int32)strlen(s) >= longestFileName ) return; Chris@4: for (i = 1; i <= longestFileName - (Int32)strlen(s); i++) Chris@4: fprintf ( stderr, " " ); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void copyFileName ( Char* to, Char* from ) Chris@4: { Chris@4: if ( strlen(from) > FILE_NAME_LEN-10 ) { Chris@4: fprintf ( Chris@4: stderr, Chris@4: "bzip2: file name\n`%s'\n" Chris@4: "is suspiciously (more than %d chars) long.\n" Chris@4: "Try using a reasonable file name instead. Sorry! :-)\n", Chris@4: from, FILE_NAME_LEN-10 Chris@4: ); Chris@4: setExit(1); Chris@4: exit(exitValue); Chris@4: } Chris@4: Chris@4: strncpy(to,from,FILE_NAME_LEN-10); Chris@4: to[FILE_NAME_LEN-10]='\0'; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: Bool fileExists ( Char* name ) Chris@4: { Chris@4: FILE *tmp = fopen ( name, "rb" ); Chris@4: Bool exists = (tmp != NULL); Chris@4: if (tmp != NULL) fclose ( tmp ); Chris@4: return exists; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: /* Open an output file safely with O_EXCL and good permissions. Chris@4: This avoids a race condition in versions < 1.0.2, in which Chris@4: the file was first opened and then had its interim permissions Chris@4: set safely. We instead use open() to create the file with Chris@4: the interim permissions required. (--- --- rw-). Chris@4: Chris@4: For non-Unix platforms, if we are not worrying about Chris@4: security issues, simple this simply behaves like fopen. Chris@4: */ Chris@4: static Chris@4: FILE* fopen_output_safely ( Char* name, const char* mode ) Chris@4: { Chris@4: # if BZ_UNIX Chris@4: FILE* fp; Chris@4: IntNative fh; Chris@4: fh = open(name, O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR); Chris@4: if (fh == -1) return NULL; Chris@4: fp = fdopen(fh, mode); Chris@4: if (fp == NULL) close(fh); Chris@4: return fp; Chris@4: # else Chris@4: return fopen(name, mode); Chris@4: # endif Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: /*-- Chris@4: if in doubt, return True Chris@4: --*/ Chris@4: static Chris@4: Bool notAStandardFile ( Char* name ) Chris@4: { Chris@4: IntNative i; Chris@4: struct MY_STAT statBuf; Chris@4: Chris@4: i = MY_LSTAT ( name, &statBuf ); Chris@4: if (i != 0) return True; Chris@4: if (MY_S_ISREG(statBuf.st_mode)) return False; Chris@4: return True; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: /*-- Chris@4: rac 11/21/98 see if file has hard links to it Chris@4: --*/ Chris@4: static Chris@4: Int32 countHardLinks ( Char* name ) Chris@4: { Chris@4: IntNative i; Chris@4: struct MY_STAT statBuf; Chris@4: Chris@4: i = MY_LSTAT ( name, &statBuf ); Chris@4: if (i != 0) return 0; Chris@4: return (statBuf.st_nlink - 1); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: /* Copy modification date, access date, permissions and owner from the Chris@4: source to destination file. We have to copy this meta-info off Chris@4: into fileMetaInfo before starting to compress / decompress it, Chris@4: because doing it afterwards means we get the wrong access time. Chris@4: Chris@4: To complicate matters, in compress() and decompress() below, the Chris@4: sequence of tests preceding the call to saveInputFileMetaInfo() Chris@4: involves calling fileExists(), which in turn establishes its result Chris@4: by attempting to fopen() the file, and if successful, immediately Chris@4: fclose()ing it again. So we have to assume that the fopen() call Chris@4: does not cause the access time field to be updated. Chris@4: Chris@4: Reading of the man page for stat() (man 2 stat) on RedHat 7.2 seems Chris@4: to imply that merely doing open() will not affect the access time. Chris@4: Therefore we merely need to hope that the C library only does Chris@4: open() as a result of fopen(), and not any kind of read()-ahead Chris@4: cleverness. Chris@4: Chris@4: It sounds pretty fragile to me. Whether this carries across Chris@4: robustly to arbitrary Unix-like platforms (or even works robustly Chris@4: on this one, RedHat 7.2) is unknown to me. Nevertheless ... Chris@4: */ Chris@4: #if BZ_UNIX Chris@4: static Chris@4: struct MY_STAT fileMetaInfo; Chris@4: #endif Chris@4: Chris@4: static Chris@4: void saveInputFileMetaInfo ( Char *srcName ) Chris@4: { Chris@4: # if BZ_UNIX Chris@4: IntNative retVal; Chris@4: /* Note use of stat here, not lstat. */ Chris@4: retVal = MY_STAT( srcName, &fileMetaInfo ); Chris@4: ERROR_IF_NOT_ZERO ( retVal ); Chris@4: # endif Chris@4: } Chris@4: Chris@4: Chris@4: static Chris@4: void applySavedTimeInfoToOutputFile ( Char *dstName ) Chris@4: { Chris@4: # if BZ_UNIX Chris@4: IntNative retVal; Chris@4: struct utimbuf uTimBuf; Chris@4: Chris@4: uTimBuf.actime = fileMetaInfo.st_atime; Chris@4: uTimBuf.modtime = fileMetaInfo.st_mtime; Chris@4: Chris@4: retVal = utime ( dstName, &uTimBuf ); Chris@4: ERROR_IF_NOT_ZERO ( retVal ); Chris@4: # endif Chris@4: } Chris@4: Chris@4: static Chris@4: void applySavedFileAttrToOutputFile ( IntNative fd ) Chris@4: { Chris@4: # if BZ_UNIX Chris@4: IntNative retVal; Chris@4: Chris@4: retVal = fchmod ( fd, fileMetaInfo.st_mode ); Chris@4: ERROR_IF_NOT_ZERO ( retVal ); Chris@4: Chris@4: (void) fchown ( fd, fileMetaInfo.st_uid, fileMetaInfo.st_gid ); Chris@4: /* chown() will in many cases return with EPERM, which can Chris@4: be safely ignored. Chris@4: */ Chris@4: # endif Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: Bool containsDubiousChars ( Char* name ) Chris@4: { Chris@4: # if BZ_UNIX Chris@4: /* On unix, files can contain any characters and the file expansion Chris@4: * is performed by the shell. Chris@4: */ Chris@4: return False; Chris@4: # else /* ! BZ_UNIX */ Chris@4: /* On non-unix (Win* platforms), wildcard characters are not allowed in Chris@4: * filenames. Chris@4: */ Chris@4: for (; *name != '\0'; name++) Chris@4: if (*name == '?' || *name == '*') return True; Chris@4: return False; Chris@4: # endif /* BZ_UNIX */ Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: #define BZ_N_SUFFIX_PAIRS 4 Chris@4: Chris@4: const Char* zSuffix[BZ_N_SUFFIX_PAIRS] Chris@4: = { ".bz2", ".bz", ".tbz2", ".tbz" }; Chris@4: const Char* unzSuffix[BZ_N_SUFFIX_PAIRS] Chris@4: = { "", "", ".tar", ".tar" }; Chris@4: Chris@4: static Chris@4: Bool hasSuffix ( Char* s, const Char* suffix ) Chris@4: { Chris@4: Int32 ns = strlen(s); Chris@4: Int32 nx = strlen(suffix); Chris@4: if (ns < nx) return False; Chris@4: if (strcmp(s + ns - nx, suffix) == 0) return True; Chris@4: return False; Chris@4: } Chris@4: Chris@4: static Chris@4: Bool mapSuffix ( Char* name, Chris@4: const Char* oldSuffix, Chris@4: const Char* newSuffix ) Chris@4: { Chris@4: if (!hasSuffix(name,oldSuffix)) return False; Chris@4: name[strlen(name)-strlen(oldSuffix)] = 0; Chris@4: strcat ( name, newSuffix ); Chris@4: return True; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void compress ( Char *name ) Chris@4: { Chris@4: FILE *inStr; Chris@4: FILE *outStr; Chris@4: Int32 n, i; Chris@4: struct MY_STAT statBuf; Chris@4: Chris@4: deleteOutputOnInterrupt = False; Chris@4: Chris@4: if (name == NULL && srcMode != SM_I2O) Chris@4: panic ( "compress: bad modes\n" ); Chris@4: Chris@4: switch (srcMode) { Chris@4: case SM_I2O: Chris@4: copyFileName ( inName, (Char*)"(stdin)" ); Chris@4: copyFileName ( outName, (Char*)"(stdout)" ); Chris@4: break; Chris@4: case SM_F2F: Chris@4: copyFileName ( inName, name ); Chris@4: copyFileName ( outName, name ); Chris@4: strcat ( outName, ".bz2" ); Chris@4: break; Chris@4: case SM_F2O: Chris@4: copyFileName ( inName, name ); Chris@4: copyFileName ( outName, (Char*)"(stdout)" ); Chris@4: break; Chris@4: } Chris@4: Chris@4: if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) { Chris@4: if (noisy) Chris@4: fprintf ( stderr, "%s: There are no files matching `%s'.\n", Chris@4: progName, inName ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: if ( srcMode != SM_I2O && !fileExists ( inName ) ) { Chris@4: fprintf ( stderr, "%s: Can't open input file %s: %s.\n", Chris@4: progName, inName, strerror(errno) ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++) { Chris@4: if (hasSuffix(inName, zSuffix[i])) { Chris@4: if (noisy) Chris@4: fprintf ( stderr, Chris@4: "%s: Input file %s already has %s suffix.\n", Chris@4: progName, inName, zSuffix[i] ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: } Chris@4: if ( srcMode == SM_F2F || srcMode == SM_F2O ) { Chris@4: MY_STAT(inName, &statBuf); Chris@4: if ( MY_S_ISDIR(statBuf.st_mode) ) { Chris@4: fprintf( stderr, Chris@4: "%s: Input file %s is a directory.\n", Chris@4: progName,inName); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: } Chris@4: if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) { Chris@4: if (noisy) Chris@4: fprintf ( stderr, "%s: Input file %s is not a normal file.\n", Chris@4: progName, inName ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: if ( srcMode == SM_F2F && fileExists ( outName ) ) { Chris@4: if (forceOverwrite) { Chris@4: remove(outName); Chris@4: } else { Chris@4: fprintf ( stderr, "%s: Output file %s already exists.\n", Chris@4: progName, outName ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: } Chris@4: if ( srcMode == SM_F2F && !forceOverwrite && Chris@4: (n=countHardLinks ( inName )) > 0) { Chris@4: fprintf ( stderr, "%s: Input file %s has %d other link%s.\n", Chris@4: progName, inName, n, n > 1 ? "s" : "" ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: Chris@4: if ( srcMode == SM_F2F ) { Chris@4: /* Save the file's meta-info before we open it. Doing it later Chris@4: means we mess up the access times. */ Chris@4: saveInputFileMetaInfo ( inName ); Chris@4: } Chris@4: Chris@4: switch ( srcMode ) { Chris@4: Chris@4: case SM_I2O: Chris@4: inStr = stdin; Chris@4: outStr = stdout; Chris@4: if ( isatty ( fileno ( stdout ) ) ) { Chris@4: fprintf ( stderr, Chris@4: "%s: I won't write compressed data to a terminal.\n", Chris@4: progName ); Chris@4: fprintf ( stderr, "%s: For help, type: `%s --help'.\n", Chris@4: progName, progName ); Chris@4: setExit(1); Chris@4: return; Chris@4: }; Chris@4: break; Chris@4: Chris@4: case SM_F2O: Chris@4: inStr = fopen ( inName, "rb" ); Chris@4: outStr = stdout; Chris@4: if ( isatty ( fileno ( stdout ) ) ) { Chris@4: fprintf ( stderr, Chris@4: "%s: I won't write compressed data to a terminal.\n", Chris@4: progName ); Chris@4: fprintf ( stderr, "%s: For help, type: `%s --help'.\n", Chris@4: progName, progName ); Chris@4: if ( inStr != NULL ) fclose ( inStr ); Chris@4: setExit(1); Chris@4: return; Chris@4: }; Chris@4: if ( inStr == NULL ) { Chris@4: fprintf ( stderr, "%s: Can't open input file %s: %s.\n", Chris@4: progName, inName, strerror(errno) ); Chris@4: setExit(1); Chris@4: return; Chris@4: }; Chris@4: break; Chris@4: Chris@4: case SM_F2F: Chris@4: inStr = fopen ( inName, "rb" ); Chris@4: outStr = fopen_output_safely ( outName, "wb" ); Chris@4: if ( outStr == NULL) { Chris@4: fprintf ( stderr, "%s: Can't create output file %s: %s.\n", Chris@4: progName, outName, strerror(errno) ); Chris@4: if ( inStr != NULL ) fclose ( inStr ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: if ( inStr == NULL ) { Chris@4: fprintf ( stderr, "%s: Can't open input file %s: %s.\n", Chris@4: progName, inName, strerror(errno) ); Chris@4: if ( outStr != NULL ) fclose ( outStr ); Chris@4: setExit(1); Chris@4: return; Chris@4: }; Chris@4: break; Chris@4: Chris@4: default: Chris@4: panic ( "compress: bad srcMode" ); Chris@4: break; Chris@4: } Chris@4: Chris@4: if (verbosity >= 1) { Chris@4: fprintf ( stderr, " %s: ", inName ); Chris@4: pad ( inName ); Chris@4: fflush ( stderr ); Chris@4: } Chris@4: Chris@4: /*--- Now the input and output handles are sane. Do the Biz. ---*/ Chris@4: outputHandleJustInCase = outStr; Chris@4: deleteOutputOnInterrupt = True; Chris@4: compressStream ( inStr, outStr ); Chris@4: outputHandleJustInCase = NULL; Chris@4: Chris@4: /*--- If there was an I/O error, we won't get here. ---*/ Chris@4: if ( srcMode == SM_F2F ) { Chris@4: applySavedTimeInfoToOutputFile ( outName ); Chris@4: deleteOutputOnInterrupt = False; Chris@4: if ( !keepInputFiles ) { Chris@4: IntNative retVal = remove ( inName ); Chris@4: ERROR_IF_NOT_ZERO ( retVal ); Chris@4: } Chris@4: } Chris@4: Chris@4: deleteOutputOnInterrupt = False; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void uncompress ( Char *name ) Chris@4: { Chris@4: FILE *inStr; Chris@4: FILE *outStr; Chris@4: Int32 n, i; Chris@4: Bool magicNumberOK; Chris@4: Bool cantGuess; Chris@4: struct MY_STAT statBuf; Chris@4: Chris@4: deleteOutputOnInterrupt = False; Chris@4: Chris@4: if (name == NULL && srcMode != SM_I2O) Chris@4: panic ( "uncompress: bad modes\n" ); Chris@4: Chris@4: cantGuess = False; Chris@4: switch (srcMode) { Chris@4: case SM_I2O: Chris@4: copyFileName ( inName, (Char*)"(stdin)" ); Chris@4: copyFileName ( outName, (Char*)"(stdout)" ); Chris@4: break; Chris@4: case SM_F2F: Chris@4: copyFileName ( inName, name ); Chris@4: copyFileName ( outName, name ); Chris@4: for (i = 0; i < BZ_N_SUFFIX_PAIRS; i++) Chris@4: if (mapSuffix(outName,zSuffix[i],unzSuffix[i])) Chris@4: goto zzz; Chris@4: cantGuess = True; Chris@4: strcat ( outName, ".out" ); Chris@4: break; Chris@4: case SM_F2O: Chris@4: copyFileName ( inName, name ); Chris@4: copyFileName ( outName, (Char*)"(stdout)" ); Chris@4: break; Chris@4: } Chris@4: Chris@4: zzz: Chris@4: if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) { Chris@4: if (noisy) Chris@4: fprintf ( stderr, "%s: There are no files matching `%s'.\n", Chris@4: progName, inName ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: if ( srcMode != SM_I2O && !fileExists ( inName ) ) { Chris@4: fprintf ( stderr, "%s: Can't open input file %s: %s.\n", Chris@4: progName, inName, strerror(errno) ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: if ( srcMode == SM_F2F || srcMode == SM_F2O ) { Chris@4: MY_STAT(inName, &statBuf); Chris@4: if ( MY_S_ISDIR(statBuf.st_mode) ) { Chris@4: fprintf( stderr, Chris@4: "%s: Input file %s is a directory.\n", Chris@4: progName,inName); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: } Chris@4: if ( srcMode == SM_F2F && !forceOverwrite && notAStandardFile ( inName )) { Chris@4: if (noisy) Chris@4: fprintf ( stderr, "%s: Input file %s is not a normal file.\n", Chris@4: progName, inName ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: if ( /* srcMode == SM_F2F implied && */ cantGuess ) { Chris@4: if (noisy) Chris@4: fprintf ( stderr, Chris@4: "%s: Can't guess original name for %s -- using %s\n", Chris@4: progName, inName, outName ); Chris@4: /* just a warning, no return */ Chris@4: } Chris@4: if ( srcMode == SM_F2F && fileExists ( outName ) ) { Chris@4: if (forceOverwrite) { Chris@4: remove(outName); Chris@4: } else { Chris@4: fprintf ( stderr, "%s: Output file %s already exists.\n", Chris@4: progName, outName ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: } Chris@4: if ( srcMode == SM_F2F && !forceOverwrite && Chris@4: (n=countHardLinks ( inName ) ) > 0) { Chris@4: fprintf ( stderr, "%s: Input file %s has %d other link%s.\n", Chris@4: progName, inName, n, n > 1 ? "s" : "" ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: Chris@4: if ( srcMode == SM_F2F ) { Chris@4: /* Save the file's meta-info before we open it. Doing it later Chris@4: means we mess up the access times. */ Chris@4: saveInputFileMetaInfo ( inName ); Chris@4: } Chris@4: Chris@4: switch ( srcMode ) { Chris@4: Chris@4: case SM_I2O: Chris@4: inStr = stdin; Chris@4: outStr = stdout; Chris@4: if ( isatty ( fileno ( stdin ) ) ) { Chris@4: fprintf ( stderr, Chris@4: "%s: I won't read compressed data from a terminal.\n", Chris@4: progName ); Chris@4: fprintf ( stderr, "%s: For help, type: `%s --help'.\n", Chris@4: progName, progName ); Chris@4: setExit(1); Chris@4: return; Chris@4: }; Chris@4: break; Chris@4: Chris@4: case SM_F2O: Chris@4: inStr = fopen ( inName, "rb" ); Chris@4: outStr = stdout; Chris@4: if ( inStr == NULL ) { Chris@4: fprintf ( stderr, "%s: Can't open input file %s:%s.\n", Chris@4: progName, inName, strerror(errno) ); Chris@4: if ( inStr != NULL ) fclose ( inStr ); Chris@4: setExit(1); Chris@4: return; Chris@4: }; Chris@4: break; Chris@4: Chris@4: case SM_F2F: Chris@4: inStr = fopen ( inName, "rb" ); Chris@4: outStr = fopen_output_safely ( outName, "wb" ); Chris@4: if ( outStr == NULL) { Chris@4: fprintf ( stderr, "%s: Can't create output file %s: %s.\n", Chris@4: progName, outName, strerror(errno) ); Chris@4: if ( inStr != NULL ) fclose ( inStr ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: if ( inStr == NULL ) { Chris@4: fprintf ( stderr, "%s: Can't open input file %s: %s.\n", Chris@4: progName, inName, strerror(errno) ); Chris@4: if ( outStr != NULL ) fclose ( outStr ); Chris@4: setExit(1); Chris@4: return; Chris@4: }; Chris@4: break; Chris@4: Chris@4: default: Chris@4: panic ( "uncompress: bad srcMode" ); Chris@4: break; Chris@4: } Chris@4: Chris@4: if (verbosity >= 1) { Chris@4: fprintf ( stderr, " %s: ", inName ); Chris@4: pad ( inName ); Chris@4: fflush ( stderr ); Chris@4: } Chris@4: Chris@4: /*--- Now the input and output handles are sane. Do the Biz. ---*/ Chris@4: outputHandleJustInCase = outStr; Chris@4: deleteOutputOnInterrupt = True; Chris@4: magicNumberOK = uncompressStream ( inStr, outStr ); Chris@4: outputHandleJustInCase = NULL; Chris@4: Chris@4: /*--- If there was an I/O error, we won't get here. ---*/ Chris@4: if ( magicNumberOK ) { Chris@4: if ( srcMode == SM_F2F ) { Chris@4: applySavedTimeInfoToOutputFile ( outName ); Chris@4: deleteOutputOnInterrupt = False; Chris@4: if ( !keepInputFiles ) { Chris@4: IntNative retVal = remove ( inName ); Chris@4: ERROR_IF_NOT_ZERO ( retVal ); Chris@4: } Chris@4: } Chris@4: } else { Chris@4: unzFailsExist = True; Chris@4: deleteOutputOnInterrupt = False; Chris@4: if ( srcMode == SM_F2F ) { Chris@4: IntNative retVal = remove ( outName ); Chris@4: ERROR_IF_NOT_ZERO ( retVal ); Chris@4: } Chris@4: } Chris@4: deleteOutputOnInterrupt = False; Chris@4: Chris@4: if ( magicNumberOK ) { Chris@4: if (verbosity >= 1) Chris@4: fprintf ( stderr, "done\n" ); Chris@4: } else { Chris@4: setExit(2); Chris@4: if (verbosity >= 1) Chris@4: fprintf ( stderr, "not a bzip2 file.\n" ); else Chris@4: fprintf ( stderr, Chris@4: "%s: %s is not a bzip2 file.\n", Chris@4: progName, inName ); Chris@4: } Chris@4: Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void testf ( Char *name ) Chris@4: { Chris@4: FILE *inStr; Chris@4: Bool allOK; Chris@4: struct MY_STAT statBuf; Chris@4: Chris@4: deleteOutputOnInterrupt = False; Chris@4: Chris@4: if (name == NULL && srcMode != SM_I2O) Chris@4: panic ( "testf: bad modes\n" ); Chris@4: Chris@4: copyFileName ( outName, (Char*)"(none)" ); Chris@4: switch (srcMode) { Chris@4: case SM_I2O: copyFileName ( inName, (Char*)"(stdin)" ); break; Chris@4: case SM_F2F: copyFileName ( inName, name ); break; Chris@4: case SM_F2O: copyFileName ( inName, name ); break; Chris@4: } Chris@4: Chris@4: if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) { Chris@4: if (noisy) Chris@4: fprintf ( stderr, "%s: There are no files matching `%s'.\n", Chris@4: progName, inName ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: if ( srcMode != SM_I2O && !fileExists ( inName ) ) { Chris@4: fprintf ( stderr, "%s: Can't open input %s: %s.\n", Chris@4: progName, inName, strerror(errno) ); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: if ( srcMode != SM_I2O ) { Chris@4: MY_STAT(inName, &statBuf); Chris@4: if ( MY_S_ISDIR(statBuf.st_mode) ) { Chris@4: fprintf( stderr, Chris@4: "%s: Input file %s is a directory.\n", Chris@4: progName,inName); Chris@4: setExit(1); Chris@4: return; Chris@4: } Chris@4: } Chris@4: Chris@4: switch ( srcMode ) { Chris@4: Chris@4: case SM_I2O: Chris@4: if ( isatty ( fileno ( stdin ) ) ) { Chris@4: fprintf ( stderr, Chris@4: "%s: I won't read compressed data from a terminal.\n", Chris@4: progName ); Chris@4: fprintf ( stderr, "%s: For help, type: `%s --help'.\n", Chris@4: progName, progName ); Chris@4: setExit(1); Chris@4: return; Chris@4: }; Chris@4: inStr = stdin; Chris@4: break; Chris@4: Chris@4: case SM_F2O: case SM_F2F: Chris@4: inStr = fopen ( inName, "rb" ); Chris@4: if ( inStr == NULL ) { Chris@4: fprintf ( stderr, "%s: Can't open input file %s:%s.\n", Chris@4: progName, inName, strerror(errno) ); Chris@4: setExit(1); Chris@4: return; Chris@4: }; Chris@4: break; Chris@4: Chris@4: default: Chris@4: panic ( "testf: bad srcMode" ); Chris@4: break; Chris@4: } Chris@4: Chris@4: if (verbosity >= 1) { Chris@4: fprintf ( stderr, " %s: ", inName ); Chris@4: pad ( inName ); Chris@4: fflush ( stderr ); Chris@4: } Chris@4: Chris@4: /*--- Now the input handle is sane. Do the Biz. ---*/ Chris@4: outputHandleJustInCase = NULL; Chris@4: allOK = testStream ( inStr ); Chris@4: Chris@4: if (allOK && verbosity >= 1) fprintf ( stderr, "ok\n" ); Chris@4: if (!allOK) testFailsExist = True; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void license ( void ) Chris@4: { Chris@4: fprintf ( stderr, Chris@4: Chris@4: "bzip2, a block-sorting file compressor. " Chris@4: "Version %s.\n" Chris@4: " \n" Chris@4: " Copyright (C) 1996-2010 by Julian Seward.\n" Chris@4: " \n" Chris@4: " This program is free software; you can redistribute it and/or modify\n" Chris@4: " it under the terms set out in the LICENSE file, which is included\n" Chris@4: " in the bzip2-1.0.6 source distribution.\n" Chris@4: " \n" Chris@4: " This program is distributed in the hope that it will be useful,\n" Chris@4: " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" Chris@4: " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" Chris@4: " LICENSE file for more details.\n" Chris@4: " \n", Chris@4: BZ2_bzlibVersion() Chris@4: ); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void usage ( Char *fullProgName ) Chris@4: { Chris@4: fprintf ( Chris@4: stderr, Chris@4: "bzip2, a block-sorting file compressor. " Chris@4: "Version %s.\n" Chris@4: "\n usage: %s [flags and input files in any order]\n" Chris@4: "\n" Chris@4: " -h --help print this message\n" Chris@4: " -d --decompress force decompression\n" Chris@4: " -z --compress force compression\n" Chris@4: " -k --keep keep (don't delete) input files\n" Chris@4: " -f --force overwrite existing output files\n" Chris@4: " -t --test test compressed file integrity\n" Chris@4: " -c --stdout output to standard out\n" Chris@4: " -q --quiet suppress noncritical error messages\n" Chris@4: " -v --verbose be verbose (a 2nd -v gives more)\n" Chris@4: " -L --license display software version & license\n" Chris@4: " -V --version display software version & license\n" Chris@4: " -s --small use less memory (at most 2500k)\n" Chris@4: " -1 .. -9 set block size to 100k .. 900k\n" Chris@4: " --fast alias for -1\n" Chris@4: " --best alias for -9\n" Chris@4: "\n" Chris@4: " If invoked as `bzip2', default action is to compress.\n" Chris@4: " as `bunzip2', default action is to decompress.\n" Chris@4: " as `bzcat', default action is to decompress to stdout.\n" Chris@4: "\n" Chris@4: " If no file names are given, bzip2 compresses or decompresses\n" Chris@4: " from standard input to standard output. You can combine\n" Chris@4: " short flags, so `-v -4' means the same as -v4 or -4v, &c.\n" Chris@4: # if BZ_UNIX Chris@4: "\n" Chris@4: # endif Chris@4: , Chris@4: Chris@4: BZ2_bzlibVersion(), Chris@4: fullProgName Chris@4: ); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void redundant ( Char* flag ) Chris@4: { Chris@4: fprintf ( Chris@4: stderr, Chris@4: "%s: %s is redundant in versions 0.9.5 and above\n", Chris@4: progName, flag ); Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: /*-- Chris@4: All the garbage from here to main() is purely to Chris@4: implement a linked list of command-line arguments, Chris@4: into which main() copies argv[1 .. argc-1]. Chris@4: Chris@4: The purpose of this exercise is to facilitate Chris@4: the expansion of wildcard characters * and ? in Chris@4: filenames for OSs which don't know how to do it Chris@4: themselves, like MSDOS, Windows 95 and NT. Chris@4: Chris@4: The actual Dirty Work is done by the platform- Chris@4: specific macro APPEND_FILESPEC. Chris@4: --*/ Chris@4: Chris@4: typedef Chris@4: struct zzzz { Chris@4: Char *name; Chris@4: struct zzzz *link; Chris@4: } Chris@4: Cell; Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void *myMalloc ( Int32 n ) Chris@4: { Chris@4: void* p; Chris@4: Chris@4: p = malloc ( (size_t)n ); Chris@4: if (p == NULL) outOfMemory (); Chris@4: return p; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: Cell *mkCell ( void ) Chris@4: { Chris@4: Cell *c; Chris@4: Chris@4: c = (Cell*) myMalloc ( sizeof ( Cell ) ); Chris@4: c->name = NULL; Chris@4: c->link = NULL; Chris@4: return c; Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: Cell *snocString ( Cell *root, Char *name ) Chris@4: { Chris@4: if (root == NULL) { Chris@4: Cell *tmp = mkCell(); Chris@4: tmp->name = (Char*) myMalloc ( 5 + strlen(name) ); Chris@4: strcpy ( tmp->name, name ); Chris@4: return tmp; Chris@4: } else { Chris@4: Cell *tmp = root; Chris@4: while (tmp->link != NULL) tmp = tmp->link; Chris@4: tmp->link = snocString ( tmp->link, name ); Chris@4: return root; Chris@4: } Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: static Chris@4: void addFlagsFromEnvVar ( Cell** argList, Char* varName ) Chris@4: { Chris@4: Int32 i, j, k; Chris@4: Char *envbase, *p; Chris@4: Chris@4: envbase = getenv(varName); Chris@4: if (envbase != NULL) { Chris@4: p = envbase; Chris@4: i = 0; Chris@4: while (True) { Chris@4: if (p[i] == 0) break; Chris@4: p += i; Chris@4: i = 0; Chris@4: while (isspace((Int32)(p[0]))) p++; Chris@4: while (p[i] != 0 && !isspace((Int32)(p[i]))) i++; Chris@4: if (i > 0) { Chris@4: k = i; if (k > FILE_NAME_LEN-10) k = FILE_NAME_LEN-10; Chris@4: for (j = 0; j < k; j++) tmpName[j] = p[j]; Chris@4: tmpName[k] = 0; Chris@4: APPEND_FLAG(*argList, tmpName); Chris@4: } Chris@4: } Chris@4: } Chris@4: } Chris@4: Chris@4: Chris@4: /*---------------------------------------------*/ Chris@4: #define ISFLAG(s) (strcmp(aa->name, (s))==0) Chris@4: Chris@4: IntNative main ( IntNative argc, Char *argv[] ) Chris@4: { Chris@4: Int32 i, j; Chris@4: Char *tmp; Chris@4: Cell *argList; Chris@4: Cell *aa; Chris@4: Bool decode; Chris@4: Chris@4: /*-- Be really really really paranoid :-) --*/ Chris@4: if (sizeof(Int32) != 4 || sizeof(UInt32) != 4 || Chris@4: sizeof(Int16) != 2 || sizeof(UInt16) != 2 || Chris@4: sizeof(Char) != 1 || sizeof(UChar) != 1) Chris@4: configError(); Chris@4: Chris@4: /*-- Initialise --*/ Chris@4: outputHandleJustInCase = NULL; Chris@4: smallMode = False; Chris@4: keepInputFiles = False; Chris@4: forceOverwrite = False; Chris@4: noisy = True; Chris@4: verbosity = 0; Chris@4: blockSize100k = 9; Chris@4: testFailsExist = False; Chris@4: unzFailsExist = False; Chris@4: numFileNames = 0; Chris@4: numFilesProcessed = 0; Chris@4: workFactor = 30; Chris@4: deleteOutputOnInterrupt = False; Chris@4: exitValue = 0; Chris@4: i = j = 0; /* avoid bogus warning from egcs-1.1.X */ Chris@4: Chris@4: /*-- Set up signal handlers for mem access errors --*/ Chris@4: signal (SIGSEGV, mySIGSEGVorSIGBUScatcher); Chris@4: # if BZ_UNIX Chris@4: # ifndef __DJGPP__ Chris@4: signal (SIGBUS, mySIGSEGVorSIGBUScatcher); Chris@4: # endif Chris@4: # endif Chris@4: Chris@4: copyFileName ( inName, (Char*)"(none)" ); Chris@4: copyFileName ( outName, (Char*)"(none)" ); Chris@4: Chris@4: copyFileName ( progNameReally, argv[0] ); Chris@4: progName = &progNameReally[0]; Chris@4: for (tmp = &progNameReally[0]; *tmp != '\0'; tmp++) Chris@4: if (*tmp == PATH_SEP) progName = tmp + 1; Chris@4: Chris@4: Chris@4: /*-- Copy flags from env var BZIP2, and Chris@4: expand filename wildcards in arg list. Chris@4: --*/ Chris@4: argList = NULL; Chris@4: addFlagsFromEnvVar ( &argList, (Char*)"BZIP2" ); Chris@4: addFlagsFromEnvVar ( &argList, (Char*)"BZIP" ); Chris@4: for (i = 1; i <= argc-1; i++) Chris@4: APPEND_FILESPEC(argList, argv[i]); Chris@4: Chris@4: Chris@4: /*-- Find the length of the longest filename --*/ Chris@4: longestFileName = 7; Chris@4: numFileNames = 0; Chris@4: decode = True; Chris@4: for (aa = argList; aa != NULL; aa = aa->link) { Chris@4: if (ISFLAG("--")) { decode = False; continue; } Chris@4: if (aa->name[0] == '-' && decode) continue; Chris@4: numFileNames++; Chris@4: if (longestFileName < (Int32)strlen(aa->name) ) Chris@4: longestFileName = (Int32)strlen(aa->name); Chris@4: } Chris@4: Chris@4: Chris@4: /*-- Determine source modes; flag handling may change this too. --*/ Chris@4: if (numFileNames == 0) Chris@4: srcMode = SM_I2O; else srcMode = SM_F2F; Chris@4: Chris@4: Chris@4: /*-- Determine what to do (compress/uncompress/test/cat). --*/ Chris@4: /*-- Note that subsequent flag handling may change this. --*/ Chris@4: opMode = OM_Z; Chris@4: Chris@4: if ( (strstr ( progName, "unzip" ) != 0) || Chris@4: (strstr ( progName, "UNZIP" ) != 0) ) Chris@4: opMode = OM_UNZ; Chris@4: Chris@4: if ( (strstr ( progName, "z2cat" ) != 0) || Chris@4: (strstr ( progName, "Z2CAT" ) != 0) || Chris@4: (strstr ( progName, "zcat" ) != 0) || Chris@4: (strstr ( progName, "ZCAT" ) != 0) ) { Chris@4: opMode = OM_UNZ; Chris@4: srcMode = (numFileNames == 0) ? SM_I2O : SM_F2O; Chris@4: } Chris@4: Chris@4: Chris@4: /*-- Look at the flags. --*/ Chris@4: for (aa = argList; aa != NULL; aa = aa->link) { Chris@4: if (ISFLAG("--")) break; Chris@4: if (aa->name[0] == '-' && aa->name[1] != '-') { Chris@4: for (j = 1; aa->name[j] != '\0'; j++) { Chris@4: switch (aa->name[j]) { Chris@4: case 'c': srcMode = SM_F2O; break; Chris@4: case 'd': opMode = OM_UNZ; break; Chris@4: case 'z': opMode = OM_Z; break; Chris@4: case 'f': forceOverwrite = True; break; Chris@4: case 't': opMode = OM_TEST; break; Chris@4: case 'k': keepInputFiles = True; break; Chris@4: case 's': smallMode = True; break; Chris@4: case 'q': noisy = False; break; Chris@4: case '1': blockSize100k = 1; break; Chris@4: case '2': blockSize100k = 2; break; Chris@4: case '3': blockSize100k = 3; break; Chris@4: case '4': blockSize100k = 4; break; Chris@4: case '5': blockSize100k = 5; break; Chris@4: case '6': blockSize100k = 6; break; Chris@4: case '7': blockSize100k = 7; break; Chris@4: case '8': blockSize100k = 8; break; Chris@4: case '9': blockSize100k = 9; break; Chris@4: case 'V': Chris@4: case 'L': license(); break; Chris@4: case 'v': verbosity++; break; Chris@4: case 'h': usage ( progName ); Chris@4: exit ( 0 ); Chris@4: break; Chris@4: default: fprintf ( stderr, "%s: Bad flag `%s'\n", Chris@4: progName, aa->name ); Chris@4: usage ( progName ); Chris@4: exit ( 1 ); Chris@4: break; Chris@4: } Chris@4: } Chris@4: } Chris@4: } Chris@4: Chris@4: /*-- And again ... --*/ Chris@4: for (aa = argList; aa != NULL; aa = aa->link) { Chris@4: if (ISFLAG("--")) break; Chris@4: if (ISFLAG("--stdout")) srcMode = SM_F2O; else Chris@4: if (ISFLAG("--decompress")) opMode = OM_UNZ; else Chris@4: if (ISFLAG("--compress")) opMode = OM_Z; else Chris@4: if (ISFLAG("--force")) forceOverwrite = True; else Chris@4: if (ISFLAG("--test")) opMode = OM_TEST; else Chris@4: if (ISFLAG("--keep")) keepInputFiles = True; else Chris@4: if (ISFLAG("--small")) smallMode = True; else Chris@4: if (ISFLAG("--quiet")) noisy = False; else Chris@4: if (ISFLAG("--version")) license(); else Chris@4: if (ISFLAG("--license")) license(); else Chris@4: if (ISFLAG("--exponential")) workFactor = 1; else Chris@4: if (ISFLAG("--repetitive-best")) redundant(aa->name); else Chris@4: if (ISFLAG("--repetitive-fast")) redundant(aa->name); else Chris@4: if (ISFLAG("--fast")) blockSize100k = 1; else Chris@4: if (ISFLAG("--best")) blockSize100k = 9; else Chris@4: if (ISFLAG("--verbose")) verbosity++; else Chris@4: if (ISFLAG("--help")) { usage ( progName ); exit ( 0 ); } Chris@4: else Chris@4: if (strncmp ( aa->name, "--", 2) == 0) { Chris@4: fprintf ( stderr, "%s: Bad flag `%s'\n", progName, aa->name ); Chris@4: usage ( progName ); Chris@4: exit ( 1 ); Chris@4: } Chris@4: } Chris@4: Chris@4: if (verbosity > 4) verbosity = 4; Chris@4: if (opMode == OM_Z && smallMode && blockSize100k > 2) Chris@4: blockSize100k = 2; Chris@4: Chris@4: if (opMode == OM_TEST && srcMode == SM_F2O) { Chris@4: fprintf ( stderr, "%s: -c and -t cannot be used together.\n", Chris@4: progName ); Chris@4: exit ( 1 ); Chris@4: } Chris@4: Chris@4: if (srcMode == SM_F2O && numFileNames == 0) Chris@4: srcMode = SM_I2O; Chris@4: Chris@4: if (opMode != OM_Z) blockSize100k = 0; Chris@4: Chris@4: if (srcMode == SM_F2F) { Chris@4: signal (SIGINT, mySignalCatcher); Chris@4: signal (SIGTERM, mySignalCatcher); Chris@4: # if BZ_UNIX Chris@4: signal (SIGHUP, mySignalCatcher); Chris@4: # endif Chris@4: } Chris@4: Chris@4: if (opMode == OM_Z) { Chris@4: if (srcMode == SM_I2O) { Chris@4: compress ( NULL ); Chris@4: } else { Chris@4: decode = True; Chris@4: for (aa = argList; aa != NULL; aa = aa->link) { Chris@4: if (ISFLAG("--")) { decode = False; continue; } Chris@4: if (aa->name[0] == '-' && decode) continue; Chris@4: numFilesProcessed++; Chris@4: compress ( aa->name ); Chris@4: } Chris@4: } Chris@4: } Chris@4: else Chris@4: Chris@4: if (opMode == OM_UNZ) { Chris@4: unzFailsExist = False; Chris@4: if (srcMode == SM_I2O) { Chris@4: uncompress ( NULL ); Chris@4: } else { Chris@4: decode = True; Chris@4: for (aa = argList; aa != NULL; aa = aa->link) { Chris@4: if (ISFLAG("--")) { decode = False; continue; } Chris@4: if (aa->name[0] == '-' && decode) continue; Chris@4: numFilesProcessed++; Chris@4: uncompress ( aa->name ); Chris@4: } Chris@4: } Chris@4: if (unzFailsExist) { Chris@4: setExit(2); Chris@4: exit(exitValue); Chris@4: } Chris@4: } Chris@4: Chris@4: else { Chris@4: testFailsExist = False; Chris@4: if (srcMode == SM_I2O) { Chris@4: testf ( NULL ); Chris@4: } else { Chris@4: decode = True; Chris@4: for (aa = argList; aa != NULL; aa = aa->link) { Chris@4: if (ISFLAG("--")) { decode = False; continue; } Chris@4: if (aa->name[0] == '-' && decode) continue; Chris@4: numFilesProcessed++; Chris@4: testf ( aa->name ); Chris@4: } Chris@4: } Chris@4: if (testFailsExist && noisy) { Chris@4: fprintf ( stderr, Chris@4: "\n" Chris@4: "You can use the `bzip2recover' program to attempt to recover\n" Chris@4: "data from undamaged sections of corrupted files.\n\n" Chris@4: ); Chris@4: setExit(2); Chris@4: exit(exitValue); Chris@4: } Chris@4: } Chris@4: Chris@4: /* Free the argument list memory to mollify leak detectors Chris@4: (eg) Purify, Checker. Serves no other useful purpose. Chris@4: */ Chris@4: aa = argList; Chris@4: while (aa != NULL) { Chris@4: Cell* aa2 = aa->link; Chris@4: if (aa->name != NULL) free(aa->name); Chris@4: free(aa); Chris@4: aa = aa2; Chris@4: } Chris@4: Chris@4: return exitValue; Chris@4: } Chris@4: Chris@4: Chris@4: /*-----------------------------------------------------------*/ Chris@4: /*--- end bzip2.c ---*/ Chris@4: /*-----------------------------------------------------------*/