home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / cenvi29.zip / DIRDIFF.CMM < prev    next >
Text File  |  1994-04-18  |  7KB  |  200 lines

  1. /*******************************************************************
  2.  *** DirDiff.cmm - Compare directory listings for differences in ***
  3.  *** ver.1         file name, file date, or file size            ***
  4.  *******************************************************************/
  5.  
  6. #include <OptParms.lib>
  7.  
  8. main(argc,argv)
  9. {
  10.    RecursiveListing = OptionalParameter(argc,argv,"R");
  11.    if ( ( 1 != argc  &&  3 != argc )
  12.      ||  OptionalParameter(argc,argv,"?")
  13.      || OptionalParameter(argc,argv,"?") || OptionalParameter(argc,argv,"HELP") )
  14.       Instructions();
  15.  
  16.    // if directories were not specified, then prompt for them
  17.    if ( 3 == argc ) {
  18.       DirSpec1 = argv[1];
  19.       DirSpec2 = argv[2];
  20.    } else {
  21.       printf("Specify directory listing specification 1: ");
  22.       if ( !(DirSpec1 = gets()) || !DirSpec1[0] )
  23.          Instructions();
  24.       printf("Specify directory listing specification 2: ");
  25.       if ( !(DirSpec2 = gets()) || !DirSpec2[0] )
  26.          Instructions();
  27.    }
  28.  
  29.    // Get sorted list of both directory specifications
  30.    DirList1 = GetDirectoryListing(DirSpec1,RecursiveListing);
  31.    DirList2 = GetDirectoryListing(DirSpec2,RecursiveListing);
  32.  
  33.    // sort each of the listings to speed up bsearch
  34.    qsort(DirList1,"DirectorySortFunction");
  35.    qsort(DirList2,"DirectorySortFunction");
  36.  
  37.    lDifferenceFound = False;
  38.  
  39.    // show files in one directory but not the other
  40.    if ( ShowFilesNotFound(DirSpec1,DirList1,DirSpec2,DirList2) )
  41.       lDifferenceFound = True;
  42.    if ( ShowFilesNotFound(DirSpec2,DirList2,DirSpec1,DirList1) )
  43.       lDifferenceFound = True;
  44.  
  45.    // show files new in one directory than the other
  46.    if ( ShowNewerFiles(DirSpec1,DirList1,DirSpec2,DirList2) )
  47.       lDifferenceFound = True;
  48.    if ( ShowNewerFiles(DirSpec2,DirList2,DirSpec1,DirList1) )
  49.       lDifferenceFound = True;
  50.  
  51.    // show differences in file size
  52.    if ( ShowSizeDifferences(DirSpec1,DirList1,DirSpec2,DirList2) )
  53.       lDifferenceFound = True;
  54.  
  55.    if ( !lDifferenceFound )
  56.       printf("No differences found.\n");
  57.    DirDiffExit( lDifferenceFound ? EXIT_FAILURE : EXIT_SUCCESS );
  58. }
  59.  
  60.  
  61. DirDiffExit(ExitCode)
  62. {
  63.    if defined(_WINDOWS_) {
  64.       printf("\nPress any key to exit...");
  65.       getch();
  66.    }
  67.    exit(ExitCode);
  68. }
  69.  
  70. GetDirectoryListing(pSearchSpec,pRecurse)
  71. {
  72.    // If pSearchSpec is x: or x:\, where X is a drive letter, then just
  73.    // append *.* (* for OS/2) for directory listing
  74.    if ( !strcmp(pSearchSpec+1,":") || !strcmp(pSearchSpec+1,":\\") ) {
  75.       strcat( pSearchSpec, "*.*" );
  76.    } else if ( !strpbrk(pSearchSpec,"?*")
  77.             && Directory(pSearchSpec,False,FATTR_SUBDIR,FATTR_SUBDIR) ) {
  78.       // If specification has no wildcards and is a directory name, then
  79.       // add wildcards
  80.       strcat( pSearchSpec, "\\*.*" );
  81.    } else {
  82.       // If this ends in . or .., possible preceded by : or \, then
  83.       // use it as a directory listing
  84.       if ( !strcmp(".",pSearchSpec)  ||  !strcmp("..",pSearchSpec) )
  85.          strcat( pSearchSpec, "\\*.*" );
  86.    }
  87.  
  88.    lDirList = Directory(pSearchSpec,pRecurse);
  89.  
  90.    if ( !lDirList ) {
  91.       printf("\aNo files found matching \"%s\"\n",pSearchSpec);
  92.       DirDiffExit(EXIT_FAILURE);
  93.    }
  94.  
  95.    // For each entry in DirList, remove the pSearchSpec portion of the name
  96.    pSearchDirLen = strlen(SplitFileName(pSearchSpec).dir);
  97.    for ( lIdx = GetArraySpan(lDirList); 0 <= lIdx; lIdx-- )
  98.       lDirList[lIdx].name += pSearchDirLen;
  99.  
  100.    return lDirList;
  101. }
  102.  
  103. DirectorySortFunction(pDirEntry1,pDirEntry2)
  104. {
  105.    return stricmp(pDirEntry1.name,pDirEntry2.name);
  106. }
  107.  
  108. ShowFilesNotFound(pDirSpec,pDirList,pOtherDirSpec,pOtherDirList)
  109. {
  110.    lDifferenceFound = False;
  111.    lDirCount = 1 + GetArraySpan(pDirList);
  112.  
  113.    for ( lIdx = 0; lIdx < lDirCount; lIdx++ ) {
  114.       if ( -1 == bsearch(pDirList[lIdx],pOtherDirList,"DirectorySortFunction") ) {
  115.          if ( !lDifferenceFound ) {
  116.             lDifferenceFound = True;
  117.             printf("\nFiles in \"%s\", but not \"%s\":\n",pDirSpec,pOtherDirSpec);
  118.          }
  119.          printf("%s\n",pDirList[lIdx].name);
  120.       }
  121.    }
  122.    if ( lDifferenceFound )
  123.       printf("\n");
  124.    return lDifferenceFound;
  125. }
  126.  
  127. ShowNewerFiles(pDirSpec,pDirList,pOtherDirSpec,pOtherDirList)
  128. {
  129.    lDifferenceFound = False;
  130.    lDirCount = 1 + GetArraySpan(pDirList);
  131.  
  132.    for ( lIdx = 0; lIdx < lDirCount; lIdx++ ) {
  133.       if ( -1 != (lOtherIdx=bsearch(pDirList[lIdx],pOtherDirList,"DirectorySortFunction")) ) {
  134.          if ( 0 < difftime(pDirList[lIdx].Write,pOtherDirList[lOtherIdx].Write) ) {
  135.             if ( !lDifferenceFound ) {
  136.                lDifferenceFound = True;
  137.                printf("\nNewer in \"%s\" than \"%s\":\n",pDirSpec,pOtherDirSpec);
  138.             }
  139.             printf("%-40s   ",pDirList[lIdx].name);
  140.             PrintDirectoryDate(pDirList[lIdx].Write);
  141.             printf(" : ");
  142.             PrintDirectoryDate(pOtherDirList[lOtherIdx].Write);
  143.             printf("\n");
  144.          }
  145.       }
  146.    }
  147.    if ( lDifferenceFound )
  148.       printf("\n");
  149.    return lDifferenceFound;
  150. }
  151.  
  152. PrintDirectoryDate(pTime)
  153. {
  154.    tm = localtime(pTime);
  155.    printf("%2d-%02d-%02d  %2d:%02d%c",
  156.           tm.tm_mon+1,tm.tm_mday,tm.tm_year%100,
  157.           (tm.tm_hour%12) == 0 ? 12 : (tm.tm_hour%12),tm.tm_min,
  158.           tm.tm_hour < 12 ? 'a' : 'p');
  159. }
  160.  
  161. ShowSizeDifferences(pDirSpec,pDirList,pOtherDirSpec,pOtherDirList)
  162. {
  163.    lDifferenceFound = False;
  164.    lDirCount = 1 + GetArraySpan(pDirList);
  165.  
  166.    for ( lIdx = 0; lIdx < lDirCount; lIdx++ ) {
  167.       if ( -1 != (lOtherIdx=bsearch(pDirList[lIdx],pOtherDirList,"DirectorySortFunction")) ) {
  168.          if ( pDirList[lIdx].size != pOtherDirList[lOtherIdx].size ) {
  169.             if ( !lDifferenceFound ) {
  170.                lDifferenceFound = True;
  171.                printf("\nSize differences \"%s\" : \"%s\":\n",pDirSpec,pOtherDirSpec);
  172.             }
  173.             printf("%s  %d : %d\n",pDirList[lIdx].name,
  174.                    pDirList[lIdx].size,pOtherDirList[lOtherIdx].size);
  175.          }
  176.       }
  177.    }
  178.    if ( lDifferenceFound )
  179.       printf("\n");
  180.    return lDifferenceFound;
  181. }
  182.  
  183.  
  184. Instructions()
  185. {
  186.    printf("\a\n")
  187.    printf("DirDiff.cmm - Show differences in directory listings\n")
  188.    printf("\n")
  189.    printf("USAGE: CEnvi DirDiff.cmm <Dir1> <Dir2>\n")
  190.    printf("\n")
  191.    printf("WHERE: Dir1, Dir2 - Specifications for two directory listings.\n")
  192.    printf("\n")
  193.    printf("NOTE: For current directory use \".\" or \".\*.*\"\n")
  194.    printf("\n")
  195.    printf("EXAMPLES: CEnvi DirDiff c:\Mine d:\Theirs\n")
  196.    printf("          CEnvi DirDiff.cmm . d:..\*.*\n")
  197.    printf("\n")
  198.    DirDiffExit(EXIT_FAILURE);
  199. }
  200.