📄 bzip2.c
字号:
case SM_I2O: inStr = stdin; outStr = stdout; if ( isatty ( fileno ( stdin ) ) ) { fprintf ( stderr, "%s: I won't read compressed data from 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 ( 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 ( "uncompress: 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; magicNumberOK = uncompressStream ( inStr, outStr ); outputHandleJustInCase = NULL; /*--- If there was an I/O error, we won't get here. ---*/ if ( magicNumberOK ) { if ( srcMode == SM_F2F ) { copyDatePermissionsAndOwner ( inName, outName ); if ( !keepInputFiles ) { IntNative retVal = remove ( inName ); ERROR_IF_NOT_ZERO ( retVal ); } } } else { if ( srcMode == SM_F2F ) { IntNative retVal = remove ( outName ); ERROR_IF_NOT_ZERO ( retVal ); } } if ( magicNumberOK ) { if (verbosity >= 1) fprintf ( stderr, "done\n" ); } else { if (verbosity >= 1) fprintf ( stderr, "not a bzip2 file, skipping.\n" ); else fprintf ( stderr, "%s: %s is not a bzip2 file, skipping.\n", progName, inName ); }}/*---------------------------------------------*/void testf ( Char *name ){ FILE *inStr; Bool allOK; if (name == NULL && srcMode != SM_I2O) panic ( "testf: bad modes\n" ); copyFileName ( outName, "(none)" ); switch (srcMode) { case SM_I2O: copyFileName ( inName, "(stdin)" ); break; case SM_F2F: copyFileName ( inName, name ); break; case SM_F2O: copyFileName ( inName, name ); 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; } switch ( srcMode ) { case SM_I2O: if ( isatty ( fileno ( stdin ) ) ) { fprintf ( stderr, "%s: I won't read compressed data from a terminal.\n", progName ); fprintf ( stderr, "%s: For help, type: `%s --help'.\n", progName, progName ); return; }; inStr = stdin; break; case SM_F2O: case SM_F2F: inStr = fopen ( inName, "rb" ); if ( inStr == NULL ) { fprintf ( stderr, "%s: Can't open input file %s, skipping.\n", progName, inName ); return; }; break; default: panic ( "testf: bad srcMode" ); break; } if (verbosity >= 1) { fprintf ( stderr, " %s: ", inName ); pad ( inName ); fflush ( stderr ); } /*--- Now the input handle is sane. Do the Biz. ---*/ allOK = testStream ( inStr ); if (allOK && verbosity >= 1) fprintf ( stderr, "ok\n" ); if (!allOK) testFailsExist = True;}/*---------------------------------------------*/void license ( void ){ fprintf ( stderr, "bzip2, a block-sorting file compressor. " "Version 0.9.0c, 18-Oct-98.\n" " \n" " Copyright (C) 1996, 1997, 1998 by Julian Seward.\n" " \n" " This program is free software; you can redistribute it and/or modify\n" " it under the terms set out in the LICENSE file, which is included\n" " in the bzip2-0.9.0c source distribution.\n" " \n" " This program is distributed in the hope that it will be useful,\n" " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" " LICENSE file for more details.\n" " \n" );}/*---------------------------------------------*/void usage ( Char *fullProgName ){ fprintf ( stderr, "bzip2, a block-sorting file compressor. " "Version 0.9.0c, 18-Oct-98.\n" "\n usage: %s [flags and input files in any order]\n" "\n" " -h --help print this message\n" " -d --decompress force decompression\n" " -z --compress force compression\n" " -k --keep keep (don't delete) input files\n" " -f --force overwrite existing output filess\n" " -t --test test compressed file integrity\n" " -c --stdout output to standard out\n" " -v --verbose be verbose (a 2nd -v gives more)\n" " -L --license display software version & license\n" " -V --version display software version & license\n" " -s --small use less memory (at most 2500k)\n" " -1 .. -9 set block size to 100k .. 900k\n" " --repetitive-fast compress repetitive blocks faster\n" " --repetitive-best compress repetitive blocks better\n" "\n" " If invoked as `bzip2', default action is to compress.\n" " as `bunzip2', default action is to decompress.\n" " as `bz2cat', default action is to decompress to stdout.\n" "\n" " If no file names are given, bzip2 compresses or decompresses\n" " from standard input to standard output. You can combine\n" " short flags, so `-v -4' means the same as -v4 or -4v, &c.\n"#if BZ_UNIX "\n"#endif , fullProgName );}/*---------------------------------------------*//*-- All the garbage from here to main() is purely to implement a linked list of command-line arguments, into which main() copies argv[1 .. argc-1]. The purpose of this ridiculous exercise is to facilitate the expansion of wildcard characters * and ? in filenames for halfwitted OSs like MSDOS, Windows 95 and NT. The actual Dirty Work is done by the platform-specific macro APPEND_FILESPEC.--*/typedef struct zzzz { Char *name; struct zzzz *link; } Cell;/*---------------------------------------------*/void *myMalloc ( Int32 n ){ void* p; p = malloc ( (size_t)n ); if (p == NULL) outOfMemory (); return p;}/*---------------------------------------------*/Cell *mkCell ( void ){ Cell *c; c = (Cell*) myMalloc ( sizeof ( Cell ) ); c->name = NULL; c->link = NULL; return c;}/*---------------------------------------------*/Cell *snocString ( Cell *root, Char *name ){ if (root == NULL) { Cell *tmp = mkCell(); tmp->name = (Char*) myMalloc ( 5 + strlen(name) ); strcpy ( tmp->name, name ); return tmp; } else { Cell *tmp = root; while (tmp->link != NULL) tmp = tmp->link; tmp->link = snocString ( tmp->link, name ); return root; }}/*---------------------------------------------*/#define ISFLAG(s) (strcmp(aa->name, (s))==0)IntNative main ( IntNative argc, Char *argv[] ){ Int32 i, j; Char *tmp; Cell *argList; Cell *aa; /*-- Be really really really paranoid :-) --*/ if (sizeof(Int32) != 4 || sizeof(UInt32) != 4 || sizeof(Int16) != 2 || sizeof(UInt16) != 2 || sizeof(Char) != 1 || sizeof(UChar) != 1) { fprintf ( stderr, "bzip2: I'm not configured correctly for this platform!\n" "\tI require Int32, Int16 and Char to have sizes\n" "\tof 4, 2 and 1 bytes to run properly, and they don't.\n" "\tProbably you can fix this by defining them correctly,\n" "\tand recompiling. Bye!\n" ); exit(3); } /*-- Set up signal handlers --*/ signal (SIGINT, mySignalCatcher); signal (SIGTERM, mySignalCatcher); signal (SIGSEGV, mySIGSEGVorSIGBUScatcher);#if BZ_UNIX signal (SIGHUP, mySignalCatcher); signal (SIGBUS, mySIGSEGVorSIGBUScatcher);#endif /*-- Initialise --*/ outputHandleJustInCase = NULL; smallMode = False; keepInputFiles = False; forceOverwrite = False; verbosity = 0; blockSize100k = 9; testFailsExist = False; numFileNames = 0; numFilesProcessed = 0; workFactor = 30; copyFileName ( inName, "(none)" ); copyFileName ( outName, "(none)" ); copyFileName ( progNameReally, argv[0] ); progName = &progNameReally[0]; for (tmp = &progNameReally[0]; *tmp != '\0'; tmp++) if (*tmp == PATH_SEP) progName = tmp + 1; /*-- Expand filename wildcards in arg list --*/ argList = NULL; for (i = 1; i <= argc-1; i++) APPEND_FILESPEC(argList, argv[i]); /*-- Find the length of the longest filename --*/ longestFileName = 7; numFileNames = 0; for (aa = argList; aa != NULL; aa = aa->link) if (aa->name[0] != '-') { numFileNames++; if (longestFileName < (Int32)strlen(aa->name) ) longestFileName = (Int32)strlen(aa->name); } /*-- Determine source modes; flag handling may change this too. --*/ if (numFileNames == 0) srcMode = SM_I2O; else srcMode = SM_F2F; /*-- Determine what to do (compress/uncompress/test/cat). --*/ /*-- Note that subsequent flag handling may change this. --*/ opMode = OM_Z; if ( (strstr ( progName, "unzip" ) != 0) || (strstr ( progName, "UNZIP" ) != 0) ) opMode = OM_UNZ; if ( (strstr ( progName, "z2cat" ) != 0) || (strstr ( progName, "Z2CAT" ) != 0) || (strstr ( progName, "zcat" ) != 0) || (strstr ( progName, "ZCAT" ) != 0) ) { opMode = OM_UNZ; srcMode = (numFileNames == 0) ? SM_I2O : SM_F2O; } /*-- Look at the flags. --*/ for (aa = argList; aa != NULL; aa = aa->link) if (aa->name[0] == '-' && aa->name[1] != '-') for (j = 1; aa->name[j] != '\0'; j++) switch (aa->name[j]) { case 'c': srcMode = SM_F2O; break; case 'd': opMode = OM_UNZ; break; case 'z': opMode = OM_Z; break; case 'f': forceOverwrite = True; break; case 't': opMode = OM_TEST; break; case 'k': keepInputFiles = True; break; case 's': smallMode = True; break; case '1': blockSize100k = 1; break; case '2': blockSize100k = 2; break; case '3': blockSize100k = 3; break; case '4': blockSize100k = 4; break; case '5': blockSize100k = 5; break; case '6': blockSize100k = 6; break; case '7': blockSize100k = 7; break; case '8': blockSize100k = 8; break; case '9': blockSize100k = 9; break; case 'V': case 'L': license(); break; case 'v': verbosity++; break; case 'h': usage ( progName ); exit ( 1 ); break; default: fprintf ( stderr, "%s: Bad flag `%s'\n", progName, aa->name ); usage ( progName ); exit ( 1 ); break; } /*-- And again ... --*/ for (aa = argList; aa != NULL; aa = aa->link) { if (ISFLAG("--stdout")) srcMode = SM_F2O; else if (ISFLAG("--decompress")) opMode = OM_UNZ; else if (ISFLAG("--compress")) opMode = OM_Z; else if (ISFLAG("--force")) forceOverwrite = True; else if (ISFLAG("--test")) opMode = OM_TEST; else if (ISFLAG("--keep")) keepInputFiles = True; else if (ISFLAG("--small")) smallMode = True; else if (ISFLAG("--version")) license(); else if (ISFLAG("--license")) license(); else if (ISFLAG("--repetitive-fast")) workFactor = 5; else if (ISFLAG("--repetitive-best")) workFactor = 150; else if (ISFLAG("--verbose")) verbosity++; else if (ISFLAG("--help")) { usage ( progName ); exit ( 1 ); } else if (strncmp ( aa->name, "--", 2) == 0) { fprintf ( stderr, "%s: Bad flag `%s'\n", progName, aa->name ); usage ( progName ); exit ( 1 ); } } if (verbosity > 4) verbosity = 4; if (opMode == OM_Z && smallMode) blockSize100k = 2; if (srcMode == SM_F2O && numFileNames == 0) { fprintf ( stderr, "%s: -c expects at least one filename.\n", progName ); exit ( 1 ); } if (opMode == OM_TEST && srcMode == SM_F2O) { fprintf ( stderr, "%s: -c and -t cannot be used together.\n", progName ); exit ( 1 ); } if (opMode != OM_Z) blockSize100k = 0; if (opMode == OM_Z) { if (srcMode == SM_I2O) compress ( NULL ); else for (aa = argList; aa != NULL; aa = aa->link) if (aa->name[0] != '-') { numFilesProcessed++; compress ( aa->name ); } } else if (opMode == OM_UNZ) { if (srcMode == SM_I2O) uncompress ( NULL ); else for (aa = argList; aa != NULL; aa = aa->link) if (aa->name[0] != '-') { numFilesProcessed++; uncompress ( aa->name ); } } else { testFailsExist = False; if (srcMode == SM_I2O) testf ( NULL ); else for (aa = argList; aa != NULL; aa = aa->link) if (aa->name[0] != '-') { numFilesProcessed++; testf ( aa->name ); } if (testFailsExist) { fprintf ( stderr, "\n" "You can use the `bzip2recover' program to *attempt* to recover\n" "data from undamaged sections of corrupted files.\n\n" ); exit(2); } } return 0;}/*-----------------------------------------------------------*//*--- end bzip2.c ---*//*-----------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -