FDD-book/examples/part-02/ch10-handling-io-efficiently/userland/mmap_myfirst.c
Abdelkader Boudih 127eb3863c refactor: adopt current style(9) license header order (copyright before SPDX)
Update all example sources, man pages, and chapter code blocks to the
current FreeBSD header convention: plain /* opening, copyright lines
first, SPDX-License-Identifier after them. Add missing headers to
ch21/ch22 example files and missing copyright lines in ch08/ch16/ch36.
Rewrite chapter 37 prose (en, pt_BR, es_ES, zh_CN) and the submission
checklist, which previously taught the /*- marker and SPDX-first order.
2026-08-26 02:32:55 +01:00

45 lines
993 B
C

/*
* Copyright (c) 2026 Edson Brandi
*
* SPDX-License-Identifier: BSD-2-Clause
*
* mmap_myfirst.c: read-only mmap tester for /dev/myfirst (Stage 5).
*
* Maps the cbuf's backing memory and dumps the first 64 bytes. The
* raw bytes do not necessarily match a sequential read; the cbuf
* lives at unspecified positions inside the mapping. Use this tool
* mainly to confirm that the mapping itself works.
*/
#include <sys/mman.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define DEVPATH "/dev/myfirst"
#define MAPSIZE 4096
int
main(void)
{
int fd = open(DEVPATH, O_RDONLY);
if (fd < 0)
err(1, "open %s", DEVPATH);
char *map = mmap(NULL, MAPSIZE, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
err(1, "mmap");
printf("first 64 bytes:\n");
for (int i = 0; i < 64; i++) {
printf(" %02x", (unsigned char)map[i]);
if ((i + 1) % 16 == 0)
putchar('\n');
}
munmap(map, MAPSIZE);
close(fd);
return (0);
}