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 (before) %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 (before) %s\n", strerror(errno));
38 fprintf(stderr, "closing\n");
41 fprintf(stderr, "close (before) %s\n", strerror(errno));
45 fprintf(stderr, "opening again\n");
46 fd = open(argv[1], O_RDWR );
48 fprintf(stderr, "open (before) %s\n", strerror(errno));
52 fprintf(stderr, "unlinking\n");
55 fprintf(stderr, "open %s\n", strerror(errno));
59 fprintf(stderr, "reading\n");
60 rc = read(fd, buf, strlen(T1) + 1);
61 if (rc != strlen(T1) + 1) {
62 fprintf(stderr, "read -after %s rc %d\n", strerror(errno), rc);
66 fprintf(stderr, "comparing data\n");
67 if (memcmp(buf, T1, strlen(T1) + 1) ) {
68 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
72 fprintf(stderr, "truncating\n");
73 rc = ftruncate(fd, 0);
75 fprintf(stderr, "truncate -after unl %s\n", strerror(errno));
79 fprintf(stderr, "seeking\n");
80 rc = lseek(fd, 0, SEEK_SET);
82 fprintf(stderr, "seek (before write) %s\n", strerror(errno));
86 fprintf(stderr, "writing again\n");
87 rc = write(fd, T2, strlen(T2) + 1);
88 if (rc != strlen(T2) + 1) {
89 fprintf(stderr, "write (before) %s (rc %d)\n", strerror(errno), rc);
93 fprintf(stderr, "seeking\n");
94 rc = lseek(fd, 0, SEEK_SET);
96 fprintf(stderr, "seek (before read) %s\n", strerror(errno));
100 fprintf(stderr, "reading again\n");
101 rc = read(fd, buf, strlen(T2) + 1);
102 if (rc != strlen(T2) + 1) {
103 fprintf(stderr, "read (after trunc) %s\n", strerror(errno));
107 fprintf(stderr, "comparing data again\n");
108 if (memcmp(buf, T2, strlen(T2) + 1) ) {
109 fprintf(stderr, "FAILURE: read wrong data after trunc\n");
113 fprintf(stderr, "closing again\n");
116 fprintf(stderr, "close (before) %s\n", strerror(errno));
120 fprintf(stderr, "SUCCESS - goto beer\n");