xref: /csrg-svn/lib/libc/gen/ttyname.c (revision 42627)
11994Swnj /*
235450Sbostic  * Copyright (c) 1988 The Regents of the University of California.
335450Sbostic  * All rights reserved.
435450Sbostic  *
5*42627Sbostic  * %sccs.include.redist.c%
61994Swnj  */
71994Swnj 
835450Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*42627Sbostic static char sccsid[] = "@(#)ttyname.c	5.6 (Berkeley) 06/01/90";
1035450Sbostic #endif /* LIBC_SCCS and not lint */
1135450Sbostic 
1235450Sbostic #include <sys/types.h>
136715Smckusick #include <sys/dir.h>
141994Swnj #include <sys/stat.h>
1535450Sbostic #include <sgtty.h>
1639113Sbostic #include <paths.h>
171994Swnj 
181994Swnj char *
1935450Sbostic ttyname(fd)
2035450Sbostic 	int fd;
211994Swnj {
2235450Sbostic 	register struct direct *dirp;
2335450Sbostic 	register DIR *dp;
2435450Sbostic 	struct stat sb1, sb2;
2535450Sbostic 	struct sgttyb ttyb;
2639113Sbostic 	static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
2735450Sbostic 	char *rval, *strcpy();
281994Swnj 
2935450Sbostic 	if (ioctl(fd, TIOCGETP, &ttyb) < 0)
301994Swnj 		return(NULL);
3135450Sbostic 	if (fstat(fd, &sb1) < 0 || (sb1.st_mode&S_IFMT) != S_IFCHR)
321994Swnj 		return(NULL);
3339113Sbostic 	if ((dp = opendir(_PATH_DEV)) == NULL)
341994Swnj 		return(NULL);
3535450Sbostic 	for (rval = NULL; dirp = readdir(dp);) {
3635450Sbostic 		if (dirp->d_ino != sb1.st_ino)
371994Swnj 			continue;
3839113Sbostic 		(void)strcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name);
3935450Sbostic 		if (stat(buf, &sb2) < 0 || sb1.st_dev != sb2.st_dev ||
4040203Sbostic 		    sb1.st_ino != sb2.st_ino)
411994Swnj 			continue;
4235450Sbostic 		closedir(dp);
4335450Sbostic 		rval = buf;
4435450Sbostic 		break;
451994Swnj 	}
4635450Sbostic 	closedir(dp);
4735450Sbostic 	return(rval);
481994Swnj }
49