Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1a |
both |
#define _POSIX1_SOURCE 2
#include <unistd.h>
int readlink(const char *path,
char *buf, size_t bufsiz);
#define _POSIX_C_SOURCE 200112L
#include <unistd.h>
ssize_t readlink(const char *__restrict__path,
char *__restrict__buf, size_t bufsiz);
Places the contents of the symbolic link path in the buffer buf. The size of the buffer is set by bufsiz. The result stored in buf does not include a terminating NULL character.
If the buffer is too small to contain the value of the symbolic link, that value is truncated to the size of the buffer (bufsiz). If the value returned is the size of the buffer, use lstat() to determine the actual size of the symbolic link.
If successful, when bufsiz is greater than 0, readlink() returns the number of bytes placed in the buffer. When bufsiz is 0 and readlink() completes successfully, it returns the number of bytes contained in the symbolic link and the buffer is not changed.
If the returned value is equal to bufsiz, you can determine the contents of the symbolic link with either lstat() or readlink(), with a 0 value for bufsiz.
⁄* CELEBR05 *⁄
#define _POSIX_SOURCE 1
#include <fcntl.h>
#include <sys⁄stat.h>
#include <sys⁄types.h>
#include <unistd.h>
#undef _POSIX_SOURCE
main() {
char fn[]="readlink.file";
char sl[]="readlink.symlink";
char buf[30];
int fd;
if ((fd = creat(fn, S_IWUSR)) < 0)
perror("creat() error");
else {
close(fd);
if (symlink(fn, sl) != 0)
perror("symlink() error");
else {
if (readlink(sl, buf, sizeof(buf)) < 0)
perror("readlink() error");
else printf("readlink() returned '%s' for '%s'\n", buf, sl);
unlink(sl);
}
unlink(fn);
}
}
readlink() returned 'readlink.file' for 'readlink.symlink'