📄 comptool.c
字号:
}
if ((fhSrc = open(rgchSrcFile, O_RDONLY | O_BINARY)) == -1)
{
printf(szCantOpenInput, rgchSrcFile);
exit(21);
}
if (WReadHeaderInfo(fhSrc) >= wAlgTypeNoCompress)
{
FFreeHeaderInfo();
if (!vfForceOverwrite)
printf(szAlreadyCompressed, rgchSrcFile);
if (vfForceOverwrite || FUserConfirmsYN())
lseek(fhSrc, 0L, SEEK_SET);
else
{
close(fhSrc);
exit(22);
}
}
DividePathIntoBaseExt(rgchSrcFile, &szSrcBase, &szSrcExt);
if (szSrcExt != NULL)
{
*(szSrcExt - 1) = '\0'; /* remove the '.' between base and ext */
fDotRemoved = TRUE;
}
else
szSrcExt = "";
lcbSrc = filelength(fhSrc);
if (lcbSrc == -1L)
ExitErrorMsgRc(rcReadSeekError);
/* set global variables */
if (strlen(szSrcBase) > _MAX_FNAME - 1 || strlen(szSrcExt) > _MAX_EXT - 1)
{
printf(szFilenameTooLongSz, szSrcBase, szSrcExt);
exit(4);
}
if (fIncludeSrcLength)
vlcbSrcLength = 0L;
if (fIncludeChecksum)
vfChecksum = (BOOL)TRUE;
if (fIncludeBase)
strcpy(vszBaseName, szSrcBase);
if (fIncludeExt)
{
if (*szSrcExt == '\0') /* use a space to indicate a null extension */
strcpy(vszExtension, " ");
else
strcpy(vszExtension, szSrcExt);
}
#ifdef EAS
#ifdef OS2_VER
if (fIncludeEAs)
{
vuscbEAs = 0;
if (fDotRemoved)
*(szSrcExt - 1) = '.';
if (!QueryEAs(rgchSrcFile, &vrgbEAs))
printf("Unable to read in file's EAs.\n");
if (fDotRemoved)
*(szSrcExt - 1) = '\0';
if (vrgbEAs)
vuscbEAs = (USHORT)(*((LONG far *)vrgbEAs));
}
#endif /* !OS2_VER */
#endif /* EAS */
if (fDotRemoved)
*(--szSrcExt) = '.'; /* replace what we took out earlier */
if (cbText > 0)
{
vcbText = cbText;
vszText = szText;
}
if (fComputeOnly)
{
lcbCompressed = LcbCalculateCompressedLength(wAlgType, fhSrc, NIL);
close(fhSrc);
if (lcbCompressed < rcNoError)
ExitErrorMsgRc((SHORT)lcbCompressed);
else
{
printf(szBytesCompressed, lcbSrc, lcbCompressed);
++cFiles;
lcbSrcTotal += lcbSrc;
lcbDestTotal += lcbCompressed;
if (lcbCompressed < lcbSrc)
printf(szPercentSavings,
((200 * (lcbSrc - lcbCompressed) / lcbSrc) + 1 ) / 2);
else
printf(szNoSavings);
return;
}
}
#ifdef OS2_VER
if (DosQFileInfo((HFILE)fhSrc, (USHORT)0x0001, &fsSrc,
(USHORT)(sizeof(fsSrc))))
#else /* not OS2_VER */
if (_dos_getftime(fhSrc, &usDate, &usTime))
#endif /* OS2_VER */
ExitErrorMsgRc(rcReadError);
/* LOOP TO DO COMPRESSION */
do /* while (!eof(fhSrc)) */
{
if (!FEnsureSzBaseDoesNotExist(rgchDestArg))
{
close(fhSrc);
exit(24);
}
if ((fhDest = open(rgchDestArg, O_RDWR | O_CREAT | O_BINARY | O_TRUNC,
S_IREAD | S_IWRITE)) == -1)
{
close(fhSrc);
printf(szCantOpenOutput, rgchDestArg);
exit(23);
}
lcbDest = LcbCompressToFile(wAlgType, fhSrc, fhDest, lcbSize);
#ifdef OS2_VER
if (DosSetFileInfo((HFILE)fhDest, (USHORT)0x0001, &fsSrc,
(USHORT)(sizeof(fsSrc))))
#else /* not OS2_VER */
if (_dos_setftime(fhDest, usDate, usTime))
#endif /* OS2_VER */
ExitErrorMsgRc(rcWriteError);
close(fhDest);
if (lcbDest < rcNoError)
{
close(fhSrc);
remove(rgchDestArg);
ExitErrorMsgRc((SHORT)lcbDest);
}
printf(szWroteBytes, lcbDest, rgchDestArg);
lcbCompressed += lcbDest;
if (!eof(fhSrc))
{
if (!FIncrementSzBase(rgchDestArg))
{
close(fhSrc);
exit(25);
}
}
} while (!eof(fhSrc) && !fOnePieceOnly);
if (!eof(fhSrc))
{
LONG lcbTail = lcbSrc - tell(fhSrc);
BYTE rgbBuffer[128];
SHORT cbSize;
lcbSrc -= lcbTail; /* uncompressed bytes of first piece */
printf(szBytesCompressed, lcbSrc, lcbCompressed);
++cFiles;
lcbSrcTotal += lcbSrc;
lcbDestTotal += lcbCompressed;
if (lcbCompressed >= lcbSrc)
printf(szNoSavings);
else
printf(szPercentSavings,
((200 * (lcbSrc - lcbCompressed) / lcbSrc) + 1 ) / 2);
if (!FEnsureSzBaseDoesNotExist(rgchDestArg))
{
close(fhSrc);
exit(24);
}
printf(szTailBytes, lcbTail, rgchDestArg);
if ((fhDest = open(rgchDestArg, O_RDWR | O_CREAT | O_BINARY | O_TRUNC,
S_IREAD | S_IWRITE)) == -1)
{
close(fhSrc);
printf(szCantOpenOutput, rgchDestArg);
exit(23);
}
if ((cbSize = read(fhSrc, rgbBuffer, 128)) <= 0)
{
close(fhSrc);
ExitErrorMsgRc(rcReadError);
}
while (cbSize > 0)
{
if (write(fhDest, rgbBuffer, cbSize) != cbSize)
{
close(fhSrc);
close(fhDest);
ExitErrorMsgRc(rcWriteError);
}
cbSize = read(fhSrc, rgbBuffer, 128);
}
#ifdef OS2_VER
if (DosSetFileInfo((HFILE)fhDest, (USHORT)0x0001, &fsSrc,
(USHORT)(sizeof(fsSrc))))
#else /* not OS2_VER */
if (_dos_setftime(fhDest, usDate, usTime))
#endif /* OS2_VER */
ExitErrorMsgRc(rcWriteError);
close(fhDest);
}
else
{
printf(szBytesCompressed, lcbSrc, lcbCompressed);
++cFiles;
lcbSrcTotal += lcbSrc;
lcbDestTotal += lcbCompressed;
if (lcbCompressed >= lcbSrc)
printf(szNoSavings);
else
printf(szPercentSavings,
((200 * (lcbSrc - lcbCompressed) / lcbSrc) + 1 ) / 2);
}
close(fhSrc);
}
/*
** void Usage(void)
**
** Print usage message when command line args are bad.
*/
void Usage(SHORT wExitCode)
{
printf(szCompUsage1);
printf(szCompUsage2, wAlgTypeDefault);
#ifdef NC_XOR1
printf(szCompUsage3, wAlgTypeNoCompress);
printf(szCompUsage4, wAlgTypeXOR1);
#endif
#ifdef ZK1
printf(szCompUsage5, wAlgTypeZK1);
#endif
#ifdef JJJ1
printf(szCompUsageJJJ1, wAlgTypeJJJ1);
#endif
printf(szCompUsage6);
printf(szCompUsage7);
printf(szCompUsage8);
printf(szCompUsage9);
printf(szCompUsage10);
printf(szCompUsage11);
printf(szCompUsage12);
printf(szCompUsage13);
printf(szCompUsage14);
printf(szCompUsage15);
printf(szCompUsage16);
printf(szCompUsage17);
printf(szCompUsage18);
printf(szCompUsage19);
#ifdef EAS
printf(szCompUsage20);
#endif /* EAS */
exit(wExitCode);
}
/*
** BOOL FEnsureSzBaseDoesNotExist(SZ szBaseFile)
*/
BOOL FEnsureSzBaseDoesNotExist(SZ szBaseFile)
{
if (access(szBaseFile, 00))
return((BOOL)TRUE);
if (!vfForceOverwrite)
{
printf(szFileExists, szBaseFile);
printf(szOverwriteYN);
}
if (vfForceOverwrite || FUserConfirmsYN())
{
chmod(szBaseFile, S_IWRITE | S_IREAD);
remove(szBaseFile);
if (access(szBaseFile, 00))
return((BOOL)TRUE);
printf(szCantOverwrite, szBaseFile);
if (!FIncrementSzBase(szBaseFile))
return(FALSE);
}
else
if (!FIncrementSzBase(szBaseFile))
return(FALSE);
return(FEnsureSzBaseDoesNotExist(szBaseFile));
}
/*
** BOOL FIncrementSzBase(SZ szBaseFile)
**
** If the last character of szBaseFile (not counting the extension) is
** a digit, then we can increment the filename. Otherwise, change it to
** a two.
*/
BOOL FIncrementSzBase(SZ szBaseFile)
{
char *pchBase;
char *pchExt;
char *pchDigit;
char szNewName[13];
DividePathIntoBaseExt(szBaseFile, &pchBase, &pchExt);
if (pchExt != NULL)
*(--pchExt) = '\0'; /* erase the '.' between base and ext */
if (strlen(pchBase) >= 8)
pchDigit = pchBase + 7;
else
pchDigit = pchBase + strlen(pchBase) - 1;
if (!isdigit(*pchDigit))
{
if (strlen(pchBase) >= 8)
{
*pchDigit = '2';
if (pchExt != NULL)
*pchExt = '.';
}
else
{
strcpy(szNewName, pchBase);
if (pchExt != NULL)
{
strcat(szNewName, "2.");
strcat(szNewName, pchExt + 1);
}
else
strcat(szNewName, "2");
strcpy(pchBase, szNewName);
}
return((BOOL)TRUE);
}
if (*pchDigit == '9')
{
printf(szNoOutputNames);
return(FALSE);
}
(*pchDigit)++;
if (pchExt != NULL)
*pchExt = '.';
return((BOOL)TRUE);
}
#ifdef EAS
#ifdef OS2_VER
#define MAX_GEA 300 /* Max size for a GEA List */
#define Ref_ASCIIZ 1 /* Reference type for DosEnumAttribute */
#define GetInfoLevel1 1 /* Get info from SFT */
#define GetInfoLevel3 3 /* Get FEAlist given the GEAlist */
BOOL QueryEAs(CHAR * pszPath, CHAR ** pEABuf)
{
struct FSPlus
{
FILESTATUS fs;
long easize;
} fsp;
USHORT usRet;
ULONG ulCurFEA, dAlloc;
CHAR * pAlloc, * pAlloc2;
CHAR * pRetBuf;
ULONG ulEntryNum = 1; /* count of current EA to read (1-relative) */
ULONG ulEnumCnt; /* Number of EAs for Enum to return, always 1 */
FEA * pFEA; /* Used to read from Enum's return buffer */
GEALIST * pGEAList; /* Ptr used to set up buffer for DosQPathInfo call */
EAOP eaopGet; /* Used to call DosQPathInfo */
*pEABuf = NULL;
usRet = DosQPathInfo(pszPath, FIL_QUERYEASIZE, (char *)&fsp, sizeof(fsp),
0L);
if (usRet)
{
printf("Can't even query the EAs size. Error: %u\n",usRet);
printf("File name: '%s'\n",pszPath);
return FALSE;
}
if (fsp.easize == 4L)
return TRUE;
pRetBuf = malloc((USHORT)fsp.easize);
if (!pRetBuf)
{
printf("Couldn't allocated %ld bytes on the far heap...\n",fsp.easize);
return FALSE;
}
*pEABuf = pRetBuf;
ulCurFEA = fsp.easize - 4;
pAlloc = malloc(MAX_GEA);
pAlloc2 = malloc(MAX_GEA);
if (!pAlloc || !pAlloc2)
{
free(pRetBuf);
pRetBuf = NULL;
printf("Couldn't allocated the 500 bytes of local memory needed...\n");
return FALSE;
}
pFEA = (FEA *)pAlloc;
while (TRUE)
{
ulEnumCnt = 1;
if (DosEnumAttribute(Ref_ASCIIZ, pszPath, ulEntryNum, pAlloc, MAX_GEA,
&ulEnumCnt, (LONG)GetInfoLevel1, 0L))
break;
if (ulEnumCnt != 1)
break;
ulEntryNum++;
dAlloc = (ULONG)(pFEA->cbName + 1 + pFEA->cbValue + sizeof(FEA));
ulCurFEA -= dAlloc;
pGEAList = (GEALIST *)pAlloc2;
pGEAList->cbList = sizeof(GEALIST) + pFEA->cbName;
pGEAList->list[0].cbName = pFEA->cbName;
strcpy(pGEAList->list[0].szName, pAlloc + sizeof(FEA));
eaopGet.fpGEAList = (GEALIST far *)pAlloc2;
eaopGet.fpFEAList = (FEALIST far *)&pRetBuf[ulCurFEA];
eaopGet.fpFEAList->cbList = dAlloc + sizeof(ULONG);
usRet = DosQPathInfo(pszPath, GetInfoLevel3, (PVOID)&eaopGet,
sizeof(EAOP), 0L);
if (usRet)
{
printf("Error on reading EA from disk\n");
free(pAlloc);
free(pAlloc2);
return FALSE;
}
}
free(pAlloc);
free(pAlloc2);
if (ulCurFEA != 0)
{
printf("Checksum problems...\n");
return FALSE;
}
eaopGet.fpFEAList->cbList = fsp.easize;
return(TRUE);
}
#endif /* !OS2_VER */
#endif /* EAS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -