
/*
 * CMP.C
 *
 *
 * (C)Copyright 1986, Matthew Dillon, All Rights Reserved.
 * Permission is granted to distribute for non-profit only.
 *
 *    comp file1 file2
 *
 *    Compare two files.  The program will tell you if two files compare
 *    the same or, if not, where the error occured.
 *
 */

#include <stdio.h>
#include <fcntl.h>

extern char *malloc();

#define BUFSIZE   16384

main(ac, av)
char *av[];
{
    long f1, f2;
    register short i, j, n;
    char fail;
    char *buf1, *buf2;
    char *premature_eof = "premature EOF in %s (files compare to that point)\n";

    buf1 = malloc(BUFSIZE);
    buf2 = malloc(BUFSIZE);
    if (!buf1 || !buf2) {
	puts("no memory");
	exit(30);
    }
    fail = 0;
    if (ac <= 2) {
	puts ("V2.00 (c)Copyright 1986-1988 Matthew Dillon, All Rights Reserved");
	puts ("cmp file1 file2");
	exit(0);
    }
    f1 = open(av[1], O_RDONLY);
    f2 = open(av[2], O_RDONLY);
    if (f1 && f2) {
	while (!fail && (i = read(f1, buf1, 256))) {
	    n = read(f2, buf2, i);
	    if (!bcmp(buf1, buf2, n))
		fail = 5;
	    if (!fail) {
		if (n == i)
		    continue;
		fail = 5;
	    }
	}
	if (!fail && read(f2, buf2, 1))
	    fail = 5;
    } else {
	puts("Could not open both files");
	fail = 20;
    }
    if (f1 >= 0)
	close(f1);       /* f1 & f2 are either valid or NULL */
    if (f2 >= 0
	close(f2);
    if (fail)
	puts("Compare failed");
    exit(fail);
}


