Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice01 / ex3-cmp.c
blob: 19bc33646c48d766670e8206240128ba1a866bfe (plain) (blame)
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
#include <unistd.h> // ssize_t, read, write
#include <fcntl.h> // open, close, O_RDONLY, ...
#include <stdio.h> // perror

// exit 0 - equal
// exit 1 - different
int main(int argc, char* argv[]) {
	int f1 = open(argv[1], O_RDONLY);
	if (f1 < 0) {
		perror("open");
		return 2;
	}

	int f2 = open(argv[2], O_RDONLY);
	if (f2 < 0) {
		perror("open");
		return 3;
	}

	char buf1, buf2;
	ssize_t read1, read2;
	do {
		read1 = read(f1, &buf1, 1);
		read2 = read(f2, &buf2, 1);
		if (buf1 != buf2) return 1;
	} while (read1 > 0 && read2 > 0);
	if (read1 != read2) return 1;

	close(f1);
	close(f2);
}