READLINK(2)
π READLINK(2) β System Calls Manual π
okay bestie, pull up a chair, we're talking about readlink, readlinkat, and the whole "read value of a symbolic link" situation. this is page 2 of the System Calls Manual and it is, lowkey, iconic. π¦
β¨ NAME β¨
readlink, readlinkat β read value of a symbolic link
π LIBRARY π
Standard C Library (libc, -lc) β yeah she lives in libc, no cap.
πΈ SYNOPSIS πΈ
#include <unistd.h>
ssize_t
readlink(const char *restrict path, char *restrict buf, size_t bufsiz);
ssize_t
readlinkat(int fd, const char *restrict path, char *restrict buf,
size_t bufsize);
π DESCRIPTION π
so. the readlink() system call takes the contents of the symbolic link path and drops them into the buffer buf, which has size bufsiz. that's it, that's the whole vibe. but β and this is the part people always get wrong β readlink() does not append a NUL character to buf. not even one. so don't be out here treating buf like a string, because it is not one, periodt. π
now for readlinkat(): she's literally equivalent to readlink() except when path is a relative path. in that case, the symbolic link content gets read relative to the directory associated with the file descriptor fd, instead of relative to the current working directory. main character behaviour fr fr. and if you pass the special value AT_FDCWD in the fd parameter, then the current working directory is used and the behaviour is identical to a plain old call to readlink(). same energy, same result. π«Ά
π RETURN VALUES π
if it succeeds, the call returns the count of characters placed in the buffer. if an error happens, you get a -1 and the error code goes into the global variable errno. classic. count on success, -1 on sadness.
π₯ ERRORS π₯
the readlink() system call will fail if:
[ENOTDIR]β a component of the path prefix is not a directory.[ENAMETOOLONG]β a component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters. (numbers matter, bestie, don't round them.)[ENOENT]β the named file does not exist. π[EACCES]β search permission is denied for a component of the path prefix.[ELOOP]β too many symbolic links were encountered in translating the pathname. (the links were linking, bestie.)[EINVAL]β the named file is not a symbolic link.[EIO]β an I/O error occurred while reading from the file system.[EINTEGRITY]β corrupted data was detected while reading from the file system.[EFAULT]β thebufargument extends outside the process's allocated address space.
and on top of all the errors readlink() returns, readlinkat() may also fail if:
[EBADF]β thepathargument does not specify an absolute path and thefdargument is neitherAT_FDCWDnor a valid file descriptor open for searching.[ENOTDIR]β thepathargument is not an absolute path andfdis neitherAT_FDCWDnor a file descriptor associated with a directory.
β οΈ heads up: [ENOTDIR] shows up in both lists. that's not a typo, that's just the lore.
π SEE ALSO π
lstat(2), stat(2), symlink(2), symlink(7)
π STANDARDS π
the readlinkat() system call follows The Open Group Extended API Set 2 specification. she's certified. π
β HISTORY β
the readlink() system call appeared in 4.2BSD. the readlinkat() system call appeared in FreeBSD 8.0. glow-up timeline, honestly. β¨
and that's the whole page, bestie β go read some symlinks responsibly π
this was brought to you by the Debian crew, dated March 30, 2020, on page READLINK(2). mwah. ππ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms