xref: /csrg-svn/lib/libc/gen/ttyname.c (revision 51181)
11994Swnj /*
235450Sbostic  * Copyright (c) 1988 The Regents of the University of California.
335450Sbostic  * All rights reserved.
435450Sbostic  *
542627Sbostic  * %sccs.include.redist.c%
61994Swnj  */
71994Swnj 
835450Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*51181Sbostic static char sccsid[] = "@(#)ttyname.c	5.12 (Berkeley) 09/25/91";
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>
1846597Sdonn #include <unistd.h>
1939113Sbostic #include <paths.h>
201994Swnj 
2146338Sbostic static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
2246338Sbostic 
231994Swnj char *
2435450Sbostic ttyname(fd)
2535450Sbostic 	int fd;
261994Swnj {
2746338Sbostic 	struct stat sb;
2835450Sbostic 	struct sgttyb ttyb;
2947051Sbostic 	DB *db;
3047051Sbostic 	DBT data, key;
3149247Sbostic 	struct {
3249247Sbostic 		mode_t type;
3349247Sbostic 		dev_t dev;
3449247Sbostic 	} bkey;
3546338Sbostic 	static char *__oldttyname();
361994Swnj 
3746338Sbostic 	/* Must be a terminal. */
3835450Sbostic 	if (ioctl(fd, TIOCGETP, &ttyb) < 0)
391994Swnj 		return(NULL);
4046338Sbostic 	/* Must be a character device. */
4146338Sbostic 	if (fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
421994Swnj 		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);
52*51181Sbostic 			(void)(db->close)(db);
5347051Sbostic 			return(buf);
5447051Sbostic 		}
55*51181Sbostic 		(void)(db->close)(db);
5647051Sbostic 	}
5747051Sbostic 	return(__oldttyname(fd, &sb));
5846338Sbostic }
5946338Sbostic 
6046338Sbostic static char *
6146338Sbostic __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 	char *rval, *strcpy();
6946338Sbostic 
7039113Sbostic 	if ((dp = opendir(_PATH_DEV)) == NULL)
711994Swnj 		return(NULL);
7246338Sbostic 
7335450Sbostic 	for (rval = NULL; 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);
8235450Sbostic 		rval = buf;
8335450Sbostic 		break;
841994Swnj 	}
8546338Sbostic 	(void)closedir(dp);
8635450Sbostic 	return(rval);
871994Swnj }
88