10 int main (int argc, char **argv) {
12 unsigned long bytes, lbytes;
14 char *str, *str2, *readbuf;
17 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
21 bytes = strtoul(argv[2], NULL, 10);
23 printf("No bytes!\n");
27 printf("Need an even number of bytes!\n");
32 str = malloc(bytes+1);
34 printf("No enough memory for %lu bytes.\n", bytes);
37 str2 = malloc(lbytes+1);
39 printf("No enough memory for %lu bytes.\n", lbytes);
42 readbuf = malloc(bytes*2);
44 printf("No enough memory for %lu bytes.\n", bytes*2);
48 for(i=0; i < bytes; i++)
49 str[i] = 'a' + (i % 26);
52 memcpy(str2, str, bytes);
53 memcpy(str2+(bytes/2), str, bytes);
57 printf("First String: %s\nSecond String: %s\n", str, str2);
59 fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
61 printf("Could not open file %s.\n", argv[1]);
65 rc = write(fd, str, bytes);
67 printf("Write failed!\n");
73 if (rc < 0 || st.st_size != bytes) {
74 printf("bad file %lu size first write %lu != %lu: rc %d\n",
75 st.st_ino, st.st_size, bytes, rc);
79 rc = lseek(fd, bytes / 2, SEEK_SET);
80 if (rc != bytes / 2) {
81 printf("Seek failed!\n");
85 rc = write(fd, str, bytes);
87 printf("Write failed!\n");
92 if (rc < 0 || st.st_size != bytes + bytes / 2) {
93 printf("bad file %lu size second write %lu != %lu: rc %d\n",
94 st.st_ino, st.st_size, bytes, rc);
98 rc = lseek(fd, 0, SEEK_SET);
100 printf("Seek failed!\n");
104 rc = read(fd, readbuf, bytes * 2);
106 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
110 printf("%s\n%s\n", readbuf, str2);
112 if (rc < 0 || st.st_size != bytes + bytes / 2) {
113 printf("bad file size after read %lu != %lu: rc %d\n",
114 st.st_size, bytes + bytes / 2, rc);
126 printf("%s\n%s\n", readbuf, str2);
127 if (strcmp(readbuf, str2)) {
128 printf("No match!\n");