|
1 | | -#include <stdlib.h> |
2 | 1 | #include <errno.h> |
3 | 2 | #include <fcntl.h> |
4 | 3 | #include <stdio.h> |
5 | 4 | #include <string.h> |
6 | 5 | #include <unistd.h> |
7 | 6 |
|
8 | 7 |
|
9 | | -char* join_path(const char* path1, const char* path2) { |
10 | | - int len1 = strlen(path1); |
11 | | - int len2 = strlen(path2); |
12 | | - char* result = malloc(len1 + len2 + 2); |
13 | | - memcpy(result, path1, len1); |
14 | | - result[len1] = '/'; |
15 | | - memcpy(result + len1 + 1, path2, len2); |
16 | | - return result; |
17 | | -} |
18 | | - |
19 | 8 | int main() { |
20 | | - char template[] = "/tmp/tmpdir.XXXXXX"; |
21 | | - char *tmpdir = mkdtemp(template); |
22 | | - char* p1 = join_path(tmpdir, "test"); |
23 | | - char* p2 = join_path(tmpdir, "test2"); |
| 9 | + char* p1 = "link_source"; |
| 10 | + char* p2 = "link_target"; |
24 | 11 |
|
25 | | - int res = symlink(p2, p1); |
26 | | - printf("link result: %d\n", res); |
27 | | - int src_fd = open(p1, O_CREAT | O_WRONLY, 0777); |
28 | | - printf("source_fd: %d, errno: %d %s\n", src_fd, errno, strerror(errno)); |
29 | | - write(src_fd, "abc", 3); |
30 | | - close(src_fd); |
31 | | - { |
32 | | - int target_fd = open(p2, O_RDONLY); |
33 | | - printf("target_fd: %d, errno: %d %s\n", target_fd, errno, strerror(errno)); |
34 | | - char buf[10]; |
35 | | - read(target_fd, buf, 10); |
36 | | - printf("buf: '%s'\n", buf); |
37 | | - close(target_fd); |
38 | | - } |
39 | | - { |
40 | | - int target_fd = open(p1, O_RDONLY); |
41 | | - printf("target_fd: %d, errno: %d %s\n", target_fd, errno, strerror(errno)); |
42 | | - char buf[10]; |
43 | | - read(target_fd, buf, 10); |
44 | | - printf("buf: '%s'\n", buf); |
45 | | - close(target_fd); |
46 | | - } |
47 | | - free(p1); |
48 | | - free(p2); |
| 12 | + int res = symlink(p2, p1); |
| 13 | + printf("link result: %d\n", res); |
| 14 | + int src_fd = open(p1, O_CREAT | O_WRONLY, 0777); |
| 15 | + printf("source_fd: %d, errno: %d %s\n", src_fd, errno, strerror(errno)); |
| 16 | + write(src_fd, "abc", 3); |
| 17 | + close(src_fd); |
| 18 | + { |
| 19 | + int target_fd = open(p2, O_RDONLY); |
| 20 | + printf("target_fd: %d, errno: %d %s\n", target_fd, errno, strerror(errno)); |
| 21 | + char buf[10]; |
| 22 | + read(target_fd, buf, 10); |
| 23 | + printf("buf: '%s'\n", buf); |
| 24 | + close(target_fd); |
| 25 | + } |
| 26 | + { |
| 27 | + int target_fd = open(p1, O_RDONLY); |
| 28 | + printf("target_fd: %d, errno: %d %s\n", target_fd, errno, strerror(errno)); |
| 29 | + char buf[10]; |
| 30 | + read(target_fd, buf, 10); |
| 31 | + printf("buf: '%s'\n", buf); |
| 32 | + close(target_fd); |
| 33 | + } |
49 | 34 | } |
0 commit comments