powerpc: Update ulps
[glibc.git] / dirent / tst-fdopendir.c
blobd6a24f47db25b7b6f638682f49161597c3e9354c
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>
8 #include <sys/stat.h>
10 #include <support/xunistd.h>
12 #ifndef O_NOATIME
13 # define O_NOATIME 0
14 #endif
16 static int
17 do_test (void)
19 char fname[] = "/tmp/jXXXXXX";
20 int fd = mkstemp (fname);
21 if (fd == -1)
23 puts ("mkstemp failed");
24 return 1;
27 xwrite (fd, "hello", 5);
28 close (fd);
30 struct stat64 st;
31 if (stat64 (fname, &st) == -1)
33 puts ("first stat failed");
34 return 0;
37 /* Make sure there is enough time between the creation and the access. */
38 sleep (2);
40 fd = open (fname, O_RDONLY | O_NOATIME);
41 if (fd == -1)
43 puts ("first open failed");
44 return 1;
47 char buf[5];
48 xread(fd, buf, sizeof (buf));
50 close(fd);
52 struct stat64 st2;
53 if (stat64 (fname, &st2) == -1)
55 puts ("second stat failed");
56 return 0;
59 bool no_noatime = false;
60 #ifdef _STATBUF_ST_NSEC
61 if (st.st_atim.tv_sec != st2.st_atim.tv_sec
62 || st.st_atim.tv_nsec != st2.st_atim.tv_nsec)
63 #else
64 if (st.st_atime != st2.st_atime)
65 #endif
67 puts ("file atime changed");
68 no_noatime = true;
71 unlink(fname);
73 strcpy(fname, "/tmp/dXXXXXX");
74 char *d = mkdtemp (fname);
75 if (d == NULL)
77 puts ("mkdtemp failed");
78 return 1;
81 if (stat64 (d, &st) == -1)
83 puts ("third stat failed");
84 return 0;
86 sleep (2);
88 fd = open64 (d, O_RDONLY|O_NDELAY|O_DIRECTORY|O_NOATIME);
89 if (fd == -1)
91 puts ("second open failed");
92 return 1;
94 DIR *dir = fdopendir (fd);
95 if (dir == NULL)
97 puts ("fdopendir failed");
98 return 1;
101 struct dirent *de;
102 while ((de = readdir (dir)) != NULL)
105 closedir (dir);
107 if (stat64 (d, &st2) == -1)
109 puts ("fourth stat failed");
110 return 0;
112 #ifdef _STATBUF_ST_NSEC
113 if (!no_noatime
114 && (st.st_atim.tv_sec != st2.st_atim.tv_sec
115 || st.st_atim.tv_nsec != st2.st_atim.tv_nsec))
116 #else
117 if (!no_noatime && st.st_atime != st2.st_atime)
118 #endif
120 puts ("directory atime changed");
121 return 1;
124 rmdir(fname);
126 return 0;
129 #define TEST_FUNCTION do_test ()
130 #include "../test-skeleton.c"