From 48d91c62cd27c15fe0e928abf6d023d17e9c41f7 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 19 Apr 2024 14:50:48 -0400 Subject: config: Check for struct stat::st_{a,c,m,birth}{tim,timespec} --- config/header.mk | 4 ++++ config/st-acmtim.c | 12 ++++++++++++ config/st-acmtimespec.c | 12 ++++++++++++ config/st-birthtim.c | 9 +++++++++ config/st-birthtimespec.c | 9 +++++++++ src/bfstd.h | 20 +++++++++++++++----- src/stat.c | 11 +++++++---- tests/xtouch.c | 4 ++-- 8 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 config/st-acmtim.c create mode 100644 config/st-acmtimespec.c create mode 100644 config/st-birthtim.c create mode 100644 config/st-birthtimespec.c diff --git a/config/header.mk b/config/header.mk index 38f1277..5b6ff11 100644 --- a/config/header.mk +++ b/config/header.mk @@ -20,6 +20,10 @@ HEADERS := \ ${GEN}/pipe2.h \ ${GEN}/posix-spawn-addfchdir.h \ ${GEN}/posix-spawn-addfchdir-np.h \ + ${GEN}/st-acmtim.h \ + ${GEN}/st-acmtimespec.h \ + ${GEN}/st-birthtim.h \ + ${GEN}/st-birthtimespec.h \ ${GEN}/statx.h \ ${GEN}/statx-syscall.h \ ${GEN}/strerror-l.h \ diff --git a/config/st-acmtim.c b/config/st-acmtim.c new file mode 100644 index 0000000..d687ab0 --- /dev/null +++ b/config/st-acmtim.c @@ -0,0 +1,12 @@ +// Copyright © Tavian Barnes +// SPDX-License-Identifier: 0BSD + +#include + +int main(void) { + struct stat sb = {0}; + unsigned int a = sb.st_atim.tv_sec; + unsigned int c = sb.st_ctim.tv_sec; + unsigned int m = sb.st_mtim.tv_sec; + return a + c + m; +} diff --git a/config/st-acmtimespec.c b/config/st-acmtimespec.c new file mode 100644 index 0000000..f747bc0 --- /dev/null +++ b/config/st-acmtimespec.c @@ -0,0 +1,12 @@ +// Copyright © Tavian Barnes +// SPDX-License-Identifier: 0BSD + +#include + +int main(void) { + struct stat sb = {0}; + unsigned int a = sb.st_atimespec.tv_sec; + unsigned int c = sb.st_ctimespec.tv_sec; + unsigned int m = sb.st_mtimespec.tv_sec; + return a + c + m; +} diff --git a/config/st-birthtim.c b/config/st-birthtim.c new file mode 100644 index 0000000..4964571 --- /dev/null +++ b/config/st-birthtim.c @@ -0,0 +1,9 @@ +// Copyright © Tavian Barnes +// SPDX-License-Identifier: 0BSD + +#include + +int main(void) { + struct stat sb = {0}; + return sb.st_birthtim.tv_sec; +} diff --git a/config/st-birthtimespec.c b/config/st-birthtimespec.c new file mode 100644 index 0000000..91a613f --- /dev/null +++ b/config/st-birthtimespec.c @@ -0,0 +1,9 @@ +// Copyright © Tavian Barnes +// SPDX-License-Identifier: 0BSD + +#include + +int main(void) { + struct stat sb = {0}; + return sb.st_birthtimespec.tv_sec; +} diff --git a/src/bfstd.h b/src/bfstd.h index 42f5d5b..f91e380 100644 --- a/src/bfstd.h +++ b/src/bfstd.h @@ -283,11 +283,21 @@ int xminor(dev_t dev); // #include -#if __APPLE__ -# define st_atim st_atimespec -# define st_ctim st_ctimespec -# define st_mtim st_mtimespec -# define st_birthtim st_birthtimespec +/** + * Get the access/change/modification time from a struct stat. + */ +#if BFS_HAS_ST_ACMTIM +# define ST_ATIM(sb) (sb).st_atim +# define ST_CTIM(sb) (sb).st_ctim +# define ST_MTIM(sb) (sb).st_mtim +#elif BFS_HAS_ST_ACMTIMESPEC +# define ST_ATIM(sb) (sb).st_atimespec +# define ST_CTIM(sb) (sb).st_ctimespec +# define ST_MTIM(sb) (sb).st_mtimespec +#else +# define ST_ATIM(sb) ((struct timespec) { .tv_sec = (sb).st_atime }) +# define ST_CTIM(sb) ((struct timespec) { .tv_sec = (sb).st_ctime }) +# define ST_MTIM(sb) ((struct timespec) { .tv_sec = (sb).st_mtime }) #endif // #include diff --git a/src/stat.c b/src/stat.c index e525f24..a32dbaa 100644 --- a/src/stat.c +++ b/src/stat.c @@ -104,18 +104,21 @@ void bfs_stat_convert(struct bfs_stat *dest, const struct stat *src) { dest->mask |= BFS_STAT_ATTRS; #endif - dest->atime = src->st_atim; + dest->atime = ST_ATIM(*src); dest->mask |= BFS_STAT_ATIME; - dest->ctime = src->st_ctim; + dest->ctime = ST_CTIM(*src); dest->mask |= BFS_STAT_CTIME; - dest->mtime = src->st_mtim; + dest->mtime = ST_MTIM(*src); dest->mask |= BFS_STAT_MTIME; -#if __APPLE__ || __FreeBSD__ || __NetBSD__ +#if BFS_HAS_ST_BIRTHTIM dest->btime = src->st_birthtim; dest->mask |= BFS_STAT_BTIME; +#elif BFS_HAS_ST_BIRTHTIMESPEC + dest->btime = src->st_birthtimespec; + dest->mask |= BFS_STAT_BTIME; #endif } diff --git a/tests/xtouch.c b/tests/xtouch.c index 82d749d..cd41842 100644 --- a/tests/xtouch.c +++ b/tests/xtouch.c @@ -237,8 +237,8 @@ int main(int argc, char *argv[]) { fprintf(stderr, "%s: '%s': %s\n", cmd, rarg, xstrerror(errno)); return EXIT_FAILURE; } - times[0] = buf.st_atim; - times[1] = buf.st_mtim; + times[0] = ST_ATIM(buf); + times[1] = ST_MTIM(buf); } else if (darg) { if (xgetdate(darg, ×[0]) != 0) { fprintf(stderr, "%s: Parsing time '%s' failed: %s\n", cmd, darg, xstrerror(errno)); -- cgit v1.2.3