forked from chocolatiers/doom-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmpfile.c
75 lines (62 loc) · 1.21 KB
/
cmpfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
int main(int c, char **v)
{
FILE *f1, *f2;
int len1, len2;
char *file1, *file2, *end1;
int rc;
if (c != 3)
{
fprintf(stderr, "Usage: cmpfile <file1> <file2>\n");
exit(-1);
}
f1 = fopen(v[1], "r");
f2 = fopen(v[2], "r");
if (!f1)
{
fprintf(stderr, "Coule not open [%s]\n", v[1]);
exit(-1);
}
if (!f2)
{
fprintf(stderr, "Coule not open [%s]\n", v[2]);
exit(-1);
}
fseek(f1, 0, SEEK_END);
len1 = ftell(f1);
fseek(f1, 0, SEEK_SET);
fseek(f2, 0, SEEK_END);
len2 = ftell(f2);
fseek(f2, 0, SEEK_SET);
if (len1 != len2)
{
fprintf(stderr, "[%s] != [%s]\n", v[1], v[2]);
exit(0);
}
file1 = (char *) malloc(len1);
file2 = (char *) malloc(len2);
rc = fread(file1, len1, 1, f1);
if (rc != 1)
{
fprintf(stderr, "Could not read [%s]\n", v[1]);
exit(-1);
}
rc = fread(file2, len2, 1, f2);
if (rc != 1)
{
fprintf(stderr, "Could not read [%s]\n", v[2]);
exit(-1);
}
end1 = file1 + len1;
while (file1 != end1)
{
if (*file1 == 13) *file1 = ' ';
if (*file2 == 13) *file2 = ' ';
if (*file1++ != *file2++)
{
fprintf(stderr, "[%s] != [%s]\n", v[1], v[2]);
exit(0);
}
}
return 0;
}