-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcmp.c
70 lines (60 loc) · 1.63 KB
/
lcmp.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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_LINE_LEN 4096
#define fatal(msg, ...) \
do { \
fprintf(stderr, "Fatal error: %s (%d): " \
msg "\n", strerror(errno), errno, ##__VA_ARGS__); \
exit(1); \
} while (0)
static void
usage(const char* exe)
{
fprintf(stderr, "Usage: %s <file1> <file2>\n", exe);
}
int
main(int argc, char** argv)
{
const char* exe;
const char* path1;
const char* path2;
FILE* f1;
FILE* f2;
if (argc != 3) {
usage(argv[0]);
exit(1);
}
exe = argv[0];
path1 = argv[1];
path2 = argv[2];
if (!(f1 = fopen(path1, "r")))
fatal("fopen(%s)", path1);
if (!(f2 = fopen(path2, "r")))
fatal("fopen(%s)", path2);
printf("%s %s %s\n", exe, path1, path2);
printf("--- %s\n", path1);
printf("+++ %s\n", path2);
while (!feof(f1) || !feof(f2)) {
char buf1[MAX_LINE_LEN];
const char* s1 = fgets(buf1, sizeof(buf1), f1);
char buf2[MAX_LINE_LEN];
const char* s2 = fgets(buf2, sizeof(buf2), f2);
if (!s1 && !s2) {
continue;
}
if (!s1) {
printf("+%s", s2);
} else if (!s2) {
printf("-%s", s1);
} else if (strcmp(s1, s2)) {
printf("-%s", s1);
printf("+%s", s2);
}
}
fclose(f1);
fclose(f2);
return 0;
}