Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice02 / ex2.c
blob: 6a8c420b6acc77cb7a8bdd3f148063fe1bd690e6 (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 <fcntl.h> // open, close, O_WRONLY, ...
#include <unistd.h> // dup
#include <stdio.h> // printf

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

	close(1);

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

	printf("Hello World!\n");

	close(f1);
	close(f2);
}