FDD-book/examples/part-02/ch08-working-with-device-files/userland/probe_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

46 lines
919 B
C

/*
* Copyright (c) 2026 Edson Brandi
*
* SPDX-License-Identifier: BSD-2-Clause
*
* probe_myfirst: a tiny userland probe for the Chapter 8 myfirst driver.
*
* Usage:
* probe_myfirst [path]
*
* With no argument, opens /dev/myfirst/0. With an argument, opens the
* given path. Performs one read(2) into a fixed buffer, reports the
* number of bytes, closes, and exits.
*
* Compile:
* cc -Wall -Werror -o probe_myfirst probe_myfirst.c
*/
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
const char *path = (argc > 1) ? argv[1] : "/dev/myfirst/0";
char buf[64];
ssize_t n;
int fd;
fd = open(path, O_RDWR);
if (fd < 0)
err(1, "open %s", path);
n = read(fd, buf, sizeof(buf));
if (n < 0)
err(1, "read %s", path);
printf("read %zd bytes from %s\n", n, path);
if (close(fd) != 0)
err(1, "close %s", path);
return (0);
}