2003-05-04 Roland McGrath <roland@redhat.com>
[glibc.git] / libio / tst-atime.c
blob0b0b4f04c4716b38c66c413165b82501eec09c99
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>
11 static int do_test (void);
12 #define TEST_FUNCTION do_test ()
13 #define TIMEOUT 5
14 #include <test-skeleton.c>
17 static int
18 do_test (void)
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;
29 buf = (char *) malloc (strlen (test_dir) + sizeof "/tst-atime.XXXXXX");
30 if (buf == NULL)
32 printf ("cannot allocate memory: %m\n");
33 return 1;
35 stpcpy (stpcpy (buf, test_dir), "/tst-atime.XXXXXX");
37 fd = mkstemp (buf);
38 if (fd == -1)
40 printf ("cannot open temporary file: %m\n");
41 return 1;
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)
50 if (e != 0)
52 printf ("cannot statvfs '%s': %m\n", buf);
53 return 1;
56 if ((sv.f_flag & ST_NOATIME) != 0)
58 puts ("Bah! The filesystem is mounted with noatime");
59 return 0;
62 #endif
64 /* Make sure it gets removed. */
65 add_temp_file (buf);
67 if (write (fd, "some string\n", 12) != 12)
69 printf ("cannot write temporary file: %m\n");
70 return 1;
73 if (lseek (fd, 0, SEEK_SET) == (off_t) -1)
75 printf ("cannot reposition temporary file: %m\n");
76 return 1;
79 fp = fdopen (fd, "r");
80 if (fp == NULL)
82 printf ("cannot create stream: %m\n");
83 return 1;
86 if (fstat (fd, &st1) == -1)
88 printf ("first stat failed: %m\n");
89 return 1;
92 sleep (2);
94 ch = fgetc (fp);
95 if (ch != 's')
97 printf ("did not read correct character: got '%c', expected 's'\n", ch);
98 return 1;
101 if (fstat (fd, &st2) == -1)
103 printf ("second stat failed: %m\n");
104 return 1;
107 if (st1.st_atime > st2.st_atime)
109 puts ("second atime smaller");
110 return 1;
112 else if (st1.st_atime == st2.st_atime)
114 puts ("atime has not changed");
115 return 1;
118 fclose (fp);
120 return 0;