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

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

	int f2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0755);
	if (f2 < 0) {
		perror("open");
		return 2;
	}

	char buf;
	while(read(f1, &buf, 1)) {
		write(f2, &buf, 1);
	}

	close(f1);
	close(f2);
}