Date: Fri, 20 Jun 2003 13:32:00 +0200 From: Poul-Henning Kamp <phk@phk.freebsd.dk> To: current@freebsd.org Subject: ttyname(3) review requested... Message-ID: <8915.1056108720@critter.freebsd.dk>
next in thread | raw e-mail | index | archive | help
I'm not up to speed on the per thread magic storage thing, so I would appreciate if somebody could review & test this patch: Index: gen/ttyname.c =================================================================== RCS file: /home/ncvs/src/lib/libc/gen/ttyname.c,v retrieving revision 1.12 diff -u -r1.12 ttyname.c --- gen/ttyname.c 1 Feb 2002 01:32:19 -0000 1.12 +++ gen/ttyname.c 20 Jun 2003 09:44:28 -0000 @@ -50,7 +50,6 @@ #include <pthread.h> #include "un-namespace.h" -#include <db.h> #include "libc_private.h" static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV; @@ -77,8 +76,6 @@ char * ttyname_r(int fd, char *buf, size_t len) { - struct dirent *dirp; - DIR *dp; struct stat dsb; struct stat sb; char *rval; @@ -96,23 +93,7 @@ if (len <= sizeof(_PATH_DEV)) return (rval); - if ((dp = opendir(_PATH_DEV)) != NULL) { - memcpy(buf, _PATH_DEV, sizeof(_PATH_DEV)); - for (rval = NULL; (dirp = readdir(dp)) != NULL;) { - if (dirp->d_fileno != sb.st_ino) - continue; - minlen = (len - (sizeof(_PATH_DEV) - 1)) < (dirp->d_namlen + 1) ? - (len - (sizeof(_PATH_DEV) - 1)) : (dirp->d_namlen + 1); - memcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name, minlen); - if (stat(buf, &dsb) || sb.st_dev != dsb.st_dev || - sb.st_ino != dsb.st_ino) - continue; - rval = buf; - break; - } - (void) closedir(dp); - } - return (rval); + return(devname_r(sb.st_rdev, S_IFCHR, buf, sizeof(buf))); } static char * @@ -151,12 +132,6 @@ { struct stat sb; struct termios ttyb; - DB *db; - DBT data, key; - struct { - mode_t type; - dev_t dev; - } bkey; /* Must be a terminal. */ if (tcgetattr(fd, &ttyb) < 0) @@ -165,44 +140,5 @@ if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode)) return (NULL); - if ( (db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL)) ) { - memset(&bkey, 0, sizeof(bkey)); - bkey.type = S_IFCHR; - bkey.dev = sb.st_rdev; - key.data = &bkey; - key.size = sizeof(bkey); - if (!(db->get)(db, &key, &data, 0)) { - bcopy(data.data, - buf + sizeof(_PATH_DEV) - 1, data.size); - (void)(db->close)(db); - return (buf); - } - (void)(db->close)(db); - } - return (oldttyname(fd, &sb)); -} - -static char * -oldttyname(int fd, struct stat *sb) -{ - struct dirent *dirp; - struct stat dsb; - DIR *dp; - - if ((dp = opendir(_PATH_DEV)) == NULL) - return (NULL); - - while ( (dirp = readdir(dp)) ) { - if (dirp->d_fileno != sb->st_ino) - continue; - bcopy(dirp->d_name, buf + sizeof(_PATH_DEV) - 1, - dirp->d_namlen + 1); - if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev || - sb->st_ino != dsb.st_ino) - continue; - (void)closedir(dp); - return (buf); - } - (void)closedir(dp); - return (NULL); + return(devname_r(sb.st_rdev, S_IFCHR, buf, sizeof(buf))); } -- Poul-Henning Kamp | UNIX since Zilog Zeus 3.20 phk@FreeBSD.ORG | TCP/IP since RFC 956 FreeBSD committer | BSD since 4.3-tahoe Never attribute to malice what can adequately be explained by incompetence.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?8915.1056108720>