1 #include <sys/stat.h>
2
3 #include <assert.h>
4 #include <fcntl.h>
5 #include <time.h>
6 #include <unistd.h>
7
8 #define BASE_DIR "/tmp"
9 #define OUTPUT_DIR BASE_DIR "/testdir"
10 #define PATH OUTPUT_DIR "/output.txt"
11 #define SIZE 500
12
main(void)13 int main(void) {
14 struct timespec times[2];
15 struct stat st;
16 int fd;
17 int ret;
18 off_t pos;
19
20 (void)st;
21 ret = mkdir(OUTPUT_DIR, 0755);
22 assert(ret == 0);
23
24 fd = open(PATH, O_CREAT | O_WRONLY, 0666);
25 assert(fd != -1);
26
27 pos = lseek(fd, SIZE - 1, SEEK_SET);
28 assert(pos == SIZE - 1);
29
30 ret = (int)write(fd, "", 1);
31 assert(ret == 1);
32
33 ret = fstat(fd, &st);
34 assert(ret == 0);
35 assert(st.st_size == SIZE);
36
37 times[0].tv_sec = 4;
38 times[0].tv_nsec = 0;
39 times[1].tv_sec = 9;
40 times[1].tv_nsec = 0;
41 assert(0 == futimens(fd, times));
42 assert(0 == fstat(fd, &st));
43 assert(4 == st.st_atime);
44 assert(9 == st.st_mtime);
45
46 ret = close(fd);
47 assert(ret == 0);
48
49 ret = access(PATH, R_OK);
50 assert(ret == 0);
51
52 ret = stat(PATH, &st);
53 assert(ret == 0);
54 assert(st.st_size == SIZE);
55
56 ret = unlink(PATH);
57 assert(ret == 0);
58
59 ret = stat(PATH, &st);
60 assert(ret == -1);
61
62 ret = stat(OUTPUT_DIR, &st);
63 assert(ret == 0);
64 assert(S_ISDIR(st.st_mode));
65
66 ret = rmdir(OUTPUT_DIR);
67 assert(ret == 0);
68
69 ret = stat(OUTPUT_DIR, &st);
70 assert(ret == -1);
71
72 return 0;
73 }
74