Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/nolibc: add more stat() variants

Add fstat(), fstatat() and lstat(). All of them use the existing implementation
based on statx().

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/r/20250428-nolibc-misc-v2-3-3c043eeab06c@linutronix.de
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

authored by

Thomas Weißschuh and committed by
Thomas Weißschuh
2337d39f 7a7cd445

+23 -2
+23 -2
tools/include/nolibc/sys/stat.h
··· 17 17 /* 18 18 * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf); 19 19 * int stat(const char *path, struct stat *buf); 20 + * int fstatat(int fd, const char *path, struct stat *buf, int flag); 21 + * int fstat(int fildes, struct stat *buf); 22 + * int lstat(const char *path, struct stat *buf); 20 23 */ 21 24 22 25 static __attribute__((unused)) ··· 40 37 41 38 42 39 static __attribute__((unused)) 43 - int stat(const char *path, struct stat *buf) 40 + int fstatat(int fd, const char *path, struct stat *buf, int flag) 44 41 { 45 42 struct statx statx; 46 43 long ret; 47 44 48 - ret = __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx)); 45 + ret = __sysret(sys_statx(fd, path, flag | AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx)); 49 46 if (ret == -1) 50 47 return ret; 51 48 ··· 71 68 buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec; 72 69 73 70 return 0; 71 + } 72 + 73 + static __attribute__((unused)) 74 + int stat(const char *path, struct stat *buf) 75 + { 76 + return fstatat(AT_FDCWD, path, buf, 0); 77 + } 78 + 79 + static __attribute__((unused)) 80 + int fstat(int fildes, struct stat *buf) 81 + { 82 + return fstatat(fildes, "", buf, AT_EMPTY_PATH); 83 + } 84 + 85 + static __attribute__((unused)) 86 + int lstat(const char *path, struct stat *buf) 87 + { 88 + return fstatat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW); 74 89 } 75 90 76 91 #endif /* _NOLIBC_SYS_STAT_H */