Make unvis() have more reasonable argument types.
[unix-history] / usr / src / lib / libc / gen / ttyname.c
CommitLineData
b11ade1b 1/*
0655850e
KB
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
269a7923 5 * %sccs.include.redist.c%
b11ade1b
BJ
6 */
7
0655850e 8#if defined(LIBC_SCCS) && !defined(lint)
17bdadd6 9static char sccsid[] = "@(#)ttyname.c 5.7 (Berkeley) %G%";
0655850e
KB
10#endif /* LIBC_SCCS and not lint */
11
12#include <sys/types.h>
b11ade1b 13#include <sys/stat.h>
17bdadd6
KB
14#include <fcntl.h>
15#include <dirent.h>
0655850e 16#include <sgtty.h>
17bdadd6 17#include <ndbm.h>
8733b48c 18#include <paths.h>
b11ade1b 19
17bdadd6
KB
20static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
21
b11ade1b 22char *
0655850e
KB
23ttyname(fd)
24 int fd;
b11ade1b 25{
17bdadd6 26 struct stat sb;
0655850e 27 struct sgttyb ttyb;
17bdadd6
KB
28 DBM *db;
29 datum dp, key;
30 static char *__oldttyname();
b11ade1b 31
17bdadd6 32 /* Must be a terminal. */
0655850e 33 if (ioctl(fd, TIOCGETP, &ttyb) < 0)
b11ade1b 34 return(NULL);
17bdadd6
KB
35 /* Must be a character device. */
36 if (fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
b11ade1b 37 return(NULL);
17bdadd6
KB
38
39 if (!(db = dbm_open(_PATH_DEVDB, O_RDONLY, 0)))
40 return(__oldttyname(fd, &sb));
41 key.dptr = (char *)&sb.st_rdev;
42 key.dsize = sizeof(sb.st_rdev);
43 dp = dbm_fetch(db, key);
44 if (!dp.dptr)
45 return(__oldttyname(fd, &sb));
46 bcopy(dp.dptr, buf + sizeof(_PATH_DEV) - 1, dp.dsize);
47 return(buf);
48}
49
50static char *
51__oldttyname(fd, sb)
52 int fd;
53 struct stat *sb;
54{
55 register struct dirent *dirp;
56 register DIR *dp;
57 struct stat dsb;
58 char *rval, *strcpy();
59
8733b48c 60 if ((dp = opendir(_PATH_DEV)) == NULL)
b11ade1b 61 return(NULL);
17bdadd6 62
0655850e 63 for (rval = NULL; dirp = readdir(dp);) {
17bdadd6 64 if (dirp->d_fileno != sb->st_ino)
b11ade1b 65 continue;
17bdadd6
KB
66 bcopy(dirp->d_name, buf + sizeof(_PATH_DEV) - 1,
67 dirp->d_namlen + 1);
68 if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev ||
69 sb->st_ino != dsb.st_ino)
b11ade1b 70 continue;
17bdadd6 71 (void)closedir(dp);
0655850e
KB
72 rval = buf;
73 break;
b11ade1b 74 }
17bdadd6 75 (void)closedir(dp);
0655850e 76 return(rval);
b11ade1b 77}