#include #include #include #include #include #include int main(int argc, char *argv[]) { struct stat buf1, buf2, buf3; int fd; if (argc != 2) { printf("syntax: %s filename\n", argv[0]); exit(1); } fd = open(argv[1], O_CREAT|O_EXCL|O_WRONLY, 0644); if (fd == -1) { perror("open(%s, O_CREAT|O_EXCL|O_WRONLY) failed\n", argv[1]); exit(1); } if (fstat(fd, &buf1) == -1) { perror("fstat() failed\n"); exit(1); } printf("File: %s, st_size = %lu, st_ctime = %s\n", argv[1], buf1.st_size, asctime(localtime(&buf1.st_ctime))); close(fd); sleep(2); fd = open(argv[1], O_TRUNC|O_WRONLY); if (fd == -1) { perror("open(%s, O_TRUNC|O_WRONLY) failed\n", argv[1]); exit(1); } if (fstat(fd, &buf2) == -1) { perror("fstat() failed\n"); exit(1); } printf("File: %s, st_size = %lu, st_ctime = %s\n", argv[1], buf2.st_size, asctime(localtime(&buf2.st_ctime))); if (buf1.st_ctime == buf2.st_ctime) printf("Bad behaviour in open(%s, O_TRUNC)!\n", argv[1]); sleep(2); if (ftruncate(fd, 0) == -1) { perror("ftruncate(0) failed\n"); exit(1); } if (fstat(fd, &buf3) == -1) { perror("fstat() failed\n"); exit(1); } printf("File: %s, st_size = %lu, st_ctime = %s\n", argv[1], buf3.st_size, asctime(localtime(&buf3.st_ctime))); if (buf2.st_ctime != buf3.st_ctime) printf("Bad behaviour in ftruncate(0)!\n"); close(fd); exit(0); }