home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / DIRDIFF.CMM < prev    next >
Text File  |  1996-01-18  |  8KB  |  276 lines

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