home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga Shareware Floppies / ma30.dms / ma30.adf / Difference / difference.c < prev    next >
C/C++ Source or Header  |  1994-09-07  |  2KB  |  98 lines

  1.    /************************************************/
  2.   /* Difference v1.1 (27.08.94) - a file comparer */
  3.  /*        Public Domain   (c) 1994 by BSZ       */
  4. /************************************************/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define max(x,y) ((x)>(y) ? (x):(y))
  11. #define min(x,y) ((x)>(y) ? (y):(x))
  12.  
  13. /* modyfikacja funkcji standardowej strncmp tak, aby zwracaîa
  14. offset (rzeczywisty, nie numer elementu tablicy liczony przecieû
  15. od zera), na którym wystëpuje róznica */
  16. int strn_cmp(char *string1, char *string2, int size)
  17. {
  18.     int position;
  19.     for (position=0; position<size; position++)
  20.     {
  21.         if (string1[position]!=string2[position])
  22.         {    
  23.             return (position+1);
  24.         }
  25.     }
  26.     return 0;
  27. }
  28.  
  29. void clean(char *buffer, int size)
  30. {
  31.     int loop;
  32.     for(loop=0; loop<size; loop++)
  33.         buffer[loop]=0;
  34. }
  35.  
  36. main(int argc, char *argv[])
  37. {
  38.     long offset, size1, size2, size, c, d;
  39.     int cmp;
  40.     char io_buffer1[1024], io_buffer2[1024];
  41.     FILE *in1, *in2;
  42.     char *path_1[30], *path_2[30];
  43.  
  44.     *path_1=argv[1];
  45.     *path_2=argv[2];
  46.  
  47.     if(argc!=3)
  48.     {
  49.         printf("\nDifference v1.1 (27.08.94) - a file comparer\n");
  50.         printf("Public Domain (c) 1994 by BSZ\n");
  51.         printf("Usage: %s <file 1> <file 2>\n", argv[0]);
  52.         exit(0);
  53.     }
  54.  
  55.     if( !(in1=fopen(*path_1, "rb")))
  56.     {
  57.         fprintf(stderr,">>>Error opening %s\n", *path_1);
  58.         exit(1);
  59.     }
  60.     else
  61.         fseek(in1,0L,SEEK_END);
  62.  
  63.     if( !(in2=fopen(*path_2, "rb")))
  64.     {
  65.         fprintf(stderr, ">>>Error opening %s\n", *path_2);
  66.         exit(1);
  67.     }
  68.     else
  69.         fseek(in2,0L,SEEK_END);
  70.  
  71.     size1=ftell(in1);
  72.     size2=ftell(in2);
  73.     size=min(size1, size2);
  74.     rewind(in1);
  75.     rewind(in2);
  76.  
  77.     for(offset=1; offset<=size ; offset+=1024)
  78.     {
  79.         printf("%02d%c completed",100*offset/size,'%');
  80.         fflush(stdout);
  81.         clean(io_buffer1,1024);
  82.         clean(io_buffer2,1024);
  83.         c=fread(io_buffer1, 1, 1024, in1);
  84.         d=fread(io_buffer2, 1, 1024, in2);
  85.         cmp=strn_cmp(io_buffer1, io_buffer2, 1024);
  86.         printf("\b\b\b\b\b\b\b\b\b\b\b\b\b");
  87.         if (cmp)
  88.         {
  89.             offset+=cmp-1;
  90.             printf("100%c completed. Files differ at position %d ($%x) \n",'%', offset,offset);
  91.             exit(0);
  92.         }
  93.     }
  94.     printf ("100%c completed. Files DO NOT differ\n",'%');
  95.     fclose(in1);
  96.     fclose(in2);
  97. }
  98.