2.5-18.1
[glibc.git] / dirent / tst-fdopendir.c
blob3cf315d85ce9eb0cc33e803aeab924f220be1610
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <dirent.h>
6 #include <stdbool.h>
7 #include <string.h>
10 static int
11 do_test (void)
13 char fname[] = "/tmp/jXXXXXX";
14 int fd = mkstemp (fname);
15 if (fd == -1)
17 puts ("mkstemp failed");
18 return 1;
21 write (fd, "hello", 5);
22 close (fd);
24 struct stat64 st;
25 if (stat64 (fname, &st) == -1)
27 puts ("first stat failed");
28 return 0;
31 /* Make sure there is enough time between the creation and the access. */
32 sleep (2);
34 fd = open (fname, O_RDONLY | O_NOATIME);
35 if (fd == -1)
37 puts ("first open failed");
38 return 1;
41 char buf[5];
42 read(fd, buf, sizeof (buf));
43 close(fd);
45 struct stat64 st2;
46 if (stat64 (fname, &st2) == -1)
48 puts ("second stat failed");
49 return 0;
52 bool no_noatime = false;
53 #ifdef _STATBUF_ST_NSEC
54 if (st.st_atim.tv_sec != st2.st_atim.tv_sec
55 || st.st_atim.tv_nsec != st2.st_atim.tv_nsec)
56 #else
57 if (st.st_atime != st2.st_atime)
58 #endif
60 puts ("file atime changed");
61 no_noatime = true;
64 unlink(fname);
66 strcpy(fname, "/tmp/dXXXXXX");
67 char *d = mkdtemp (fname);
68 if (d == NULL)
70 puts ("mkdtemp failed");
71 return 1;
74 if (stat64 (d, &st) == -1)
76 puts ("third stat failed");
77 return 0;
79 sleep (2);
81 fd = open64 (d, O_RDONLY|O_NDELAY|O_DIRECTORY|O_NOATIME);
82 if (fd == -1)
84 puts ("second open failed");
85 return 1;
87 DIR *dir = fdopendir (fd);
88 if (dir == NULL)
90 puts ("fdopendir failed");
91 return 1;
94 struct dirent *de;
95 while ((de = readdir (dir)) != NULL)
98 closedir (dir);
100 if (stat64 (d, &st2) == -1)
102 puts ("fourth stat failed");
103 return 0;
105 #ifdef _STATBUF_ST_NSEC
106 if (!no_noatime
107 && (st.st_atim.tv_sec != st2.st_atim.tv_sec
108 || st.st_atim.tv_nsec != st2.st_atim.tv_nsec))
109 #else
110 if (!no_noatime && st.st_atime != st2.st_atime)
111 #endif
113 puts ("directory atime changed");
114 return 1;
117 rmdir(fname);
119 return 0;
122 #define TIMEOUT 6
123 #define TEST_FUNCTION do_test ()
124 #include "../test-skeleton.c"