FDD-book/examples/part-01/ch04-a-first-look-at-the-c-programming-language/final-practice/lab3-bounded-device-names/test_devname.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

32 lines
861 B
C

#include <stdio.h>
#include "devname.h"
/*
* Table-driven tests: we try several cases and print both code and result.
* Practise reading return codes and not assuming success.
*/
struct case_ { const char *pref; int unit; size_t cap; };
int
main(void)
{
char buf[8]; /* small to force you to think about capacity */
struct case_ cases[] = {
{"ttyu", 0, sizeof(buf)}, /* fits */
{"ttyv", 12, sizeof(buf)}, /* just fits or close */
{"", 1, sizeof(buf)}, /* invalid prefix */
};
for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
int rc = build_devname(buf, cases[i].cap,
cases[i].pref, cases[i].unit);
printf("case %u -> rc=%d, buf='%s'\n",
i, rc, (rc == DN_OK) ? buf : "<invalid>");
}
printf("valid? 'ttyu0'=%d, 'u0tty'=%d\n",
is_valid_devname("ttyu0"),
is_valid_devname("u0tty"));
return (0);
}