FDD-book/examples/part-02/ch08-working-with-device-files/userland/probe_myfirst.c
Edson Brandi 6e6dac94fe Add book companion files under examples/
Organize companion code and materials by book part:

  part-01/      Foundations
  part-02/      Kernel & driver fundamentals
  part-03/      Character & block devices
  part-04/      Networking & storage
  part-05/      Buses & advanced topics
  part-06/      Debugging & performance
  part-07/      Real-world drivers
  appendices/   Supplementary material
  README.md     Overview and build instructions
2026-04-25 16:51:00 +00:00

44 lines
882 B
C

/*-
* 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);
}