11994Swnj /* 235450Sbostic * Copyright (c) 1988 The Regents of the University of California. 335450Sbostic * All rights reserved. 435450Sbostic * 535450Sbostic * Redistribution and use in source and binary forms are permitted 635450Sbostic * provided that the above copyright notice and this paragraph are 735450Sbostic * duplicated in all such forms and that any documentation, 835450Sbostic * advertising materials, and other materials related to such 935450Sbostic * distribution and use acknowledge that the software was developed 1035450Sbostic * by the University of California, Berkeley. The name of the 1135450Sbostic * University may not be used to endorse or promote products derived 1235450Sbostic * from this software without specific prior written permission. 1335450Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1435450Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1535450Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 161994Swnj */ 171994Swnj 1835450Sbostic #if defined(LIBC_SCCS) && !defined(lint) 19*39113Sbostic static char sccsid[] = "@(#)ttyname.c 5.4 (Berkeley) 09/11/89"; 2035450Sbostic #endif /* LIBC_SCCS and not lint */ 2135450Sbostic 2235450Sbostic #include <sys/types.h> 236715Smckusick #include <sys/dir.h> 241994Swnj #include <sys/stat.h> 2535450Sbostic #include <sgtty.h> 26*39113Sbostic #include <paths.h> 271994Swnj 281994Swnj char * 2935450Sbostic ttyname(fd) 3035450Sbostic int fd; 311994Swnj { 3235450Sbostic register struct direct *dirp; 3335450Sbostic register DIR *dp; 3435450Sbostic struct stat sb1, sb2; 3535450Sbostic struct sgttyb ttyb; 36*39113Sbostic static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV; 3735450Sbostic char *rval, *strcpy(); 381994Swnj 3935450Sbostic if (ioctl(fd, TIOCGETP, &ttyb) < 0) 401994Swnj return(NULL); 4135450Sbostic if (fstat(fd, &sb1) < 0 || (sb1.st_mode&S_IFMT) != S_IFCHR) 421994Swnj return(NULL); 43*39113Sbostic if ((dp = opendir(_PATH_DEV)) == NULL) 441994Swnj return(NULL); 4535450Sbostic for (rval = NULL; dirp = readdir(dp);) { 4635450Sbostic if (dirp->d_ino != sb1.st_ino) 471994Swnj continue; 48*39113Sbostic (void)strcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name); 4935450Sbostic if (stat(buf, &sb2) < 0 || sb1.st_dev != sb2.st_dev || 5035450Sbostic sb1.st_ino != sb1.st_ino) 511994Swnj continue; 5235450Sbostic closedir(dp); 5335450Sbostic rval = buf; 5435450Sbostic break; 551994Swnj } 5635450Sbostic closedir(dp); 5735450Sbostic return(rval); 581994Swnj } 59