1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-15 05:54:59 +00:00
serenity/Base/usr/share/man/man2/readlink.md
Shannon Booth e2e7c4d574 Everywhere: Use to_number<T> instead of to_{int,uint,float,double}
In a bunch of cases, this actually ends up simplifying the code as
to_number will handle something such as:

```
Optional<I> opt;
if constexpr (IsSigned<I>)
    opt = view.to_int<I>();
else
    opt = view.to_uint<I>();
```

For us.

The main goal here however is to have a single generic number conversion
API between all of the String classes.
2023-12-23 20:41:07 +01:00

67 lines
1.8 KiB
Markdown

## Name
readlink - get symlink target
## Synopsis
```**c++
#include <unistd.h>
ssize_t readlink(const char* path, char* buffer, size_t size)
```
## Description
`readlink()` writes up to `size` bytes of the target path of a symbolic link at
the specified `path` to the given `buffer`. `readlink()` does not
null-terminate the buffer. If the target of the link is longer than `size`
bytes, it will get truncated.
## Return value
On success, `readlink()` returns the number of bytes written to the buffer,
which is always less or equal to the specified `size`. Otherwise, it returns -1
and sets `errno` to describe the error.
## Notes
The underlying system call always returns the full size of the target path on
success, not the number of bytes written. `FileSystem::read_link()` makes use
of this to provide an alternative way to read links that doesn't require the
caller to pick a buffer size and allocate a buffer straight up.
Since it's essentially impossible to guess the right buffer size for reading
links, it's strongly recommended that everything uses `FileSystem::read_link()`
instead.
## Examples
The following example demonstrates how one could implement an alternative
version of `getpid(2)` which reads the calling process ID from ProcFS:
```c++
#include <LibFileSystem/FileSystem.h>
#include <unistd.h>
pid_t read_pid_using_readlink()
{
char buffer[64];
int rc = readlink("/proc/self", buffer, sizeof(buffer) - 1);
if (rc < 0)
return rc;
buffer[rc] = 0;
return atoi(buffer);
}
ErrorOr<pid_t> read_pid_using_core_file()
{
auto target = TRY(FileSystem::read_link("/proc/self"sv));
auto pid = target.to_number<pid_t>();
VERIFY(pid.has_value());
return pid.value();
}
```
## See also
* [`readlink`(1)](help://man/1/readlink)