1 #include <sys/types.h> 2 #include <sys/fcntl.h> 3 #include <sys/mman.h> 4 #include <unistd.h> 5 6 int main(int argc, char** argv) 7 { 8 int fd, fd2; 9 caddr_t addr; 10 char zeros[4096]; 11 char ones[200]; 12 13 memset(zeros, 0, sizeof zeros); 14 memset(ones, 1, sizeof ones); 15 #if 0 16 unlink("test2.data"); 17 fd = open("test2.data", O_RDWR|O_CREAT, 0666); 18 if (fd < 0) 19 err(1, "creating file"); 20 if (write(fd, zeros, sizeof zeros) < 0) 21 err(1, "writing zeros"); 22 close(fd); 23 #endif 24 25 fd = open("test2.data", O_RDWR); 26 if (fd < 0) 27 err(1, "opening file"); 28 if (lseek(fd, 600, SEEK_SET) < 0) 29 err(1, "seeking"); 30 31 if (write(fd, ones, sizeof ones) < 0) 32 err(1, "writing ones"); 33 34 addr = mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); 35 if (addr == MAP_FAILED) 36 err(1, "mapping"); 37 unlink("test2.scratch"); 38 fd2 = open("test2.scratch", O_RDWR|O_CREAT, 0666); 39 if (fd2 < 0) 40 err(1, "creating scratch"); 41 42 if (write(fd2, addr, 4096) < 0) 43 err(1, "writing scratch"); 44 } 45