Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F144364567
symlink.c: an example of using O_PATH to read symlink
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
kib
Sun, Feb 8, 1:42 AM
2026-02-08 01:42:47 (UTC+0)
Size
1 KB
Referenced Files
None
Subscribers
None
symlink.c: an example of using O_PATH to read symlink
View Options
/* $Id: symlink.c,v 1.7 2026/02/08 01:42:12 kostik Exp kostik $ */
#include <sys/stat.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
static void
onelink(const char *name)
{
struct stat st;
long plen;
ssize_t llen;
int fd;
fd = open(name, O_PATH | O_NOFOLLOW);
if (fd == -1) {
warn("open \"%s\"", name);
return;
}
if (fstatat(fd, "", &st, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW) == -1) {
warn("fstatat \"%s\"", name);
goto ret;
}
if (!S_ISLNK(st.st_mode)) {
warnx("%s: not a symlink", name);
goto ret;
}
#if defined __FreeBSD__
plen = fpathconf(fd, _PC_SYMLINK_MAX);
if (plen == -1) {
warn("fpathconf \"%s\"", name);
goto ret;
}
#elif defined __linux__
/* On Linux, fpathconf() does not work on O_PATH-fd. */
plen = 2048;
#endif
{
char path[plen + 1];
llen = readlinkat(fd, "", path, plen);
if (llen == -1) {
warn("readlinkat \"%s\"", name);
goto ret;
}
path[llen] = '\0';
printf("%s => %s\n", name, path);
}
ret:
close(fd);
}
int
main(int argc, char *argv[])
{
for (int i = 1; i < argc; i++)
onelink(argv[i]);
}
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
28522734
Default Alt Text
symlink.c: an example of using O_PATH to read symlink (1 KB)
Attached To
Mode
P701 symlink.c: an example of using O_PATH to read symlink
Attached
Detach File
Event Timeline
Log In to Comment