Bell 32V development
[unix-history] / usr / src / libc / gen / ttyname.c
CommitLineData
1d869915
TL
1/*
2 * ttyname(f): return "/dev/ttyXX" which the the name of the
3 * tty belonging to file f.
4 * NULL if it is not a tty
5 */
6
7#define NULL 0
8#include <sys/types.h>
9#include <sys/dir.h>
10#include <sys/stat.h>
11
12static char dev[] = "/dev/";
13char *strcpy();
14char *strcat();
15
16char *
17ttyname(f)
18{
19 struct stat fsb;
20 struct stat tsb;
21 struct direct db;
22 static char rbuf[32];
23 register df;
24
25 if (isatty(f)==0)
26 return(NULL);
27 if (fstat(f, &fsb) < 0)
28 return(NULL);
29 if ((fsb.st_mode&S_IFMT) != S_IFCHR)
30 return(NULL);
31 if ((df = open(dev, 0)) < 0)
32 return(NULL);
33 while (read(df, (char *)&db, sizeof(db)) == sizeof(db)) {
34 if (db.d_ino == 0)
35 continue;
36 if (db.d_ino != fsb.st_ino)
37 continue;
38 strcpy(rbuf, dev);
39 strcat(rbuf, db.d_name);
40 if (stat(rbuf, &tsb) < 0)
41 continue;
42 if (tsb.st_dev==fsb.st_dev && tsb.st_ino==fsb.st_ino) {
43 close(df);
44 return(rbuf);
45 }
46 }
47 close(df);
48 return(NULL);
49}