home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 266.lha / BinComp / Bcom.c < prev    next >
C/C++ Source or Header  |  1989-07-10  |  4KB  |  171 lines

  1. /*
  2.  *  bcom - binary compare utility.
  3.  *
  4.  *  usage: bcom (-option -option ... )  file1 file2
  5.  *
  6.  *  !! no options installed yet !!
  7.  *
  8.  *  Joe Majors - 11 Jan 88
  9.  *
  10.  *  Purpose:  Compares two binary files.  Prints locations when
  11.  *  files differ and when files resume.  Location specification
  12.  *  is sector/byte format that is compatible with 'NewZap' program.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <fcntl.h>
  17.  
  18. /* Definitions to make life easier  */
  19.  
  20. #define TRUE 1
  21. #define FALSE 0
  22. #define abort exit(20)
  23.  
  24. #define MAX(a,b) ((a)>(b)?(a):(b))
  25. #define MIN(a,b) ((a)<(b)?(a):(b))
  26. #define ABS(x) ((x<0)?(-(x)):(x))
  27.  
  28. #define USAGE "Usage: bcom file1 file2"
  29. #define BUFSIZE 512
  30.  
  31. char *malloc();  /* too avoid compiler warning */
  32.  
  33.  
  34. /*  Declare file associated stuff */
  35.  
  36. char *name1, *name2; /* pointers to file names */
  37.  
  38. int   fd1,    fd2;   /* file descriptors (unit numbers) for files */
  39.  
  40. char *buf1,  *buf2;  /* pointers to dynamically allocated buffers */
  41.  
  42. /*
  43.  *  Open a file with needed attributes in needed mode. Handle errors
  44.  */
  45.  
  46. int myopen( filename ) /* returns file descriptor: int */
  47.   char *filename; 
  48. {
  49.   int fd;
  50.  
  51.   if( (fd=open(filename,O_RDONLY)) < 0 ) {
  52.     fprintf(stderr,"Can't Open %s\n",filename);
  53.     abort;
  54.   }
  55.   return(fd);
  56. }   
  57.  
  58. /*
  59.  *  Function: hex - converts to formatted uppercase string 
  60.  */
  61.  
  62. char *hex( i )
  63.   short i;
  64.   {
  65.   static char str[] = "000";
  66.  
  67.   sprintf(str,"%03x",i);
  68.   for(i=0;i<3;i++){
  69.       if(str[i]<103 && str[i]>96) str[i] &= 0x5f;
  70.   }
  71.   return( (char *)&str);
  72. }
  73.  
  74. /*
  75.  *  Begin Main Program
  76.  */
  77.  
  78. main(argc,argv)
  79.   int argc;
  80.   char *argv[];
  81. {
  82.   if(argc<3) {
  83.     fprintf(stderr,"%s\n",USAGE); exit();
  84.   }
  85.  
  86.   name1 = argv[1]; fd1 = myopen( name1 );
  87.   name2 = argv[2]; fd2 = myopen( name2 );
  88.  
  89.   buf1 = malloc(BUFSIZE);
  90.   buf2 = malloc(BUFSIZE);
  91.  
  92.   if( buf1==FALSE || buf2==FALSE ) {
  93.     fprintf(stderr,"Can't Allocate Memory\n");
  94.     abort;
  95.   }
  96.  
  97.   printf("Comparison of  %s  vs  %s\n\n",name1,name2);
  98.  
  99.   /*  Main Processing Loop */
  100.   {
  101.     short length_diff = 0;
  102.     short length;
  103.     short len1, len2;
  104.     short byte;
  105.     int sector = 0;
  106.     int numdifs = 0;
  107.     unsigned char last_equal = TRUE;
  108.  
  109.     while( length_diff == 0 ) {
  110.  
  111.       len1 = read(fd1,buf1,BUFSIZE);  /* Get Data from Files */
  112.       len2 = read(fd2,buf2,BUFSIZE);
  113.  
  114.       length_diff = len1 - len2;     /* length_diff < 0  file1 ends */
  115.       length = MIN(len1,len2);       /* length_diff > 0  file2 ends */
  116.       if(length==0) break;           /* one or more files ended */ 
  117.  
  118.       sector++;
  119.  
  120.       /*  Compare all bytes within this buffer (sector) */
  121.  
  122.       for( byte=0; byte<length; byte++) {
  123.         if( buf1[byte] == buf2[byte] ) {
  124.           if(!last_equal) {
  125.             printf("Resume at %4d %s\n",sector,hex(byte));
  126.             last_equal = TRUE;
  127.           }
  128.         } else {     /* bytes differ */
  129.           if(last_equal) {
  130.             printf("Differ at %4d %s ... ",sector,hex(byte));
  131.             last_equal = FALSE;
  132.           }
  133.           numdifs++;
  134.         }
  135.       }
  136.  
  137.       if( length < BUFSIZE) break;  /* Didn't get full sector */
  138.  
  139.     }
  140.  
  141.     /* Determine and printout Final messages */
  142.  
  143.     if(length_diff == 0) {                          /* files same length */
  144.       if(numdifs == 0) printf("Files Equivalent\n\n");
  145.     } else {                                     /* files differ in length */
  146.       char *fname;
  147.  
  148.       fname = (length_diff < 0 ) ? name1 : name2 ;
  149.       if(!last_equal) printf("\n");
  150.       printf("\nFile:  %s  ends first at %4d %s\n",fname,sector,hex(--byte));
  151.     }
  152.     if(numdifs>0) printf("\nTotal Differing Bytes: %d\n\n",numdifs);
  153.  
  154.   }
  155.   /*  End of Main Processing Loop  */
  156.  
  157.   /*  Close up and de-allocate */
  158.  
  159.   free( buf1 ); close( fd1 );
  160.   free( buf2 ); close( fd2 );
  161. }
  162.  
  163. /*
  164.  *  Null function - reduces code size by 212 bytes.
  165.  *                  Can't run from WB now.
  166.  */
  167.  
  168. _wb_parse()
  169. {
  170. }
  171.