Ticket #19045: foo.c

File foo.c, 1.5 KB (added by bhaible, 3 weeks ago)

test case foo.c

Line 
1#include <fcntl.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <sys/stat.h>
6#include <time.h>
7#include <unistd.h>
8#include <utime.h>
9#include <assert.h>
10
11static int
12ctime_compare (struct stat const *a, struct stat const *b)
13{
14 if (a->st_ctime < b->st_ctime)
15 return -1;
16 else if (b->st_ctime < a->st_ctime)
17 return 1;
18 else if (a->st_ctim.tv_nsec < b->st_ctim.tv_nsec)
19 return -1;
20 else if (b->st_ctim.tv_nsec < a->st_ctim.tv_nsec)
21 return 1;
22 else
23 return 0;
24}
25
26int main ()
27{
28 struct stat st1;
29 struct stat st2;
30
31 int fd = open ("file", O_RDWR | O_CREAT | O_TRUNC, 0600);
32 assert (0 <= fd);
33 assert (fstat (fd, &st1) == 0);
34 assert (close (fd) == 0);
35
36 sleep (1);
37
38 {
39 /* Set both times. */
40 struct utimbuf ts;
41 ts.actime = ts.modtime = time (NULL);
42 assert (utime ("file", &ts) == 0);
43 assert (stat ("file", &st1) == 0);
44 }
45
46 sleep (1);
47
48 assert (stat ("file", &st2) == 0);
49
50 {
51 /* Set both times. */
52 struct utimbuf ts;
53 ts.actime = ts.modtime = 946684800;
54 assert (utime ("file", &ts) == 0);
55
56 assert (stat ("file", &st2) == 0);
57 assert (st2.st_mtime == 946684800);
58 assert (0 <= st2.st_mtim.tv_nsec);
59 assert (st2.st_mtim.tv_nsec < 1000000000);
60 printf ("st1.st_ctime = %lu.%09u, st2.st_ctime = %lu.%09u\n",
61 (unsigned long) st1.st_ctime, (unsigned int) st1.st_ctim.tv_nsec,
62 (unsigned long) st2.st_ctime, (unsigned int) st2.st_ctim.tv_nsec);
63 assert (ctime_compare (&st1, &st2) < 0);
64 }
65
66 printf ("OK\n");
67}