Setup LOCPATH for tst-ftell-active-handler and tst-ftell-partial-wide in libio
[platform/upstream/glibc.git] / libio / tst-atime.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
8 #include <sys/statvfs.h>
9
10
11 static int do_test (void);
12 #define TEST_FUNCTION do_test ()
13 #define TIMEOUT 5
14 #include <test-skeleton.c>
15
16
17 static int
18 do_test (void)
19 {
20   char *buf;
21   int fd;
22   FILE *fp;
23   int ch;
24   struct stat st1;
25   struct stat st2;
26   struct statvfs sv;
27   int e;
28
29   buf = (char *) malloc (strlen (test_dir) + sizeof "/tst-atime.XXXXXX");
30   if (buf == NULL)
31     {
32       printf ("cannot allocate memory: %m\n");
33       return 1;
34     }
35   stpcpy (stpcpy (buf, test_dir), "/tst-atime.XXXXXX");
36
37   fd = mkstemp (buf);
38   if (fd == -1)
39     {
40       printf ("cannot open temporary file: %m\n");
41       return 1;
42     }
43
44 #ifdef ST_NOATIME
45   /* Make sure the filesystem doesn't have the noatime option set.  If
46      statvfs is not available just continue.  */
47   e = fstatvfs (fd, &sv);
48   if (e != ENOSYS)
49     {
50       if (e != 0)
51         {
52           printf ("cannot statvfs '%s': %m\n", buf);
53           return 1;
54         }
55
56       if ((sv.f_flag & ST_NOATIME) != 0)
57         {
58           puts ("Bah!  The filesystem is mounted with noatime");
59           return 0;
60         }
61     }
62 #endif
63
64   /* Make sure it gets removed.  */
65   add_temp_file (buf);
66
67   if (write (fd, "some string\n", 12) != 12)
68     {
69       printf ("cannot write temporary file: %m\n");
70       return 1;
71     }
72
73   if (lseek (fd, 0, SEEK_SET) == (off_t) -1)
74     {
75       printf ("cannot reposition temporary file: %m\n");
76       return 1;
77     }
78
79   fp = fdopen (fd, "r");
80   if (fp == NULL)
81     {
82       printf ("cannot create stream: %m\n");
83       return 1;
84     }
85
86   if (fstat (fd, &st1) == -1)
87     {
88       printf ("first stat failed: %m\n");
89       return 1;
90     }
91
92   sleep (2);
93
94   ch = fgetc (fp);
95   if (ch != 's')
96     {
97       printf ("did not read correct character: got '%c', expected 's'\n", ch);
98       return 1;
99     }
100
101   if (fstat (fd, &st2) == -1)
102     {
103       printf ("second stat failed: %m\n");
104       return 1;
105     }
106
107   if (st1.st_atime > st2.st_atime)
108     {
109       puts ("second atime smaller");
110       return 1;
111     }
112   else if (st1.st_atime == st2.st_atime)
113     {
114       puts ("atime has not changed");
115       return 1;
116     }
117
118   fclose (fp);
119
120   return 0;
121 }