DEVNAME(3)
π 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-_rversions = static buffer = don't trust it past the next call._rversions = 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
- Copyright (c) 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms