1570af302Sopenharmony_ci#define _GNU_SOURCE
2570af302Sopenharmony_ci#define USE_STATX
3570af302Sopenharmony_ci#include <sys/stat.h>
4570af302Sopenharmony_ci#include <string.h>
5570af302Sopenharmony_ci#include <syscall.h>
6570af302Sopenharmony_ci#include <sys/sysmacros.h>
7570af302Sopenharmony_ci#include <errno.h>
8570af302Sopenharmony_ci
9570af302Sopenharmony_ciint statx(int dirfd, const char *restrict path, int flags, unsigned mask, struct statx *restrict stx)
10570af302Sopenharmony_ci{
11570af302Sopenharmony_ci	int ret = __syscall(SYS_statx, dirfd, path, flags, mask, stx);
12570af302Sopenharmony_ci
13570af302Sopenharmony_ci#ifndef SYS_fstatat
14570af302Sopenharmony_ci	return __syscall_ret(ret);
15570af302Sopenharmony_ci#endif
16570af302Sopenharmony_ci
17570af302Sopenharmony_ci	if (ret != -ENOSYS) return __syscall_ret(ret);
18570af302Sopenharmony_ci
19570af302Sopenharmony_ci	struct stat st;
20570af302Sopenharmony_ci	ret = fstatat(dirfd, path, &st, flags);
21570af302Sopenharmony_ci	if (ret) return ret;
22570af302Sopenharmony_ci
23570af302Sopenharmony_ci	stx->stx_dev_major = major(st.st_dev);
24570af302Sopenharmony_ci	stx->stx_dev_minor = minor(st.st_dev);
25570af302Sopenharmony_ci	stx->stx_ino = st.st_ino;
26570af302Sopenharmony_ci	stx->stx_mode = st.st_mode;
27570af302Sopenharmony_ci	stx->stx_nlink = st.st_nlink;
28570af302Sopenharmony_ci	stx->stx_uid = st.st_uid;
29570af302Sopenharmony_ci	stx->stx_gid = st.st_gid;
30570af302Sopenharmony_ci	stx->stx_size = st.st_size;
31570af302Sopenharmony_ci	stx->stx_blksize = st.st_blksize;
32570af302Sopenharmony_ci	stx->stx_blocks = st.st_blocks;
33570af302Sopenharmony_ci	stx->stx_atime.tv_sec = st.st_atim.tv_sec;
34570af302Sopenharmony_ci	stx->stx_atime.tv_nsec = st.st_atim.tv_nsec;
35570af302Sopenharmony_ci	stx->stx_mtime.tv_sec = st.st_mtim.tv_sec;
36570af302Sopenharmony_ci	stx->stx_mtime.tv_nsec = st.st_mtim.tv_nsec;
37570af302Sopenharmony_ci	stx->stx_ctime.tv_sec = st.st_ctim.tv_sec;
38570af302Sopenharmony_ci	stx->stx_ctime.tv_nsec = st.st_ctim.tv_nsec;
39570af302Sopenharmony_ci	stx->stx_btime = (struct statx_timestamp){.tv_sec=0, .tv_nsec=0};
40570af302Sopenharmony_ci	stx->stx_mask = STATX_BASIC_STATS;
41570af302Sopenharmony_ci
42570af302Sopenharmony_ci	return 0;
43570af302Sopenharmony_ci}
44