| 1 |
#include <stdio.h> |
| 2 |
#include <fcntl.h> |
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
int main() { |
| 6 |
int fd1 = open("1.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| 7 |
if (fd1 < 0) { |
| 8 |
fprintf(stderr, "failed to open 1.txt\n"); |
| 9 |
return 1; |
| 10 |
} |
| 11 |
int fd2 = dup2(fd1, 51); |
| 12 |
if (fd2 < 0) { |
| 13 |
fprintf(stderr, "failed to move <1.txt> to 51\n"); |
| 14 |
return 1; |
| 15 |
} |
| 16 |
close(fd1); |
| 17 |
|
| 18 |
|
| 19 |
dup2(51, 50); |
| 20 |
|
| 21 |
fd1 = open("2.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| 22 |
if (fd1 < 0) { |
| 23 |
fprintf(stderr, "failed to open 2.txt\n"); |
| 24 |
return 1; |
| 25 |
} |
| 26 |
fd2 = dup2(fd1, 50); |
| 27 |
if (fd2 < 0) { |
| 28 |
fprintf(stderr, "failed to move <2.txt> to 50\n"); |
| 29 |
return 1; |
| 30 |
} |
| 31 |
close(fd1); |
| 32 |
|
| 33 |
write(50, "hi\n", 3); |
| 34 |
close(50); |
| 35 |
|
| 36 |
return 0; |
| 37 |
} |