-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfd-1.c
More file actions
38 lines (31 loc) · 756 Bytes
/
Copy pathfd-1.c
File metadata and controls
38 lines (31 loc) · 756 Bytes
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
33
34
35
36
37
38
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
int main() {
char c1, c2, c3;
char *fname = "file.txt";
int fd1 = open(fname, O_RDONLY);
int fd2 = open(fname, O_RDONLY);
int fd3 = open(fname, O_RDONLY);
if (fd1 < 0 || fd2 < 0 || fd3 < 0) {
perror("open");
exit(1);
}
// Make fd3 refer to the same open file description as fd2
if (dup2(fd2, fd3) < 0) {
perror("dup2");
exit(1);
}
if (read(fd1, &c1, 1) != 1 ||
read(fd2, &c2, 1) != 1 ||
read(fd3, &c3, 1) != 1) {
perror("read");
exit(1);
}
printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3);
close(fd1);
close(fd2);
close(fd3);
return 0;
}