xref: /original-bsd/lib/libc/gen/ttyname.c (revision 62734ea8)
1 /* @(#)ttyname.c	4.3 (Berkeley) 05/07/82 */
2 /*
3  * ttyname(f): return "/dev/ttyXX" which the the name of the
4  * tty belonging to file f.
5  *  NULL if it is not a tty
6  */
7 
8 #define	NULL	0
9 #include <sys/param.h>
10 #include <sys/dir.h>
11 #include <sys/stat.h>
12 
13 static	char	dev[]	= "/dev/";
14 char	*strcpy();
15 char	*strcat();
16 
17 char *
18 ttyname(f)
19 {
20 	struct stat fsb;
21 	struct stat tsb;
22 	register struct direct *db;
23 	register DIR *df;
24 	static char rbuf[32];
25 
26 	if (isatty(f)==0)
27 		return(NULL);
28 	if (fstat(f, &fsb) < 0)
29 		return(NULL);
30 	if ((fsb.st_mode&S_IFMT) != S_IFCHR)
31 		return(NULL);
32 	if ((df = opendir(dev)) == NULL)
33 		return(NULL);
34 	while ((db = readdir(df)) != NULL) {
35 		if (db->d_ino != fsb.st_ino)
36 			continue;
37 		strcpy(rbuf, dev);
38 		strcat(rbuf, db->d_name);
39 		if (stat(rbuf, &tsb) < 0)
40 			continue;
41 		if (tsb.st_dev == fsb.st_dev && tsb.st_ino == fsb.st_ino) {
42 			closedir(df);
43 			return(rbuf);
44 		}
45 	}
46 	closedir(df);
47 	return(NULL);
48 }
49