DEVNAME(3)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’… DEVNAME(3) β€” the girlies' guide to getting device names

okay so bestie, welcome to section 3 of the Library Functions Manual β€” we're in libc land today and honestly? it's a vibe. πŸŽ€


✨ NAME ✨

devname β€” get device name. that's it. that's the whole name section. short and iconic. πŸ’–

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) β€” yeah she lives in the standard C library, no exotic dependencies, we love a low-maintenance queen. 🫢

🌸 SYNOPSIS 🌸

#include <sys/stat.h>
#include <stdlib.h>

char *
devname(dev_t dev, mode_t type);

char *
devname_r(dev_t dev, mode_t type, char *buf, int len);

char *
fdevname(int fd);

char *
fdevname_r(int fd, char *buf, int len);

πŸ’« DESCRIPTION πŸ’«

so devname() hands you back a pointer to the name of the block or character device sitting in /dev β€” the one with device number dev, and a file type matching whatever's encoded in type. and heads up, type has GOT to be one of S_IFBLK or S_IFCHR, no exceptions, we don't do chaos here. 😌

the way it finds the right name? she asks the kernel directly via the kern.devname sysctl. like, respectfully, she goes straight to the source. πŸ”₯ but if she can't come up with a suitable name, she'll just format the info encapsulated in dev and type into a human-readable format β€” a little fallback moment, still serving looks. πŸ’…

then we have fdevname() and fdevname_r(), which get the device name directly from a file descriptor pointing to a character device. no middleman, no sysctl errand, just vibes. ✨ but if they can't come up with a suitable name? they return a NULL pointer. ngl, a little brutal, but that's the deal. πŸ’€

the static buffer situation (iykyk, this is the important part): devname() and fdevname() return the name stored in a static buffer β€” which gets overwritten on subsequent calls. so if you call twice and keep the first pointer, bestie, that pointer is now lying to you. 😭 devname_r() and fdevname_r() take a buffer and length as arguments specifically to avoid this exact problem. give them your own buffer and you're golden. πŸ‘‘

lowkey the theme of this whole page: non-_r versions = static buffer = don't trust it past the next call. _r versions = your buffer, your rules. πŸ¦‹

πŸ“ EXAMPLES πŸ“

int fd;
struct stat buf;
char *name;

        fd = open("/dev/tun");
        fstat(fd, &buf);
        printf("devname is /dev/%s\n", devname(buf.st_rdev, S_IFCHR));
        printf("fdevname is /dev/%s\n", fdevname(fd));

look at her go β€” opens /dev/tun, stats it, prints the name both ways. devname() with the device number and S_IFCHR, and fdevname() straight off the fd. two paths, same destination, main character energy honestly. 🧚

πŸ‘€ SEE ALSO πŸ‘€

  • stat(2) β€” go read it, it's her bestie. 🌈

⭐ HISTORY ⭐

devname() appeared in 4.4BSD β€” vintage, iconic, ahead of her time. πŸ’œ fdevname() appeared in FreeBSD 8.0 β€” the newer sister who joined the group chat later. 🌸


k that's the whole page bestie, no cap, every flag and fact accounted for. πŸŽ€ this one's from Debian, dated February 22, 2005 β€” mwah, thanks for reading, slay, periodt. πŸ’–βœ¨

πŸ“¦ where this page comes from
  • freebsd 15.1: base 15.1
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms