1570af302Sopenharmony_ci#include <sys/stat.h>
2570af302Sopenharmony_ci#include <errno.h>
3570af302Sopenharmony_ci#include <string.h>
4570af302Sopenharmony_ci#include <stdio.h>
5570af302Sopenharmony_ci#include <time.h>
6570af302Sopenharmony_ci#include <stdint.h>
7570af302Sopenharmony_ci#include <unistd.h>
8570af302Sopenharmony_ci#include <fcntl.h>
9570af302Sopenharmony_ci#include "test.h"
10570af302Sopenharmony_ci
11570af302Sopenharmony_ci#define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
12570af302Sopenharmony_ci#define TESTVAL(v,op,x) TEST(v op x, "%jd\n", (intmax_t)(v))
13570af302Sopenharmony_ci
14570af302Sopenharmony_ciint main(void)
15570af302Sopenharmony_ci{
16570af302Sopenharmony_ci	struct stat st;
17570af302Sopenharmony_ci	FILE *f;
18570af302Sopenharmony_ci	int fd;
19570af302Sopenharmony_ci	time_t t;
20570af302Sopenharmony_ci
21570af302Sopenharmony_ci	TEST(utimensat(AT_FDCWD, "/dev/null/invalid", ((struct timespec[2]){{.tv_nsec=UTIME_OMIT},{.tv_nsec=UTIME_OMIT}}), 0)==0 || errno==ENOTDIR,
22570af302Sopenharmony_ci		"%s\n", strerror(errno));
23570af302Sopenharmony_ci	TEST(futimens(-1, ((struct timespec[2]){{.tv_nsec=UTIME_OMIT},{.tv_nsec=UTIME_OMIT}}))==0 || errno==EBADF,
24570af302Sopenharmony_ci		"%s\n", strerror(errno));
25570af302Sopenharmony_ci
26570af302Sopenharmony_ci	if (!TEST(f = tmpfile())) return t_status;
27570af302Sopenharmony_ci	fd = fileno(f);
28570af302Sopenharmony_ci
29570af302Sopenharmony_ci	TEST(futimens(fd, (struct timespec[2]){0}) == 0, "\n");
30570af302Sopenharmony_ci	TEST(fstat(fd, &st) == 0, "\n");
31570af302Sopenharmony_ci	TESTVAL(st.st_atim.tv_sec,==,0);
32570af302Sopenharmony_ci	TESTVAL(st.st_atim.tv_nsec,==,0);
33570af302Sopenharmony_ci	TESTVAL(st.st_mtim.tv_sec,==,0);
34570af302Sopenharmony_ci	TESTVAL(st.st_mtim.tv_nsec,==,0);
35570af302Sopenharmony_ci
36570af302Sopenharmony_ci	TEST(futimens(fd, ((struct timespec[2]){{.tv_sec=1,.tv_nsec=UTIME_OMIT},{.tv_sec=1,.tv_nsec=UTIME_OMIT}})) == 0, "\n");
37570af302Sopenharmony_ci	TEST(fstat(fd, &st) == 0, "\n");
38570af302Sopenharmony_ci	TESTVAL(st.st_atim.tv_sec,==,0);
39570af302Sopenharmony_ci	TESTVAL(st.st_atim.tv_nsec,==,0);
40570af302Sopenharmony_ci	TESTVAL(st.st_mtim.tv_sec,==,0);
41570af302Sopenharmony_ci	TESTVAL(st.st_mtim.tv_nsec,==,0);
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci	t = time(0);
44570af302Sopenharmony_ci
45570af302Sopenharmony_ci	TEST(futimens(fd, ((struct timespec[2]){{.tv_nsec=UTIME_NOW},{.tv_nsec=UTIME_OMIT}})) == 0, "\n");
46570af302Sopenharmony_ci	TEST(fstat(fd, &st) == 0, "\n");
47570af302Sopenharmony_ci	TESTVAL(st.st_atim.tv_sec,>=,t);
48570af302Sopenharmony_ci	TESTVAL(st.st_mtim.tv_sec,==,0);
49570af302Sopenharmony_ci	TESTVAL(st.st_mtim.tv_nsec,==,0);
50570af302Sopenharmony_ci
51570af302Sopenharmony_ci	TEST(futimens(fd, (struct timespec[2]){0}) == 0, "\n");
52570af302Sopenharmony_ci	TEST(futimens(fd, ((struct timespec[2]){{.tv_nsec=UTIME_OMIT},{.tv_nsec=UTIME_NOW}})) == 0, "\n");
53570af302Sopenharmony_ci	TEST(fstat(fd, &st) == 0, "\n");
54570af302Sopenharmony_ci	TESTVAL(st.st_atim.tv_sec,==,0);
55570af302Sopenharmony_ci	TESTVAL(st.st_mtim.tv_sec,>=,t);
56570af302Sopenharmony_ci
57570af302Sopenharmony_ci	TEST(futimens(fd, ((struct timespec[2]){{.tv_nsec=UTIME_NOW},{.tv_nsec=UTIME_OMIT}})) == 0, "\n");
58570af302Sopenharmony_ci	TEST(fstat(fd, &st) == 0, "\n");
59570af302Sopenharmony_ci	TESTVAL(st.st_atim.tv_sec,>=,t);
60570af302Sopenharmony_ci	TESTVAL(st.st_mtim.tv_sec,>=,t);
61570af302Sopenharmony_ci
62570af302Sopenharmony_ci	if (TEST((time_t)(1LL<<32) == (1LL<<32), "implementation has Y2038 EOL\n")) {
63570af302Sopenharmony_ci		if (TEST(futimens(fd, ((struct timespec[2]){{.tv_sec=1LL<<32},{.tv_sec=1LL<<32}})) == 0, "%s\n", strerror(errno))) {
64570af302Sopenharmony_ci			TEST(fstat(fd, &st) == 0, "\n");
65570af302Sopenharmony_ci			TESTVAL(st.st_atim.tv_sec, ==, 1LL<<32);
66570af302Sopenharmony_ci			TESTVAL(st.st_mtim.tv_sec, ==, 1LL<<32);
67570af302Sopenharmony_ci		}
68570af302Sopenharmony_ci	}
69570af302Sopenharmony_ci
70570af302Sopenharmony_ci	fclose(f);
71570af302Sopenharmony_ci
72570af302Sopenharmony_ci	return t_status;
73570af302Sopenharmony_ci}
74