mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
util_libc: Add util_readlink() and util_readlinkat() helpers
Introduce util_readlinkat() to read symbolic links relative to a directory file descriptor, and util_readlink() as a convenience wrapper using AT_FDCWD. util_readlink() delegates to util_readlinkat() instead of duplicating logic, ensuring a single implementation for both interfaces. Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com> Signed-off-by: Jan Polensky <japo@linux.ibm.com> Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
committed by
Jan Höppner
parent
789b097d3a
commit
365be71dfc
@@ -10,11 +10,15 @@
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "lib/util_base.h"
|
||||
#include "lib/util_libc.h"
|
||||
@@ -295,3 +299,31 @@ size_t util_strlcpy(char *dest, const char *src, size_t size)
|
||||
|
||||
return str_len;
|
||||
}
|
||||
|
||||
char *__util_readlinkat(const char *func, const char *file, int line, int dirfd, const char *path)
|
||||
{
|
||||
ssize_t link_len = PATH_MAX;
|
||||
struct stat st;
|
||||
char *linkdir;
|
||||
ssize_t len;
|
||||
|
||||
if (fstatat(dirfd, path, &st, AT_SYMLINK_NOFOLLOW) == 0 && st.st_size > 0)
|
||||
link_len = st.st_size + 1;
|
||||
|
||||
linkdir = __util_malloc(func, file, line, link_len);
|
||||
|
||||
len = readlinkat(dirfd, path, linkdir, link_len);
|
||||
if (len == -1) {
|
||||
free(linkdir);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (len >= link_len) {
|
||||
warnx("%s: Link target too long", path);
|
||||
free(linkdir);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
linkdir[len] = '\0';
|
||||
return __util_realloc(func, file, line, linkdir, (size_t)len + 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user