xref: /csrg-svn/lib/libc/gen/ttyname.c (revision 61111)
11994Swnj /*
2*61111Sbostic  * Copyright (c) 1988, 1993
3*61111Sbostic  *	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*61111Sbostic static char sccsid[] = "@(#)ttyname.c	8.1 (Berkeley) 06/04/93";
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 *
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)) {
4549247Sbostic 		bkey.type = S_IFCHR;
4649247Sbostic 		bkey.dev = sb.st_rdev;
4749247Sbostic 		key.data = &bkey;
4849247Sbostic 		key.size = sizeof(bkey);
4947051Sbostic 		if (!(db->get)(db, &key, &data, 0)) {
5047051Sbostic 			bcopy(data.data,
5147051Sbostic 			    buf + sizeof(_PATH_DEV) - 1, data.size);
5251181Sbostic 			(void)(db->close)(db);
5351432Sbostic 			return (buf);
5447051Sbostic 		}
5551181Sbostic 		(void)(db->close)(db);
5647051Sbostic 	}
5751432Sbostic 	return (oldttyname(fd, &sb));
5846338Sbostic }
5946338Sbostic 
6046338Sbostic static char *
6151432Sbostic oldttyname(fd, sb)
6246338Sbostic 	int fd;
6346338Sbostic 	struct stat *sb;
6446338Sbostic {
6546338Sbostic 	register struct dirent *dirp;
6646338Sbostic 	register DIR *dp;
6746338Sbostic 	struct stat dsb;
6846338Sbostic 
6939113Sbostic 	if ((dp = opendir(_PATH_DEV)) == NULL)
7051432Sbostic 		return (NULL);
7146338Sbostic 
7251375Smckusick 	while (dirp = readdir(dp)) {
7346338Sbostic 		if (dirp->d_fileno != sb->st_ino)
741994Swnj 			continue;
7546338Sbostic 		bcopy(dirp->d_name, buf + sizeof(_PATH_DEV) - 1,
7646338Sbostic 		    dirp->d_namlen + 1);
7746338Sbostic 		if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev ||
7846338Sbostic 		    sb->st_ino != dsb.st_ino)
791994Swnj 			continue;
8046338Sbostic 		(void)closedir(dp);
8151432Sbostic 		return (buf);
821994Swnj 	}
8346338Sbostic 	(void)closedir(dp);
8451432Sbostic 	return (NULL);
851994Swnj }
86