11 #include <ulz/stdio-repl.h>
14 static int fetch(FILE *f
, off_t
*cnt
) {
16 if(c
>= 0) *cnt
= *cnt
+ 1;
20 static int diff(FILE *f1
, FILE* f2
) {
24 if(fstat(fileno(f1
), &st1
)) return 1;
25 if(fstat(fileno(f2
), &st2
)) return 1;
27 off_t p1
, p2
, l
, diffbytes
= 0;
28 if (st2
.st_size
!= min
) {
29 printf("sizes differ! %llu, %llu\n",
30 (long long) st1
.st_size
, (long long) st2
.st_size
);
31 if (st2
.st_size
< min
) min
= st2
.st_size
;
37 while(p1
< min
&& fetch(f1
, &p1
) != fetch(f2
, &p2
)) l
++;
39 printf("difference at offset %llu of size %llu\n",
40 (long long) p1
-l
-1, (long long) l
);
45 printf("%zu differences in %llu out of %llu bytes detected.\n",
46 diffs
, (long long) diffbytes
, (long long) min
);
51 static void syntax(void) {
52 printf("syntax: filename1 filename2\nlist differences in binaries\n");
56 static int canread(char* fn
) {
57 return access(fn
, R_OK
) != -1;
60 int main(int argc
, char** argv
) {
61 if(argc
< 3) syntax();
62 if(!canread(argv
[1]) || !canread(argv
[2])) {
63 perror("can not read");
66 return diff(fopen(argv
[1], "r"), fopen(argv
[2], "r"));