xref: /freebsd/tools/test/stress2/tools/serial.c (revision a91a2465)
1 /* Fill a file with a sequence of byte values from 0 - 0xff */
2 
3 #include <sys/param.h>
4 #include <sys/mman.h>
5 #include <err.h>
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 
11 int
12 main(int argc, char *argv[])
13 {
14 	size_t i, size;
15 	int fd;
16 	char *cp, *file;
17 
18 	if (argc != 3) {
19 		fprintf(stderr, "Usage: %s <file> <file length in bytes>\n", argv[0]);
20 		exit(1);
21 	}
22 	file = argv[1];
23 	size = atol(argv[2]);
24 
25 	if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
26 		err(1, "%s", file);
27 
28 	if (lseek(fd, size - 1, SEEK_SET) == -1)
29 		err(1, "lseek error");
30 
31 	/* write a dummy byte at the last location */
32 	if (write(fd, "\0", 1) != 1)
33 		err(1, "write error");
34 
35 	if ((cp = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
36 		err(1, "mmap()");
37 
38 	for (i = 0; i < size; i++)
39 		cp[i] = i & 0xff;
40 
41 	if (munmap(cp, size) == -1)
42 		err(1, "munmap");
43 	close(fd);
44 }
45