#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
char buf[4];
struct stat stbuf;
printf("\t\tatime\t\tmtime\t\tctime\n");
if (lstat(argv[1], &stbuf) == -1) {;
perror("lstat");
return -1;
}
printf("before open\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
if ((fd = open(argv[1], O_RDWR)) == -1) {
perror("open");
return -2;
}
if (fstat(fd, &stbuf) == -1) {
perror("fstat");
return -2;
}
printf("after open\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
read(fd, buf, sizeof(buf));
if (fstat(fd, &stbuf) == -1)
perror("fstat");
printf("after read\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
lseek(fd, 1, SEEK_SET);
if (fstat(fd, &stbuf) == -1)
perror("fstat");
printf("after lseek\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
sleep(1);
ftruncate(fd, 0);
if (fstat(fd, &stbuf) == -1) {
perror("fstat");
return -1;
}
printf("after truncate\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
sleep(1);
write(fd, "test", 4);
if (fstat(fd, &stbuf) == -1) {
perror("fstat");
return -1;
}
printf("after write\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
close(fd);
if (lstat(argv[1], &stbuf) == -1) {
perror("lstat");
return -1;
}
printf("after close\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
sleep(1);
if (chmod(argv[1], S_IRUSR | S_IWUSR | S_IXUSR) == -1) {
perror("chmod");
return -2;
}
if (lstat(argv[1], &stbuf) == -1) {
perror("lstat");
return -1;
}
printf("after chmod\t");
printf("%u\t%u\t%u\n", stbuf.st_atime, stbuf.st_mtime, stbuf.st_ctime);
return 0;
}
|