diff options
author | ed <ed@FreeBSD.org> | 2009-02-11 20:24:59 +0000 |
---|---|---|
committer | ed <ed@FreeBSD.org> | 2009-02-11 20:24:59 +0000 |
commit | de78bbbfe832781ef4718e4d175d53fbe4e7ac40 (patch) | |
tree | 0b13c353e44fe54b12f0be5c1ae687436add79ee /lib/libc/gen/devname.3 | |
parent | c1e92bfb206adffe9aca701014c9028722d1ed02 (diff) | |
download | FreeBSD-src-de78bbbfe832781ef4718e4d175d53fbe4e7ac40.zip FreeBSD-src-de78bbbfe832781ef4718e4d175d53fbe4e7ac40.tar.gz |
Add two new routines: fdevname() and fdevname_r().
A more elegant way of obtaining a name of a character device by its file
descriptor on FreeBSD, is to use the FIODGNAME ioctl. Because a valid
file descriptor implies a file descriptor is visible in /dev, it will
always resolve a valid device name.
I'm adding a more friendly wrapper for this ioctl, called fdevname(). It
is a lot easier to use than devname() and also has better error
handling. When a device name cannot be resolved, it will just return
NULL instead of a generated device name that makes no sense.
Discussed with: kib
Diffstat (limited to 'lib/libc/gen/devname.3')
-rw-r--r-- | lib/libc/gen/devname.3 | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/lib/libc/gen/devname.3 b/lib/libc/gen/devname.3 index c05d0ba..7ff31c7 100644 --- a/lib/libc/gen/devname.3 +++ b/lib/libc/gen/devname.3 @@ -43,6 +43,10 @@ .Fn devname "dev_t dev" "mode_t type" .Ft char * .Fn devname_r "dev_t dev" "mode_t type" "char *buf" "int len" +.Ft char * +.Fn fdevname "int fd" +.Ft char * +.Fn fdevname_r "int fd" "char *buf" "int len" .Sh DESCRIPTION The .Fn devname @@ -69,11 +73,24 @@ and .Fa type in a human-readable format. .Pp +The +.Fn fdevname +and +.Fn fdevname_r +function obtains the device name directly from a file descriptor +pointing to a character device. +If it is unable to come up with a suitable name, these functions will +return a NULL pointer. +.Pp .Fn devname -returns the name stored in a static buffer which will be overwritten +and +.Fn fdevname +return the name stored in a static buffer which will be overwritten on subsequent calls. .Fn devname_r -takes a buffer and length as argument to avoid this problem. +and +.Fn fdevname_r +take a buffer and length as argument to avoid this problem. .Sh EXAMPLES .Bd -literal -compact int fd; @@ -83,6 +100,7 @@ char *name; fd = open("/dev/tun"); fstat(fd, &buf); printf("devname is /dev/%s\en", devname(buf.st_rdev, S_IFCHR)); + printf("fdevname is /dev/%s\en", fdevname(fd)); .Ed .Sh SEE ALSO .Xr stat 2 @@ -91,3 +109,7 @@ The .Fn devname function appeared in .Bx 4.4 . +The +.Fn fdevname +function appeared in +.Fx 8.0 . |