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