DEVNAME(3)

NAME
devname, devname_r, fdevname, fdevname_r - get device name

LIBRARY
libc

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, size_t len);
char *
fdevname(int fd);
int
fdevname_r(int fd, char *buf, size_t len);

DESCRIPTION
The devname() and devname_r() functions return a pointer to the name of the block or character device in /dev with a device number of dev, and a file type matching the one encoded in type which must be one of S_IFBLK or S_IFCHR. To find the right name, devname() and devname_r() asks the kernel via the kern.devname sysctl. If it fails, it will format the information encapsulated in dev and type in a human-readable format.

The fdevname() and fdevname_r() function obtain the device name directly from a file descriptor pointing to a character device.

devname() and fdevname() returns a pointer to an internal static object; thus, subsequent calls will modify the same buffer. devname_r() and fdevname_r() avoid this problem by taking a buffer buf and a buffer length len as arguments.

RETURN VALUES
The devname(), devname_r() and fdevname() functions return a pointer to the name of the block or character device in /dev if successful; otherwise NULL is returned. If fdevname() fails, errno is set to indicate the error.

The fdevname_r() function returns 0 if successful. Otherwise an error number is returned.

EXAMPLES
The following code fragment determines the name of the cloned tun(4) device that is created by opening /dev/tun.

    int fd;
    struct stat st;
    fd = open("/dev/tun", O_RDONLY);
    fstat(fd, &st);
    printf("devname is /dev/%s\n", devname(st.st_rdev, S_IFCHR));
    printf("fdevname is /dev/%s\n", fdevname(fd));
    close(fd);

ERRORS
The fdevname() and fdevname_r() functions may fail and return the following error codes:

[EBADF]
    The fd is not a valid open file descriptor.

[EINVAL]
    The fd must belong to a character device.

The fdevname_r() function may fail and return the following error code:

[ERANGE]
    The len argument is smaller than the length of the string to be returned.

SEE ALSO
stat(2), devfs(5)

HISTORY
The devname() function appeared in 4.4BSD.

The devname_r() function appeared in DragonFly 1.0 and the fdevname() and fdevname_r() functions appeared in DragonFly 2.3.