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

43 lines
956 B
C

/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* stat_myfirst: print stat(2) metadata for one or more device-file
* paths. Useful to confirm that a primary node and its alias resolve
* to the same cdev (same rdev) and that the advertised mode and
* ownership match what the driver asked for.
*
* Usage:
* stat_myfirst path [path ...]
*
* Compile:
* cc -Wall -Werror -o stat_myfirst stat_myfirst.c
*/
#include <err.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int
main(int argc, char **argv)
{
struct stat sb;
int i;
if (argc < 2) {
fprintf(stderr, "usage: %s path [path ...]\n", argv[0]);
return (1);
}
for (i = 1; i < argc; i++) {
if (stat(argv[i], &sb) != 0)
err(1, "stat %s", argv[i]);
printf("%s: mode=%06o uid=%u gid=%u rdev=%#jx\n",
argv[i],
(unsigned)(sb.st_mode & 07777),
(unsigned)sb.st_uid,
(unsigned)sb.st_gid,
(uintmax_t)sb.st_rdev);
}
return (0);
}