2023-06-11 00:54:04 +03:00
|
|
|
#include <errno.h>
|
2023-06-05 14:36:17 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-06-11 00:54:04 +03:00
|
|
|
int fstat(int fildes, struct stat* buf)
|
|
|
|
{
|
|
|
|
return syscall(SYS_FSTAT, fildes, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fstatat(int fd, const char* __restrict path, struct stat* __restrict buf, int flag)
|
|
|
|
{
|
|
|
|
if (flag & ~AT_SYMLINK_NOFOLLOW)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int target = openat(fd, path, (flag & AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0);
|
|
|
|
if (target == -1)
|
|
|
|
return -1;
|
|
|
|
int ret = fstat(target, buf);
|
|
|
|
close(target);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-06-05 14:36:17 +03:00
|
|
|
int lstat(const char* __restrict path, struct stat* __restrict buf)
|
|
|
|
{
|
2023-06-11 00:54:04 +03:00
|
|
|
int fd = open(path, O_SEARCH | O_NOFOLLOW);
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
int ret = fstat(fd, buf);
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
2023-06-05 14:36:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int stat(const char* __restrict path, struct stat* __restrict buf)
|
|
|
|
{
|
2023-06-11 00:54:04 +03:00
|
|
|
int fd = open(path, O_SEARCH);
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
int ret = fstat(fd, buf);
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
2023-06-05 14:36:17 +03:00
|
|
|
}
|