9 #define T1 "write before unlink\n"
10 #define T2 "write after unlink\n"
13 int main(int argc, char **argv)
18 fprintf(stderr, "usage: %s filename\n", argv[0]);
21 fprintf(stderr, "congratulations - program starting\n");
24 fprintf(stderr, "opening\n");
25 fd = open(argv[1], O_RDWR | O_TRUNC | O_CREAT, 0644);
27 fprintf(stderr, "open (normal) %s\n", strerror(errno));
31 fprintf(stderr, "writing\n");
32 rc = write(fd, T1, strlen(T1) + 1);
33 if (rc != strlen(T1) + 1) {
34 fprintf(stderr, "write (normal) %s\n", strerror(errno));
38 fprintf(stderr, "closing\n");
41 fprintf(stderr, "close (normal) %s\n", strerror(errno));
45 fprintf(stderr, "opening again\n");
46 fd = open(argv[1], O_RDWR);
48 fprintf(stderr, "open (unlink) %s\n", strerror(errno));
53 fprintf(stderr, "unlinking\n");
56 fprintf(stderr, "unlink %s\n", strerror(errno));
60 printf("unlink %s and press enter\n", argv[1]);
64 fprintf(stderr, "reading\n");
65 rc = read(fd, buf, strlen(T1) + 1);
66 if (rc != strlen(T1) + 1) {
67 fprintf(stderr, "read (unlink) %s rc %d\n",
72 fprintf(stderr, "comparing data\n");
73 if (memcmp(buf, T1, strlen(T1) + 1) ) {
74 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
78 fprintf(stderr, "truncating\n");
79 rc = ftruncate(fd, 0);
81 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
85 fprintf(stderr, "seeking\n");
86 rc = lseek(fd, 0, SEEK_SET);
88 fprintf(stderr, "seek (after unlink trunc) %s\n",
93 fprintf(stderr, "writing again\n");
94 rc = write(fd, T2, strlen(T2) + 1);
95 if (rc != strlen(T2) + 1) {
96 fprintf(stderr, "write (after unlink trunc) %s (rc %d)\n",
101 fprintf(stderr, "seeking\n");
102 rc = lseek(fd, 0, SEEK_SET);
104 fprintf(stderr, "seek (before unlink read) %s\n",
109 fprintf(stderr, "reading again\n");
110 rc = read(fd, buf, strlen(T2) + 1);
111 if (rc != strlen(T2) + 1) {
112 fprintf(stderr, "read (after unlink rewrite) %s\n",
117 fprintf(stderr, "comparing data again\n");
118 if (memcmp(buf, T2, strlen(T2) + 1)) {
119 fprintf(stderr, "FAILURE: read wrong data after rewrite\n");
123 fprintf(stderr, "closing again\n");
126 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
130 fprintf(stderr, "SUCCESS - goto beer\n");