Created
January 28, 2015 08:33
-
-
Save rfjakob/a956df2edcb69b486938 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <unistd.h> | |
| #include <stdio.h> | |
| #include <sys/stat.h> | |
| #include <fcntl.h> | |
| int | |
| main (void) | |
| { | |
| char const *filename = "test.txt"; | |
| int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666); | |
| if (fd < 0) | |
| return perror ("open"), 1; | |
| static char const message[] = "This is a test.\n"; | |
| int messagelen = sizeof message - 1; | |
| if (write (fd, message, messagelen) != messagelen) | |
| return perror ("write"), 1; | |
| if (fsync (fd) != 0) | |
| return perror ("fsync"), 1; | |
| struct stat st1, st2, st3; | |
| if (fstat (fd, &st1) != 0) | |
| return perror ("fstat"), 1; | |
| if (close (fd) != 0) | |
| return perror ("close"), 1; | |
| int fd2 = open(filename, O_WRONLY|O_CLOEXEC); | |
| if (fd2 < 0) | |
| return perror ("open 2"), 1; | |
| if (fstat (fd2, &st2) != 0) | |
| return perror ("fstat 2"), 1; | |
| if (close (fd2) != 0) | |
| return perror ("close 2"), 1; | |
| if (! (st1.st_mtim.tv_sec == st2.st_mtim.tv_sec | |
| && st1.st_mtim.tv_nsec == st2.st_mtim.tv_nsec)) | |
| printf ("Emacs should work around this POSIX-conformance bug.\n"); | |
| sleep (2); | |
| if (stat (filename, &st3) != 0) | |
| return perror ("stat"), 1; | |
| if (! (st2.st_mtim.tv_sec == st3.st_mtim.tv_sec | |
| && st2.st_mtim.tv_nsec == st3.st_mtim.tv_nsec)) | |
| { | |
| printf ("Emacs does not work around this POSIX-conformance bug.\n"); | |
| } | |
| printf ("stat #1: %ld.%09ld\n", st1.st_mtim.tv_sec, st1.st_mtim.tv_nsec); | |
| printf ("stat #2: %ld.%09ld\n", st2.st_mtim.tv_sec, st2.st_mtim.tv_nsec); | |
| printf ("stat #3: %ld.%09ld\n", st3.st_mtim.tv_sec, st3.st_mtim.tv_nsec); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment