10 #define GOTO(label, rc) do { rc; goto label; } while (0)
12 int main (int argc, char **argv) {
14 unsigned long bytes, lbytes;
16 char *str, *str2, *readbuf;
19 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
23 bytes = strtoul(argv[2], NULL, 10);
25 printf("No bytes!\n");
29 printf("Need an even number of bytes!\n");
34 str = malloc(bytes+1);
36 printf("No enough memory for %lu bytes.\n", bytes);
39 str2 = malloc(lbytes+1);
41 printf("No enough memory for %lu bytes.\n", lbytes);
42 GOTO(out_str, rc = 5);
44 readbuf = malloc(bytes*2);
46 printf("No enough memory for %lu bytes.\n", bytes*2);
47 GOTO(out_str2, rc = 6);
50 for(i=0; i < bytes; i++)
51 str[i] = 'a' + (i % 26);
54 memcpy(str2, str, bytes);
55 memcpy(str2+(bytes/2), str, bytes);
59 printf("First String: %s\nSecond String: %s\n", str, str2);
61 fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
63 printf("Could not open file %s.\n", argv[1]);
64 GOTO(out_readbuf, rc = 7);
67 rc = write(fd, str, bytes);
69 printf("Write failed!\n");
75 if (rc < 0 || st.st_size != bytes) {
76 printf("bad file %lu size first write %lu != %lu: rc %d\n",
77 (unsigned long)st.st_ino, (unsigned long)st.st_size,
82 rc = lseek(fd, bytes / 2, SEEK_SET);
83 if (rc != bytes / 2) {
84 printf("Seek failed!\n");
85 GOTO(out_fd, rc = 10);
88 rc = write(fd, str, bytes);
90 printf("Write failed!\n");
91 GOTO(out_fd, rc = 11);
95 if (rc < 0 || st.st_size != bytes + bytes / 2) {
96 printf("bad file %lu size second write %lu != %lu: rc %d\n",
97 (unsigned long)st.st_ino, (unsigned long)st.st_size,
99 GOTO(out_fd, rc = 12);
102 rc = lseek(fd, 0, SEEK_SET);
104 printf("Seek failed!\n");
105 GOTO(out_fd, rc = 13);
108 rc = read(fd, readbuf, bytes * 2);
110 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
114 printf("%s\n%s\n", readbuf, str2);
116 if (rc < 0 || st.st_size != bytes + bytes / 2) {
117 printf("bad file size after read %lu != %lu: rc %d\n",
118 (unsigned long)st.st_size, bytes + bytes / 2,
120 GOTO(out_fd, rc = 14);
123 GOTO(out_fd, rc = 15);
128 printf("%s\n%s\n", readbuf, str2);
129 if (strcmp(readbuf, str2)) {
130 printf("No match!\n");
131 GOTO(out_fd, rc = 16);