home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cenvi23.zip
/
DIRDIFF.CMM
< prev
next >
Wrap
Text File
|
1996-01-18
|
8KB
|
276 lines
//*******************************************************************
//*** DirDiff.cmm - Compare directory listings for differences in ***
//*** ver.4 file name, file date, or file size. Many ***
//*** thanks to B.A. for making this much faster ***
//*******************************************************************
#include <OptParms.lib>
main(argc,argv)
{
RecursiveListing = OptionalParameter(argc,argv,"R");
if ( ( 1 != argc && 3 != argc )
|| OptionalParameter(argc,argv,"?")
|| OptionalParameter(argc,argv,"?") || OptionalParameter(argc,argv,"HELP") )
Instructions();
// if directories were not specified, then prompt for them
if ( 3 == argc ) {
DirSpec1 = argv[1];
DirSpec2 = argv[2];
} else {
printf("Specify directory listing specification 1: ");
if ( !(DirSpec1 = gets()) || !DirSpec1[0] )
Instructions();
printf("Specify directory listing specification 2: ");
if ( !(DirSpec2 = gets()) || !DirSpec2[0] )
Instructions();
}
// Get sorted list of both directory specifications
DirList1 = GetDirectoryListing(DirSpec1,RecursiveListing);
DirList2 = GetDirectoryListing(DirSpec2,RecursiveListing);
// sort each of the listings to speed up bsearch
qsort(DirList1,"DirectorySortFunction");
qsort(DirList2,"DirectorySortFunction");
lDifferenceFound = False;
// show files in one directory but not the other
if ( ShowFilesNotFound(DirSpec1,DirList1,DirSpec2,DirList2) )
lDifferenceFound = True;
if ( ShowFilesNotFound(DirSpec2,DirList2,DirSpec1,DirList1) )
lDifferenceFound = True;
// show files new in one directory than the other
if ( ShowNewerFiles(DirSpec1,DirList1,DirSpec2,DirList2) )
lDifferenceFound = True;
if ( ShowNewerFiles(DirSpec2,DirList2,DirSpec1,DirList1) )
lDifferenceFound = True;
// show differences in file size
if ( ShowSizeDifferences(DirSpec1,DirList1,DirSpec2,DirList2) )
lDifferenceFound = True;
if ( !lDifferenceFound )
printf("No differences found.\n");
DirDiffExit( lDifferenceFound ? EXIT_FAILURE : EXIT_SUCCESS );
}
DirDiffExit(ExitCode)
{
if defined(_WINDOWS_) && !defined(_SHELL_) {
printf("\nPress any key to exit...");
getch();
}
exit(ExitCode);
}
GetDirectoryListing(pSearchSpec,pRecurse)
{
// If pSearchSpec is x: or x:\, where X is a drive letter, then just
// append *.* (* for OS/2) for directory listing
if ( !strcmp(pSearchSpec+1,":") || !strcmp(pSearchSpec+1,":\\") ) {
strcat( pSearchSpec, "*.*" );
} else if ( !strpbrk(pSearchSpec,"?*")
&& Directory(pSearchSpec,False,FATTR_SUBDIR,FATTR_SUBDIR) ) {
// If specification has no wildcards and is a directory name, then
// add wildcards
strcat( pSearchSpec, "\\*.*" );
} else {
// If this ends in . or .., possible preceded by : or \, then
// use it as a directory listing
if ( !strcmp(".",pSearchSpec) || !strcmp("..",pSearchSpec) )
strcat( pSearchSpec, "\\*.*" );
}
lDirList = Directory(pSearchSpec,pRecurse);
if ( !lDirList ) {
printf("\aNo files found matching \"%s\"\n",pSearchSpec);
DirDiffExit(EXIT_FAILURE);
}
// For each entry in DirList, remove the pSearchSpec portion of the name
pSearchDirLen = strlen(SplitFileName(pSearchSpec).dir);
for ( lIdx = GetArraySpan(lDirList); 0 <= lIdx; lIdx-- )
lDirList[lIdx].name += pSearchDirLen;
return lDirList;
}
DirectorySortFunction(pDirEntry1,pDirEntry2)
{
return stricmp(pDirEntry1.name,pDirEntry2.name);
}
ShowFilesNotFound(pDirSpec,pDirList,pOtherDirSpec,pOtherDirList)
{
lDifferenceFound = False;
lDirCount = GetArraySpan(pDirList);
lDirCount2 = GetArraySpan(pOtherDirList);
i = 0;
k = 0;
while (i <= lDirCount || k <= lDirCount2)
{
if (i <= lDirCount && k <= lDirCount2)
Result = stricmp(pDirList[i].name,pOtherDirList[k].name);
else if (i > lDirCount)
Result = 1; // increment OtherDirList
else
Result = -1; // increment DirList
if (Result == 0) {
i++; // increment DirList
k++; // increment OtherDirList
}
else if (Result > 0) {
k++ // increment OtherDirList
}
else {
if ( !lDifferenceFound ) {
printf("\nFiles in \"%s\", but not \"%s\":\n",pDirSpec,pOtherDirSpec);
lDifferenceFound = True;
}
printf("%s\n",pDirList[i].name);
i++; // increment DirList
}
}
if ( lDifferenceFound )
printf("\n");
return lDifferenceFound;
}
ShowNewerFiles(pDirSpec,pDirList,pOtherDirSpec,pOtherDirList)
{
lDifferenceFound = False;
lDirCount = GetArraySpan(pDirList);
lDirCount2 = GetArraySpan(pOtherDirList)
i = 0
k = 0
while (i <= lDirCount || k <= lDirCount2)
{
if (i <= lDirCount && k <= lDirCount2)
Result = stricmp(pDirList[i].name,pOtherDirList[k].name);
else if (i > lDirCount)
Result = 1; // increment OtherDirList
else
Result = -1; // increment DirList
if (Result == 0) {
if ( 0 < difftime(pDirList[i].Write,pOtherDirList[k].Write) ) {
if ( !lDifferenceFound ) {
printf("\nNewer in \"%s\" than \"%s\":\n",pDirSpec,pOtherDirSpec);
lDifferenceFound = True;
}
printf("%-40s ",pDirList[i].name);
PrintDirectoryDate(pDirList[i].Write);
printf(" : ");
PrintDirectoryDate(pOtherDirList[k].Write);
printf("\n");
}
i++; // increment DirList
k++; // increment OtherDirList
}
else if (Result > 0) {
k++ // increment OtherDirList
}
else {
i++; // increment DirList
}
}
if ( lDifferenceFound )
printf("\n");
return lDifferenceFound;
}
PrintDirectoryDate(pTime)
{
tm = localtime(pTime);
printf("%2d-%02d-%02d %2d:%02d%c",
tm.tm_mon+1,tm.tm_mday,tm.tm_year%100,
(tm.tm_hour%12) == 0 ? 12 : (tm.tm_hour%12),tm.tm_min,
tm.tm_hour < 12 ? 'a' : 'p');
}
ShowSizeDifferences(pDirSpec,pDirList,pOtherDirSpec,pOtherDirList)
{
lDifferenceFound = False;
lDirCount = GetArraySpan(pDirList);
lDirCount2 = GetArraySpan(pOtherDirList)
i = 0
k = 0
while (i <= lDirCount || k <= lDirCount2)
{
if (i <= lDirCount && k <= lDirCount2)
Result = stricmp(pDirList[i].name,pOtherDirList[k].name);
else if (i > lDirCount)
Result = 1; // increment OtherDirList
else
Result = -1; // increment DirList
if (Result == 0) {
if ( pDirList[i].size != pOtherDirList[k].size ) {
if ( !lDifferenceFound ) {
printf("\nSize differences \"%s\" : \"%s\":\n",pDirSpec,pOtherDirSpec);
lDifferenceFound = True;
}
printf("%s %d : %d\n",pDirList[i].name,
pDirList[i].size,pOtherDirList[k].size);
}
i++; // increment DirList
k++; // increment OtherDirList
}
else if (Result > 0) {
k++ // increment OtherDirList
}
else {
i++; // increment DirList
}
}
if ( lDifferenceFound )
printf("\n");
return lDifferenceFound;
}
Instructions()
{
printf("\a\n")
printf("DirDiff.cmm - Show differences in directory listings\n")
printf("\n")
printf("USAGE: CEnvi DirDiff.cmm <Dir1> <Dir2>\n")
printf("\n")
printf("WHERE: Dir1, Dir2 - Specifications for two directory listings.\n")
printf("\n")
printf("NOTE: For current directory use \".\" or \".\*.*\"\n")
printf("\n")
printf("EXAMPLES: CEnvi DirDiff c:\Mine d:\Theirs\n")
printf(" CEnvi DirDiff.cmm . d:..\*.*\n")
printf("\n")
DirDiffExit(EXIT_FAILURE);
}