put reasonable header on it
[unix-history] / usr / src / lib / libc / gen / ttyname.c
CommitLineData
2ce81398
DS
1#if defined(LIBC_SCCS) && !defined(lint)
2static char sccsid[] = "@(#)ttyname.c 5.2 (Berkeley) %G%";
3#endif LIBC_SCCS and not lint
b8f253e8 4
b11ade1b
BJ
5/*
6 * ttyname(f): return "/dev/ttyXX" which the the name of the
7 * tty belonging to file f.
8 * NULL if it is not a tty
9 */
10
11#define NULL 0
164a2d2d 12#include <sys/param.h>
c9f0b4d5 13#include <sys/dir.h>
b11ade1b
BJ
14#include <sys/stat.h>
15
16static char dev[] = "/dev/";
17char *strcpy();
18char *strcat();
19
20char *
21ttyname(f)
22{
23 struct stat fsb;
24 struct stat tsb;
164a2d2d
KM
25 register struct direct *db;
26 register DIR *df;
b11ade1b 27 static char rbuf[32];
b11ade1b
BJ
28
29 if (isatty(f)==0)
30 return(NULL);
31 if (fstat(f, &fsb) < 0)
32 return(NULL);
33 if ((fsb.st_mode&S_IFMT) != S_IFCHR)
34 return(NULL);
164a2d2d 35 if ((df = opendir(dev)) == NULL)
b11ade1b 36 return(NULL);
164a2d2d
KM
37 while ((db = readdir(df)) != NULL) {
38 if (db->d_ino != fsb.st_ino)
b11ade1b
BJ
39 continue;
40 strcpy(rbuf, dev);
164a2d2d 41 strcat(rbuf, db->d_name);
b11ade1b
BJ
42 if (stat(rbuf, &tsb) < 0)
43 continue;
164a2d2d
KM
44 if (tsb.st_dev == fsb.st_dev && tsb.st_ino == fsb.st_ino) {
45 closedir(df);
b11ade1b
BJ
46 return(rbuf);
47 }
48 }
164a2d2d 49 closedir(df);
b11ade1b
BJ
50 return(NULL);
51}