xref: /csrg-svn/lib/libc/gen/ttyname.c (revision 65922)
11994Swnj /*
261111Sbostic  * Copyright (c) 1988, 1993
361111Sbostic  *	The Regents of the University of California.  All rights reserved.
435450Sbostic  *
542627Sbostic  * %sccs.include.redist.c%
61994Swnj  */
71994Swnj 
835450Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*65922Sbostic static char sccsid[] = "@(#)ttyname.c	8.2 (Berkeley) 01/27/94";
1035450Sbostic #endif /* LIBC_SCCS and not lint */
1135450Sbostic 
1235450Sbostic #include <sys/types.h>
131994Swnj #include <sys/stat.h>
1446338Sbostic #include <fcntl.h>
1546338Sbostic #include <dirent.h>
1635450Sbostic #include <sgtty.h>
1747051Sbostic #include <db.h>
1851432Sbostic #include <string.h>
1939113Sbostic #include <paths.h>
201994Swnj 
2146338Sbostic static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
2251432Sbostic static char *oldttyname __P((int, struct stat *));
2346338Sbostic 
241994Swnj char *
ttyname(fd)2535450Sbostic ttyname(fd)
2635450Sbostic 	int fd;
271994Swnj {
2846338Sbostic 	struct stat sb;
2935450Sbostic 	struct sgttyb ttyb;
3047051Sbostic 	DB *db;
3147051Sbostic 	DBT data, key;
3249247Sbostic 	struct {
3349247Sbostic 		mode_t type;
3449247Sbostic 		dev_t dev;
3549247Sbostic 	} bkey;
361994Swnj 
3746338Sbostic 	/* Must be a terminal. */
3835450Sbostic 	if (ioctl(fd, TIOCGETP, &ttyb) < 0)
3951432Sbostic 		return (NULL);
4046338Sbostic 	/* Must be a character device. */
4146338Sbostic 	if (fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
4251432Sbostic 		return (NULL);
4346338Sbostic 
4451127Sbostic 	if (db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL)) {
45*65922Sbostic 		memset(&bkey, 0, sizeof(bkey));
4649247Sbostic 		bkey.type = S_IFCHR;
4749247Sbostic 		bkey.dev = sb.st_rdev;
4849247Sbostic 		key.data = &bkey;
4949247Sbostic 		key.size = sizeof(bkey);
5047051Sbostic 		if (!(db->get)(db, &key, &data, 0)) {
5147051Sbostic 			bcopy(data.data,
5247051Sbostic 			    buf + sizeof(_PATH_DEV) - 1, data.size);
5351181Sbostic 			(void)(db->close)(db);
5451432Sbostic 			return (buf);
5547051Sbostic 		}
5651181Sbostic 		(void)(db->close)(db);
5747051Sbostic 	}
5851432Sbostic 	return (oldttyname(fd, &sb));
5946338Sbostic }
6046338Sbostic 
6146338Sbostic static char *
oldttyname(fd,sb)6251432Sbostic oldttyname(fd, sb)
6346338Sbostic 	int fd;
6446338Sbostic 	struct stat *sb;
6546338Sbostic {
6646338Sbostic 	register struct dirent *dirp;
6746338Sbostic 	register DIR *dp;
6846338Sbostic 	struct stat dsb;
6946338Sbostic 
7039113Sbostic 	if ((dp = opendir(_PATH_DEV)) == NULL)
7151432Sbostic 		return (NULL);
7246338Sbostic 
7351375Smckusick 	while (dirp = readdir(dp)) {
7446338Sbostic 		if (dirp->d_fileno != sb->st_ino)
751994Swnj 			continue;
7646338Sbostic 		bcopy(dirp->d_name, buf + sizeof(_PATH_DEV) - 1,
7746338Sbostic 		    dirp->d_namlen + 1);
7846338Sbostic 		if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev ||
7946338Sbostic 		    sb->st_ino != dsb.st_ino)
801994Swnj 			continue;
8146338Sbostic 		(void)closedir(dp);
8251432Sbostic 		return (buf);
831994Swnj 	}
8446338Sbostic 	(void)closedir(dp);
8551432Sbostic 	return (NULL);
861994Swnj }
87