forked from dohyunk58/OS-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.c
More file actions
38 lines (31 loc) · 890 Bytes
/
diff.c
File metadata and controls
38 lines (31 loc) · 890 Bytes
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
#include "diff.h"
#include <stdio.h>
#include <string.h>
void diff(const char *file1, const char *file2) {
FILE *fp1 = fopen(file1, "r");
FILE *fp2 = fopen(file2, "r");
if (!fp1 || !fp2) {
printf("diff: cannot open file(s)\n");
if (fp1) fclose(fp1);
if (fp2) fclose(fp2);
return;
}
char buf1[1024], buf2[1024];
int line = 1;
int difference = 0;
while (fgets(buf1, sizeof(buf1), fp1) && fgets(buf2, sizeof(buf2), fp2)) {
if (strcmp(buf1, buf2) != 0) {
printf("Line %d differs\n", line);
difference = 1;
}
line++;
}
if (fgets(buf1, sizeof(buf1), fp1) || fgets(buf2, sizeof(buf2), fp2)) {
printf("Files have different lengths\n");
difference = 1;
}
if (!difference)
printf("Files are identical\n");
fclose(fp1);
fclose(fp2);
}