program compare;

{ compares two files on byte-by-byte basis
  and displays non-matching bytes
  23.02.86 }

type hex2 = string[2];
     hex4 = string[4];

const reclg:  integer=128;

var ch: char;
    name1,name2: string[14];
    datei1,datei2: file;
    fsize1,fsize2: integer;
    buff1,buff2: array [1..128] of byte;
    b1,b2: byte;
    offset: integer;
    i,ii,iii: integer;
    j,k: integer;
    pflag: boolean;

function Hexof(b: byte): hex2;
var j,m: integer;
    buff: string[2];
begin {Hexof}
for j:=1 to 2 do
    begin
    m:=b mod 16;
    if m<10 then
        insert(Chr($30+m),buff,1)
    else
        insert(Chr(55+m),buff,1);
    b:=b div 16;
    end;
Hexof:=buff;
end; {Hexof}

function Hexadr(i: integer): hex4;
var j,m: integer;
    buff: string[4];
begin {Hexadr}
for j:=1 to 4 do
    begin
    m:=i mod 16;
    if m<10 then
        insert(Chr($30+m),buff,1)
    else
        insert(Chr(55+m),buff,1);
    i:=i div 16;
    end;
Hexadr:=buff;
end; {Hexadr}

begin { cf }

repeat
writeln; write('Filename #1 '); readln(name1);
for i:=1 to length(name1) do
    name1[i]:=UpCase(name1[i]);
assign(datei1,name1);
{$I-} reset(datei1) {$I+};
if Ioresult<>0 then writeln('File ',name1,' not found.')
else
    begin
    write('Filename #2 '); readln(name2);
    for i:=1 to length(name2) do
        name2[i]:=UpCase(name2[i]);
    assign(datei2,name2);
    {$I-} reset(datei2) {$I+};
    if Ioresult<>0 then writeln('File ',name2,' not found.')
    else
        begin
        fsize1:=FileSize(datei1);
        fsize2:=FileSize(datei2);
        writeln('File #1 ',name1:12,' has ',fsize1:4,' records');
        writeln('File #2 ',name2:12,' has ',fsize2:4,' records');
        writeln;
        if (fsize1=0) or (fsize2=0) then
            writeln('No comparison made')
        else
            begin
            write('Offset for Begin of File ($nnnn for nnnn Hex) ');
            readln(offset);
            write('Printer output ? (Y/N) ');
            repeat
                read(kbd,ch); writeln(ch);
            until ch in ['Y','y','N','n'];
            if ch in ['Y','y'] then pflag:=true
            else pflag:=false;
            writeln; writeln('Addr #1 #2');
            if pflag then
                begin
                writeln(LST,'File #1 ',name1:12,' has ',fsize1:4,' records');
                writeln(LST,'File #2 ',name2:12,' has ',fsize2:4,' records');
                writeln(LST); writeln(LST,'Addr #1 #2');
                end;
            k:=0;       { counts nonmatching bytes }
            i:=offset;  { address of current byte }
            ii:=-2;     { address of first nonmatching byte }
            iii:=-2;    { address of last nonmatching byte }
            repeat
                blockread(datei1,buff1,1);
                blockread(datei2,buff2,1);
                for j:=1 to reclg do
                    begin
                    if buff1[j]<>buff2[j] then
                        begin
                        k:=k+1;
                        if i=iii+1 then iii:=i
                        else
                            begin
                            b1:=buff1[j]; b2:=buff2[j];
                            if ii>=0 then
                                begin
                                if iii=ii then
                                    begin
                                    writeln;
                                    if pflag then writeln(LST);
                                    end;
                                if iii>ii+1 then
                                    begin
                                    write('   ....     ');
                                    if pflag then write(LST,'   ....     ');
                                    end;
                                if iii>=ii+1 then
                                    begin
                                    writeln(Hexadr(iii):4,Hexof(b1):3,Hexof(b2):3);
                                    if pflag then
                                        writeln(LST,Hexadr(iii):4,Hexof(b1):3,Hexof(b2):3);
                                    end;
                                end;
                            ii:=i; iii:=i;
                            write(Hexadr(i):4,Hexof(b1):3,Hexof(b2):3,'  ');
                            if pflag then
                                write(LST,Hexadr(i):4,Hexof(b1):3,Hexof(b2):3,'  ');
                            end;
                        end;
                    i:=i+1;
                    end;
            until  EOF(datei1) or EOF(datei2);
            end;
        close(datei1);
        close(datei2);
        writeln; writeln;
        writeln(k,' non-matching bytes');
        if pflag then
            begin
            writeln(LST); writeln(LST);
            writeln(LST,k,' non-matching bytes');
            end;
        end;
    end;
writeln; writeln; write('Restart ? (Y/N) ');
repeat
    read(kbd,ch); writeln(ch);
until ch in ['Y','y','N','n'];
until ch in ['N','n'];

end. { cf }


