Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Operating Systems / Pract11 / cp.c
blob: 044b2781dc3267ceeccedf12b3409bd912583613 (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
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

const size_t BUF_SIZE = 1024;

int main(int argc, char *argv[]) {
	if (argc < 3) return 1;

	int fd_from = open(argv[1], O_RDONLY);
	if (fd_from < 0) return 2;

	int fd_to = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0744);
	if (fd_to < 0) return 3;

	char buf[BUF_SIZE];
	size_t actual = read(fd_from, buf, BUF_SIZE);
	while (actual > 0) {
		write(fd_to, buf, actual);
		actual = read(fd_from, buf, BUF_SIZE);
	}
	return 0;
}