Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice02 / ex1-2.c
blob: ef98a27e3e8d99720b91cf997aa658deadd8a48f (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
32
#include <fcntl.h> // open, close, O_RDONLY
#include <unistd.h> // read
#include <stdio.h> // printf, perror

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

	int f2 = dup(f1);
	if (f2 < 0) {
		perror("dup");
		return 2;
	}

	int readTimes = 10; // няколко пъти
	char buf1, buf2;
	while (readTimes > 0 && read(f1, &buf1, 1) && read(f2, &buf2, 1)) {
		printf("%c%c", buf1, buf2);
		readTimes--;
	}

	close(f1);

	while (read(f2, &buf2, 1)) {
		printf("%c", buf2);
	}

	close(f2);
}