packaging: add license files to all sub components
[platform/upstream/linaro-glibc.git] / dirent / tst-fdopendir.c
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
9
10 static int
11 do_test (void)
12 {
13   char fname[] = "/tmp/jXXXXXX";
14   int fd = mkstemp (fname);
15   if (fd == -1)
16     {
17       puts ("mkstemp failed");
18       return 1;
19     }
20
21   write (fd, "hello", 5);
22   close (fd);
23
24   struct stat64 st;
25   if (stat64 (fname, &st) == -1)
26     {
27       puts ("first stat failed");
28       return 0;
29     }
30
31   /* Make sure there is enough time between the creation and the access.  */
32   sleep (2);
33
34   fd = open (fname, O_RDONLY | O_NOATIME);
35   if (fd == -1)
36     {
37       puts ("first open failed");
38       return 1;
39     }
40
41   char buf[5];
42   read(fd, buf, sizeof (buf));
43   close(fd);
44
45   struct stat64 st2;
46   if (stat64 (fname, &st2) == -1)
47     {
48       puts ("second stat failed");
49       return 0;
50     }
51
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
59     {
60       puts ("file atime changed");
61       no_noatime = true;
62     }
63
64   unlink(fname);
65
66   strcpy(fname, "/tmp/dXXXXXX");
67   char *d = mkdtemp (fname);
68   if (d == NULL)
69     {
70       puts ("mkdtemp failed");
71       return 1;
72     }
73
74   if (stat64 (d, &st) == -1)
75     {
76       puts ("third stat failed");
77       return 0;
78     }
79   sleep (2);
80
81   fd = open64 (d, O_RDONLY|O_NDELAY|O_DIRECTORY|O_NOATIME);
82   if (fd == -1)
83     {
84       puts ("second open failed");
85       return 1;
86     }
87   DIR *dir = fdopendir (fd);
88   if (dir == NULL)
89     {
90       puts ("fdopendir failed");
91       return 1;
92     }
93
94   struct dirent *de;
95   while ((de = readdir (dir)) != NULL)
96     ;
97
98   closedir (dir);
99
100   if (stat64 (d, &st2) == -1)
101     {
102       puts ("fourth stat failed");
103       return 0;
104     }
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
112     {
113       puts ("directory atime changed");
114       return 1;
115     }
116
117   rmdir(fname);
118
119   return 0;
120 }
121
122 #define TIMEOUT 6
123 #define TEST_FUNCTION do_test ()
124 #include "../test-skeleton.c"