Commit | Line | Data |
---|---|---|
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) |
ab225a7a | 9 | static char sccsid[] = "@(#)ttyname.c 5.9 (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> |
ab225a7a | 17 | #include <db.h> |
c5980113 | 18 | #include <unistd.h> |
8733b48c | 19 | #include <paths.h> |
b11ade1b | 20 | |
17bdadd6 KB |
21 | static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV; |
22 | ||
b11ade1b | 23 | char * |
0655850e KB |
24 | ttyname(fd) |
25 | int fd; | |
b11ade1b | 26 | { |
17bdadd6 | 27 | struct stat sb; |
0655850e | 28 | struct sgttyb ttyb; |
ab225a7a KB |
29 | DB *db; |
30 | DBT data, key; | |
17bdadd6 | 31 | static char *__oldttyname(); |
b11ade1b | 32 | |
17bdadd6 | 33 | /* Must be a terminal. */ |
0655850e | 34 | if (ioctl(fd, TIOCGETP, &ttyb) < 0) |
b11ade1b | 35 | return(NULL); |
17bdadd6 KB |
36 | /* Must be a character device. */ |
37 | if (fstat(fd, &sb) || !S_ISCHR(sb.st_mode)) | |
b11ade1b | 38 | return(NULL); |
17bdadd6 | 39 | |
ab225a7a KB |
40 | if (db = hash_open(_PATH_DEVDB, O_RDONLY, 0, NULL)) { |
41 | key.data = (u_char *)&sb.st_rdev; | |
42 | key.size = sizeof(sb.st_rdev); | |
43 | if (!(db->get)(db, &key, &data, 0)) { | |
44 | bcopy(data.data, | |
45 | buf + sizeof(_PATH_DEV) - 1, data.size); | |
46 | return(buf); | |
47 | } | |
48 | } | |
49 | return(__oldttyname(fd, &sb)); | |
17bdadd6 KB |
50 | } |
51 | ||
52 | static char * | |
53 | __oldttyname(fd, sb) | |
54 | int fd; | |
55 | struct stat *sb; | |
56 | { | |
57 | register struct dirent *dirp; | |
58 | register DIR *dp; | |
59 | struct stat dsb; | |
60 | char *rval, *strcpy(); | |
61 | ||
8733b48c | 62 | if ((dp = opendir(_PATH_DEV)) == NULL) |
b11ade1b | 63 | return(NULL); |
17bdadd6 | 64 | |
0655850e | 65 | for (rval = NULL; dirp = readdir(dp);) { |
17bdadd6 | 66 | if (dirp->d_fileno != sb->st_ino) |
b11ade1b | 67 | continue; |
17bdadd6 KB |
68 | bcopy(dirp->d_name, buf + sizeof(_PATH_DEV) - 1, |
69 | dirp->d_namlen + 1); | |
70 | if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev || | |
71 | sb->st_ino != dsb.st_ino) | |
b11ade1b | 72 | continue; |
17bdadd6 | 73 | (void)closedir(dp); |
0655850e KB |
74 | rval = buf; |
75 | break; | |
b11ade1b | 76 | } |
17bdadd6 | 77 | (void)closedir(dp); |
0655850e | 78 | return(rval); |
b11ade1b | 79 | } |