FDD-book/examples/part-04/ch18-writing-pci-driver/labs/myfirst_test.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

43 lines
800 B
C

/*
* myfirst_test.c -- Chapter 18 user-space smoke test.
*
* Opens /dev/myfirst0, reads up to 64 bytes, writes 16 bytes, and
* closes the descriptor. Exits non-zero on any failure.
*
* Build:
* cc -o myfirst_test myfirst_test.c
*/
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
int
main(void)
{
int fd;
ssize_t n;
char rbuf[64];
char wbuf[16] = "hello-ch18\n";
fd = open("/dev/myfirst0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open: %s\n", strerror(errno));
return (1);
}
n = read(fd, rbuf, sizeof(rbuf));
printf("read returned %zd\n", n);
n = write(fd, wbuf, sizeof(wbuf));
printf("write returned %zd\n", n);
if (close(fd) < 0) {
fprintf(stderr, "close: %s\n", strerror(errno));
return (1);
}
return (0);
}