convert to use new directory access routines
[unix-history] / usr / src / lib / libc / gen / ttyname.c
CommitLineData
164a2d2d 1/* @(#)ttyname.c 4.2 (Berkeley) %G% */
b11ade1b
BJ
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
164a2d2d
KM
9#include <sys/param.h>
10#include <sys/ndir.h>
b11ade1b
BJ
11#include <sys/stat.h>
12
13static char dev[] = "/dev/";
14char *strcpy();
15char *strcat();
16
17char *
18ttyname(f)
19{
20 struct stat fsb;
21 struct stat tsb;
164a2d2d
KM
22 register struct direct *db;
23 register DIR *df;
b11ade1b 24 static char rbuf[32];
b11ade1b
BJ
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);
164a2d2d 32 if ((df = opendir(dev)) == NULL)
b11ade1b 33 return(NULL);
164a2d2d
KM
34 while ((db = readdir(df)) != NULL) {
35 if (db->d_ino != fsb.st_ino)
b11ade1b
BJ
36 continue;
37 strcpy(rbuf, dev);
164a2d2d 38 strcat(rbuf, db->d_name);
b11ade1b
BJ
39 if (stat(rbuf, &tsb) < 0)
40 continue;
164a2d2d
KM
41 if (tsb.st_dev == fsb.st_dev && tsb.st_ino == fsb.st_ino) {
42 closedir(df);
b11ade1b
BJ
43 return(rbuf);
44 }
45 }
164a2d2d 46 closedir(df);
b11ade1b
BJ
47 return(NULL);
48}