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/ttyname.c | |
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/ttyname.c')
-rw-r--r-- | lib/libc/gen/ttyname.c | 14 |
1 files changed, 2 insertions, 12 deletions
diff --git a/lib/libc/gen/ttyname.c b/lib/libc/gen/ttyname.c index 6543604..a21b77f 100644 --- a/lib/libc/gen/ttyname.c +++ b/lib/libc/gen/ttyname.c @@ -35,7 +35,6 @@ __FBSDID("$FreeBSD$"); #include "namespace.h" #include <sys/types.h> -#include <sys/stat.h> #include <sys/ioctl.h> #include <sys/filio.h> #include <fcntl.h> @@ -60,8 +59,6 @@ static int ttyname_keycreated = 0; int ttyname_r(int fd, char *buf, size_t len) { - struct stat sb; - struct fiodgname_arg fgn; size_t used; *buf = '\0'; @@ -69,21 +66,14 @@ ttyname_r(int fd, char *buf, size_t len) /* Must be a terminal. */ if (!isatty(fd)) return (ENOTTY); - /* Must be a character device. */ - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode)) - return (ENOTTY); /* Must have enough room */ if (len <= sizeof(_PATH_DEV)) return (ERANGE); strcpy(buf, _PATH_DEV); used = strlen(buf); - fgn.len = len - used; - fgn.buf = buf + used; - if (!_ioctl(fd, FIODGNAME, &fgn)) - return (0); - used = strlen(buf); - devname_r(sb.st_rdev, S_IFCHR, buf + used, len - used); + if (fdevname_r(fd, buf + used, len - used) == NULL) + return (ENOTTY); return (0); } |