xref: /freebsd/tools/regression/nfsmmap/test1/test1.c (revision 52267f7411adcc76ede961420e08c0e42f42d415)
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("test1.data");
17     fd = open("test1.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("test1.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     fsync(fd);
35 
36     addr = mmap(0, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
37     if (addr == MAP_FAILED)
38 	err(1, "mapping");
39     unlink("test1.scratch");
40     fd2 = open("test1.scratch", O_RDWR|O_CREAT, 0666);
41     if (fd2 < 0)
42 	err(1, "creating scratch");
43 
44     if (write(fd2, addr, 4096) < 0)
45 	err(1, "writing scratch");
46 }
47