| 1 |
|
| 2 |
// RUN: %clang -O0 %s -o %t && %run %t |
| 3 |
|
| 4 |
#include <assert.h> |
| 5 |
#include <fcntl.h> |
| 6 |
#include <limits.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <string.h> |
| 9 |
#include <sys/types.h> |
| 10 |
#include <unistd.h> |
| 11 |
|
| 12 |
int main(int argc, char **argv) { |
| 13 |
char symlink_path[PATH_MAX]; |
| 14 |
snprintf(symlink_path, sizeof(symlink_path), "%s_%d.symlink", argv[0], |
| 15 |
getpid()); |
| 16 |
remove(symlink_path); |
| 17 |
int res = symlink(argv[0], symlink_path); |
| 18 |
assert(!res); |
| 19 |
|
| 20 |
int fd; |
| 21 |
char readlink_path[PATH_MAX]; |
| 22 |
fd = open(symlink_path, O_RDONLY | O_SYMLINK); |
| 23 |
assert(fd > 0); |
| 24 |
ssize_t res2 = freadlink(fd, readlink_path, sizeof(readlink_path)); |
| 25 |
assert(res2 >= 0); |
| 26 |
readlink_path[res2] = '\0'; |
| 27 |
assert(!strcmp(readlink_path, argv[0])); |
| 28 |
close(fd); |
| 29 |
|
| 30 |
return 0; |
| 31 |
} |