📄 bzip2.c
字号:
while (bzerr == BZ_OK) { nread = bzRead ( &bzerr, bzf, obuf, 5000 ); if (bzerr == BZ_DATA_ERROR_MAGIC) goto errhandler; } if (bzerr != BZ_STREAM_END) goto errhandler; bzReadGetUnused ( &bzerr, bzf, (void**)(&unusedTmp), &nUnused ); if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" ); for (i = 0; i < nUnused; i++) unused[i] = unusedTmp[i]; bzReadClose ( &bzerr, bzf ); if (bzerr != BZ_OK) panic ( "test:bzReadGetUnused" ); if (nUnused == 0 && myfeof(zStream)) break; } if (ferror(zStream)) goto errhandler_io; ret = fclose ( zStream ); if (ret == EOF) goto errhandler_io; if (verbosity >= 2) fprintf ( stderr, "\n " ); return True; errhandler: bzReadClose ( &bzerr_dummy, bzf ); switch (bzerr) { case BZ_IO_ERROR: errhandler_io: ioError(); break; case BZ_DATA_ERROR: fprintf ( stderr, "\n%s: data integrity (CRC) error in data\n", inName ); return False; case BZ_MEM_ERROR: outOfMemory(); case BZ_UNEXPECTED_EOF: fprintf ( stderr, "\n%s: file ends unexpectedly\n", inName ); return False; case BZ_DATA_ERROR_MAGIC: if (streamNo == 1) { fprintf ( stderr, "\n%s: bad magic number (ie, not created by bzip2)\n", inName ); return False; } else { fprintf ( stderr, "\n%s: %s: trailing garbage after EOF ignored\n", progName, inName ); return True; } default: panic ( "test:unexpected error" ); } panic ( "test:end" ); return True; /*notreached*/}/*---------------------------------------------------*//*--- Error [non-] handling grunge ---*//*---------------------------------------------------*//*---------------------------------------------*/void cadvise ( void ){ fprintf ( stderr, "\nIt is possible that the compressed file(s) have become corrupted.\n" "You can use the -tvv option to test integrity of such files.\n\n" "You can use the `bzip2recover' program to *attempt* to recover\n" "data from undamaged sections of corrupted files.\n\n" );}/*---------------------------------------------*/void showFileNames ( void ){ fprintf ( stderr, "\tInput file = %s, output file = %s\n", inName, outName );}/*---------------------------------------------*/void cleanUpAndFail ( Int32 ec ){ IntNative retVal; if ( srcMode == SM_F2F && opMode != OM_TEST ) { fprintf ( stderr, "%s: Deleting output file %s, if it exists.\n", progName, outName ); if (outputHandleJustInCase != NULL) fclose ( outputHandleJustInCase ); retVal = remove ( outName ); if (retVal != 0) fprintf ( stderr, "%s: WARNING: deletion of output file (apparently) failed.\n", progName ); } if (numFileNames > 0 && numFilesProcessed < numFileNames) { fprintf ( stderr, "%s: WARNING: some files have not been processed:\n" "\t%d specified on command line, %d not processed yet.\n\n", progName, numFileNames, numFileNames - numFilesProcessed ); } exit ( ec );}/*---------------------------------------------*/void panic ( Char* s ){ fprintf ( stderr, "\n%s: PANIC -- internal consistency error:\n" "\t%s\n" "\tThis is a BUG. Please report it to me at:\n" "\tjseward@acm.org\n", progName, s ); showFileNames(); cleanUpAndFail( 3 );}/*---------------------------------------------*/void crcError (){ fprintf ( stderr, "\n%s: Data integrity error when decompressing.\n", progName ); showFileNames(); cadvise(); cleanUpAndFail( 2 );}/*---------------------------------------------*/void compressedStreamEOF ( void ){ fprintf ( stderr, "\n%s: Compressed file ends unexpectedly;\n\t" "perhaps it is corrupted? *Possible* reason follows.\n", progName ); perror ( progName ); showFileNames(); cadvise(); cleanUpAndFail( 2 );}/*---------------------------------------------*/void ioError ( ){ fprintf ( stderr, "\n%s: I/O or other error, bailing out. Possible reason follows.\n", progName ); perror ( progName ); showFileNames(); cleanUpAndFail( 1 );}/*---------------------------------------------*/void mySignalCatcher ( IntNative n ){ fprintf ( stderr, "\n%s: Control-C (or similar) caught, quitting.\n", progName ); cleanUpAndFail(1);}/*---------------------------------------------*/void mySIGSEGVorSIGBUScatcher ( IntNative n ){ if (opMode == OM_Z) fprintf ( stderr, "\n%s: Caught a SIGSEGV or SIGBUS whilst compressing,\n" "\twhich probably indicates a bug in bzip2. Please\n" "\treport it to me at: jseward@acm.org\n", progName ); else fprintf ( stderr, "\n%s: Caught a SIGSEGV or SIGBUS whilst decompressing,\n" "\twhich probably indicates that the compressed data\n" "\tis corrupted.\n", progName ); showFileNames(); if (opMode == OM_Z) cleanUpAndFail( 3 ); else { cadvise(); cleanUpAndFail( 2 ); }}/*---------------------------------------------*/void outOfMemory ( void ){ fprintf ( stderr, "\n%s: couldn't allocate enough memory\n", progName ); showFileNames(); cleanUpAndFail(1);}/*---------------------------------------------------*//*--- The main driver machinery ---*//*---------------------------------------------------*//*---------------------------------------------*/void pad ( Char *s ){ Int32 i; if ( (Int32)strlen(s) >= longestFileName ) return; for (i = 1; i <= longestFileName - (Int32)strlen(s); i++) fprintf ( stderr, " " );}/*---------------------------------------------*/void copyFileName ( Char* to, Char* from ) { if ( strlen(from) > FILE_NAME_LEN-10 ) { fprintf ( stderr, "bzip2: file name\n`%s'\nis suspiciously (> 1024 chars) long.\n" "Try using a reasonable file name instead. Sorry! :)\n", from ); exit(1); } strncpy(to,from,FILE_NAME_LEN-10); to[FILE_NAME_LEN-10]='\0';}/*---------------------------------------------*/Bool fileExists ( Char* name ){ FILE *tmp = fopen ( name, "rb" ); Bool exists = (tmp != NULL); if (tmp != NULL) fclose ( tmp ); return exists;}/*---------------------------------------------*//*-- if in doubt, return True--*/Bool notAStandardFile ( Char* name ){ IntNative i; struct MY_STAT statBuf; i = MY_LSTAT ( name, &statBuf ); if (i != 0) return True; if (MY_S_IFREG(statBuf.st_mode)) return False; return True;}/*---------------------------------------------*/void copyDatePermissionsAndOwner ( Char *srcName, Char *dstName ){#if BZ_UNIX IntNative retVal; struct MY_STAT statBuf; struct utimbuf uTimBuf; retVal = MY_LSTAT ( srcName, &statBuf ); ERROR_IF_NOT_ZERO ( retVal ); uTimBuf.actime = statBuf.st_atime; uTimBuf.modtime = statBuf.st_mtime; retVal = chmod ( dstName, statBuf.st_mode ); ERROR_IF_NOT_ZERO ( retVal ); /* Not sure if this is really portable or not. Causes problems on my x86-Linux Redhat 5.0 box. Decided to omit it from 0.9.0. JRS, 27 June 98. If you understand Unix file semantics and portability issues well enough to fix this properly, drop me a line at jseward@acm.org. retVal = chown ( dstName, statBuf.st_uid, statBuf.st_gid ); ERROR_IF_NOT_ZERO ( retVal ); */ retVal = utime ( dstName, &uTimBuf ); ERROR_IF_NOT_ZERO ( retVal );#endif}/*---------------------------------------------*/void setInterimPermissions ( Char *dstName ){#if BZ_UNIX IntNative retVal; retVal = chmod ( dstName, S_IRUSR | S_IWUSR ); ERROR_IF_NOT_ZERO ( retVal );#endif}/*---------------------------------------------*/Bool endsInBz2 ( Char* name ){ Int32 n = strlen ( name ); if (n <= 4) return False; return (name[n-4] == '.' && name[n-3] == 'b' && name[n-2] == 'z' && name[n-1] == '2');}/*---------------------------------------------*/Bool containsDubiousChars ( Char* name ){ Bool cdc = False; for (; *name != '\0'; name++) if (*name == '?' || *name == '*') cdc = True; return cdc;}/*---------------------------------------------*/void compress ( Char *name ){ FILE *inStr; FILE *outStr; if (name == NULL && srcMode != SM_I2O) panic ( "compress: bad modes\n" ); switch (srcMode) { case SM_I2O: copyFileName ( inName, "(stdin)" ); copyFileName ( outName, "(stdout)" ); break; case SM_F2F: copyFileName ( inName, name ); copyFileName ( outName, name ); strcat ( outName, ".bz2" ); break; case SM_F2O: copyFileName ( inName, name ); copyFileName ( outName, "(stdout)" ); break; } if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) { fprintf ( stderr, "%s: There are no files matching `%s'.\n", progName, inName ); return; } if ( srcMode != SM_I2O && !fileExists ( inName ) ) { fprintf ( stderr, "%s: Input file %s doesn't exist, skipping.\n", progName, inName ); return; } if ( srcMode != SM_I2O && endsInBz2 ( inName )) { fprintf ( stderr, "%s: Input file name %s ends in `.bz2', skipping.\n", progName, inName ); return; } if ( srcMode != SM_I2O && notAStandardFile ( inName )) { fprintf ( stderr, "%s: Input file %s is not a normal file, skipping.\n", progName, inName ); return; } if ( srcMode == SM_F2F && !forceOverwrite && fileExists ( outName ) ) { fprintf ( stderr, "%s: Output file %s already exists, skipping.\n", progName, outName ); return; } switch ( srcMode ) { case SM_I2O: inStr = stdin; outStr = stdout; if ( isatty ( fileno ( stdout ) ) ) { fprintf ( stderr, "%s: I won't write compressed data to a terminal.\n", progName ); fprintf ( stderr, "%s: For help, type: `%s --help'.\n", progName, progName ); return; }; break; case SM_F2O: inStr = fopen ( inName, "rb" ); outStr = stdout; if ( isatty ( fileno ( stdout ) ) ) { fprintf ( stderr, "%s: I won't write compressed data to a terminal.\n", progName ); fprintf ( stderr, "%s: For help, type: `%s --help'.\n", progName, progName ); return; }; if ( inStr == NULL ) { fprintf ( stderr, "%s: Can't open input file %s, skipping.\n", progName, inName ); return; }; break; case SM_F2F: inStr = fopen ( inName, "rb" ); outStr = fopen ( outName, "wb" ); if ( outStr == NULL) { fprintf ( stderr, "%s: Can't create output file %s, skipping.\n", progName, outName ); return; } if ( inStr == NULL ) { fprintf ( stderr, "%s: Can't open input file %s, skipping.\n", progName, inName ); return; }; setInterimPermissions ( outName ); break; default: panic ( "compress: bad srcMode" ); break; } if (verbosity >= 1) { fprintf ( stderr, " %s: ", inName ); pad ( inName ); fflush ( stderr ); } /*--- Now the input and output handles are sane. Do the Biz. ---*/ outputHandleJustInCase = outStr; compressStream ( inStr, outStr ); outputHandleJustInCase = NULL; /*--- If there was an I/O error, we won't get here. ---*/ if ( srcMode == SM_F2F ) { copyDatePermissionsAndOwner ( inName, outName ); if ( !keepInputFiles ) { IntNative retVal = remove ( inName ); ERROR_IF_NOT_ZERO ( retVal ); } }}/*---------------------------------------------*/void uncompress ( Char *name ){ FILE *inStr; FILE *outStr; Bool magicNumberOK; if (name == NULL && srcMode != SM_I2O) panic ( "uncompress: bad modes\n" ); switch (srcMode) { case SM_I2O: copyFileName ( inName, "(stdin)" ); copyFileName ( outName, "(stdout)" ); break; case SM_F2F: copyFileName ( inName, name ); copyFileName ( outName, name ); if (endsInBz2 ( outName )) outName [ strlen ( outName ) - 4 ] = '\0'; break; case SM_F2O: copyFileName ( inName, name ); copyFileName ( outName, "(stdout)" ); break; } if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) { fprintf ( stderr, "%s: There are no files matching `%s'.\n", progName, inName ); return; } if ( srcMode != SM_I2O && !fileExists ( inName ) ) { fprintf ( stderr, "%s: Input file %s doesn't exist, skipping.\n", progName, inName ); return; } if ( srcMode != SM_I2O && !endsInBz2 ( inName )) { fprintf ( stderr, "%s: Input file name %s doesn't end in `.bz2', skipping.\n", progName, inName ); return; } if ( srcMode != SM_I2O && notAStandardFile ( inName )) { fprintf ( stderr, "%s: Input file %s is not a normal file, skipping.\n", progName, inName ); return; } if ( srcMode == SM_F2F && !forceOverwrite && fileExists ( outName ) ) { fprintf ( stderr, "%s: Output file %s already exists, skipping.\n", progName, outName ); return; } switch ( srcMode ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -