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
12int main(int argc, char **argv) {
13 char symlink_path[PATH_MAX];
14 snprintf(s: symlink_path, maxlen: sizeof(symlink_path), format: "%s_%d.symlink", argv[0],
15 getpid());
16 remove(filename: symlink_path);
17 int res = symlink(from: argv[0], to: 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: fd);
29
30 return 0;
31}
32

source code of compiler-rt/test/sanitizer_common/TestCases/Darwin/freadlink.c