Fix indentation.
[glibc.git] / dirent / tst-fdopendir.c
blob0780c19916e96d370080be24e8a72d1d3a73a791
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>
9 #ifndef O_NOATIME
10 # define O_NOATIME 0
11 #endif
13 static int
14 do_test (void)
16 char fname[] = "/tmp/jXXXXXX";
17 int fd = mkstemp (fname);
18 if (fd == -1)
20 puts ("mkstemp failed");
21 return 1;
24 write (fd, "hello", 5);
25 close (fd);
27 struct stat64 st;
28 if (stat64 (fname, &st) == -1)
30 puts ("first stat failed");
31 return 0;
34 /* Make sure there is enough time between the creation and the access. */
35 sleep (2);
37 fd = open (fname, O_RDONLY | O_NOATIME);
38 if (fd == -1)
40 puts ("first open failed");
41 return 1;
44 char buf[5];
45 read(fd, buf, sizeof (buf));
46 close(fd);
48 struct stat64 st2;
49 if (stat64 (fname, &st2) == -1)
51 puts ("second stat failed");
52 return 0;
55 bool no_noatime = false;
56 #ifdef _STATBUF_ST_NSEC
57 if (st.st_atim.tv_sec != st2.st_atim.tv_sec
58 || st.st_atim.tv_nsec != st2.st_atim.tv_nsec)
59 #else
60 if (st.st_atime != st2.st_atime)
61 #endif
63 puts ("file atime changed");
64 no_noatime = true;
67 unlink(fname);
69 strcpy(fname, "/tmp/dXXXXXX");
70 char *d = mkdtemp (fname);
71 if (d == NULL)
73 puts ("mkdtemp failed");
74 return 1;
77 if (stat64 (d, &st) == -1)
79 puts ("third stat failed");
80 return 0;
82 sleep (2);
84 fd = open64 (d, O_RDONLY|O_NDELAY|O_DIRECTORY|O_NOATIME);
85 if (fd == -1)
87 puts ("second open failed");
88 return 1;
90 DIR *dir = fdopendir (fd);
91 if (dir == NULL)
93 puts ("fdopendir failed");
94 return 1;
97 struct dirent *de;
98 while ((de = readdir (dir)) != NULL)
101 closedir (dir);
103 if (stat64 (d, &st2) == -1)
105 puts ("fourth stat failed");
106 return 0;
108 #ifdef _STATBUF_ST_NSEC
109 if (!no_noatime
110 && (st.st_atim.tv_sec != st2.st_atim.tv_sec
111 || st.st_atim.tv_nsec != st2.st_atim.tv_nsec))
112 #else
113 if (!no_noatime && st.st_atime != st2.st_atime)
114 #endif
116 puts ("directory atime changed");
117 return 1;
120 rmdir(fname);
122 return 0;
125 #define TIMEOUT 6
126 #define TEST_FUNCTION do_test ()
127 #include "../test-skeleton.c"