7fa7a1f80d319c32059032a0f2180fabd7d415c3
[libc-test] / src / functional / utime.c
1 #include <sys/stat.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <time.h>
6 #include <stdint.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include "test.h"
10
11 #define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
12 #define TESTVAL(v,op,x) TEST(v op x, "%jd\n", (intmax_t)(v))
13
14 int main(void)
15 {
16         struct stat st;
17         FILE *f;
18         int fd;
19         time_t t;
20
21         TEST(utimensat(AT_FDCWD, "/dev/null/invalid", ((struct timespec[2]){{.tv_nsec=UTIME_OMIT},{.tv_nsec=UTIME_OMIT}}), 0)==0 || errno==ENOTDIR,
22                 "%s\n", strerror(errno));
23         TEST(futimens(-1, ((struct timespec[2]){{.tv_nsec=UTIME_OMIT},{.tv_nsec=UTIME_OMIT}}))==0 || errno==EBADF,
24                 "%s\n", strerror(errno));
25
26         if (!TEST(f = tmpfile())) return t_status;
27         fd = fileno(f);
28
29         TEST(futimens(fd, (struct timespec[2]){0}) == 0, "\n");
30         TEST(fstat(fd, &st) == 0, "\n");
31         TESTVAL(st.st_atim.tv_sec,==,0);
32         TESTVAL(st.st_atim.tv_nsec,==,0);
33         TESTVAL(st.st_mtim.tv_sec,==,0);
34         TESTVAL(st.st_mtim.tv_nsec,==,0);
35
36         TEST(futimens(fd, ((struct timespec[2]){{.tv_sec=1,.tv_nsec=UTIME_OMIT},{.tv_sec=1,.tv_nsec=UTIME_OMIT}})) == 0, "\n");
37         TEST(fstat(fd, &st) == 0, "\n");
38         TESTVAL(st.st_atim.tv_sec,==,0);
39         TESTVAL(st.st_atim.tv_nsec,==,0);
40         TESTVAL(st.st_mtim.tv_sec,==,0);
41         TESTVAL(st.st_mtim.tv_nsec,==,0);
42
43         t = time(0);
44
45         TEST(futimens(fd, ((struct timespec[2]){{.tv_nsec=UTIME_NOW},{.tv_nsec=UTIME_OMIT}})) == 0, "\n");
46         TEST(fstat(fd, &st) == 0, "\n");
47         TESTVAL(st.st_atim.tv_sec,>=,t);
48         TESTVAL(st.st_mtim.tv_sec,==,0);
49         TESTVAL(st.st_mtim.tv_nsec,==,0);
50         
51         TEST(futimens(fd, (struct timespec[2]){0}) == 0, "\n");
52         TEST(futimens(fd, ((struct timespec[2]){{.tv_nsec=UTIME_OMIT},{.tv_nsec=UTIME_NOW}})) == 0, "\n");
53         TEST(fstat(fd, &st) == 0, "\n");
54         TESTVAL(st.st_atim.tv_sec,==,0);
55         TESTVAL(st.st_mtim.tv_sec,>=,t);
56
57         TEST(futimens(fd, ((struct timespec[2]){{.tv_nsec=UTIME_NOW},{.tv_nsec=UTIME_OMIT}})) == 0, "\n");
58         TEST(fstat(fd, &st) == 0, "\n");
59         TESTVAL(st.st_atim.tv_sec,>=,t);
60         TESTVAL(st.st_mtim.tv_sec,>=,t);
61
62         if (TEST((time_t)(1LL<<32) == (1LL<<32), "implementation has Y2038 EOL\n")) {
63                 if (TEST(futimens(fd, ((struct timespec[2]){{.tv_sec=1LL<<32},{.tv_sec=1LL<<32}})) == 0, "%s\n", strerror(errno))) {
64                         TEST(fstat(fd, &st) == 0, "\n");
65                         TESTVAL(st.st_atim.tv_sec, ==, 1LL<<32);
66                         TESTVAL(st.st_mtim.tv_sec, ==, 1LL<<32);
67                 }
68         }
69
70         fclose(f);
71
72         return t_status;
73 }