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 (unsigned long)st.st_ino, (unsigned long)st.st_size,
80 rc = lseek(fd, bytes / 2, SEEK_SET);
81 if (rc != bytes / 2) {
82 printf("Seek failed!\n");
86 rc = write(fd, str, bytes);
88 printf("Write failed!\n");
93 if (rc < 0 || st.st_size != bytes + bytes / 2) {
94 printf("bad file %lu size second write %lu != %lu: rc %d\n",
95 (unsigned long)st.st_ino, (unsigned long)st.st_size,
100 rc = lseek(fd, 0, SEEK_SET);
102 printf("Seek failed!\n");
106 rc = read(fd, readbuf, bytes * 2);
108 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
112 printf("%s\n%s\n", readbuf, str2);
114 if (rc < 0 || st.st_size != bytes + bytes / 2) {
115 printf("bad file size after read %lu != %lu: rc %d\n",
116 (unsigned long)st.st_size, bytes + bytes / 2,
129 printf("%s\n%s\n", readbuf, str2);
130 if (strcmp(readbuf, str2)) {
131 printf("No match!\n");