cmp.pl 783 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/local/bin/perl
  2. ($#ARGV == 1) || die "usage: cmp.pl <file1> <file2>\n";
  3. open(IN0,"<$ARGV[0]") || die "unable to open $ARGV[0]\n";
  4. open(IN1,"<$ARGV[1]") || die "unable to open $ARGV[1]\n";
  5. binmode IN0;
  6. binmode IN1;
  7. $tot=0;
  8. $ret=1;
  9. for (;;)
  10. {
  11. $n1=sysread(IN0,$b1,4096);
  12. $n2=sysread(IN1,$b2,4096);
  13. last if ($n1 != $n2);
  14. last if ($b1 ne $b2);
  15. last if ($n1 < 0);
  16. if ($n1 == 0)
  17. {
  18. $ret=0;
  19. last;
  20. }
  21. $tot+=$n1;
  22. }
  23. close(IN0);
  24. close(IN1);
  25. if ($ret)
  26. {
  27. printf STDERR "$ARGV[0] and $ARGV[1] are different\n";
  28. @a1=unpack("C*",$b1);
  29. @a2=unpack("C*",$b2);
  30. for ($i=0; $i<=$#a1; $i++)
  31. {
  32. if ($a1[$i] ne $a2[$i])
  33. {
  34. printf "%02X %02X <<\n",$a1[$i],$a2[$i];
  35. last;
  36. }
  37. }
  38. $nm=$tot+$n1;
  39. $tot+=$i+1;
  40. printf STDERR "diff at char $tot of $nm\n";
  41. }
  42. exit($ret);