Blame src/functional/utime.c

Rich Felker de945f
#include <sys/stat.h>
Rich Felker de945f
#include <errno.h>
Rich Felker de945f
#include <string.h>
Rich Felker de945f
#include <stdio.h>
Rich Felker de945f
#include <time.h>
Rich Felker de945f
#include <stdint.h>
Rich Felker de945f
#include <unistd.h>
Rich Felker de945f
#include <fcntl.h>
Rich Felker de945f
#include "test.h"
Rich Felker de945f
Rich Felker de945f
#define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
Rich Felker de945f
#define TESTVAL(v,op,x) TEST(v op x, "%jd\n", (intmax_t)(v))
Rich Felker de945f
Rich Felker de945f
int main(void)
Rich Felker de945f
{
Rich Felker de945f
	struct stat st;
Rich Felker de945f
	FILE *f;
Rich Felker de945f
	int fd;
Rich Felker de945f
	time_t t;
Rich Felker de945f
Rich Felker de945f
	TEST(utimensat(AT_FDCWD, "/dev/null/invalid", ((struct timespec[2]){{.tv_nsec=UTIME_OMIT},{.tv_nsec=UTIME_OMIT}}), 0)==0 || errno==ENOTDIR,
Rich Felker de945f
		"%s\n", strerror(errno));
Rich Felker de945f
	TEST(futimens(-1, ((struct timespec[2]){{.tv_nsec=UTIME_OMIT},{.tv_nsec=UTIME_OMIT}}))==0 || errno==EBADF,
Rich Felker de945f
		"%s\n", strerror(errno));
Rich Felker de945f
Rich Felker de945f
	if (!TEST(f = tmpfile())) return t_status;
Rich Felker de945f
	fd = fileno(f);
Rich Felker de945f
Rich Felker de945f
	TEST(futimens(fd, (struct timespec[2]){0}) == 0, "\n");
Rich Felker de945f
	TEST(fstat(fd, &st) == 0, "\n");
Rich Felker de945f
	TESTVAL(st.st_atim.tv_sec,==,0);
Rich Felker de945f
	TESTVAL(st.st_atim.tv_nsec,==,0);
Rich Felker de945f
	TESTVAL(st.st_mtim.tv_sec,==,0);
Rich Felker de945f
	TESTVAL(st.st_mtim.tv_nsec,==,0);
Rich Felker de945f
Rich Felker de945f
	TEST(futimens(fd, ((struct timespec[2]){{.tv_sec=1,.tv_nsec=UTIME_OMIT},{.tv_sec=1,.tv_nsec=UTIME_OMIT}})) == 0, "\n");
Rich Felker de945f
	TEST(fstat(fd, &st) == 0, "\n");
Rich Felker de945f
	TESTVAL(st.st_atim.tv_sec,==,0);
Rich Felker de945f
	TESTVAL(st.st_atim.tv_nsec,==,0);
Rich Felker de945f
	TESTVAL(st.st_mtim.tv_sec,==,0);
Rich Felker de945f
	TESTVAL(st.st_mtim.tv_nsec,==,0);
Rich Felker de945f
Rich Felker de945f
	t = time(0);
Rich Felker de945f
Rich Felker de945f
	TEST(futimens(fd, ((struct timespec[2]){{.tv_nsec=UTIME_NOW},{.tv_nsec=UTIME_OMIT}})) == 0, "\n");
Rich Felker de945f
	TEST(fstat(fd, &st) == 0, "\n");
Rich Felker de945f
	TESTVAL(st.st_atim.tv_sec,>=,t);
Rich Felker de945f
	TESTVAL(st.st_mtim.tv_sec,==,0);
Rich Felker de945f
	TESTVAL(st.st_mtim.tv_nsec,==,0);
Rich Felker de945f
	
Rich Felker de945f
	TEST(futimens(fd, (struct timespec[2]){0}) == 0, "\n");
Rich Felker de945f
	TEST(futimens(fd, ((struct timespec[2]){{.tv_nsec=UTIME_OMIT},{.tv_nsec=UTIME_NOW}})) == 0, "\n");
Rich Felker de945f
	TEST(fstat(fd, &st) == 0, "\n");
Rich Felker de945f
	TESTVAL(st.st_atim.tv_sec,==,0);
Rich Felker de945f
	TESTVAL(st.st_mtim.tv_sec,>=,t);
Rich Felker de945f
Rich Felker de945f
	TEST(futimens(fd, ((struct timespec[2]){{.tv_nsec=UTIME_NOW},{.tv_nsec=UTIME_OMIT}})) == 0, "\n");
Rich Felker de945f
	TEST(fstat(fd, &st) == 0, "\n");
Rich Felker de945f
	TESTVAL(st.st_atim.tv_sec,>=,t);
Rich Felker de945f
	TESTVAL(st.st_mtim.tv_sec,>=,t);
Rich Felker de945f
Rich Felker de945f
	if (TEST((time_t)(1LL<<32) == (1LL<<32), "implementation has Y2038 EOL\n")) {
Rich Felker de945f
		if (TEST(futimens(fd, ((struct timespec[2]){{.tv_sec=1LL<<32},{.tv_sec=1LL<<32}})) == 0, "%s\n", strerror(errno))) {
Rich Felker de945f
			TEST(fstat(fd, &st) == 0, "\n");
Rich Felker de945f
			TESTVAL(st.st_atim.tv_sec, ==, 1LL<<32);
Rich Felker de945f
			TESTVAL(st.st_mtim.tv_sec, ==, 1LL<<32);
Rich Felker de945f
		}
Rich Felker de945f
	}
Rich Felker de945f
Rich Felker de945f
	fclose(f);
Rich Felker de945f
Rich Felker de945f
	return t_status;
Rich Felker de945f
}