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) |
269a7923 | 9 | static char sccsid[] = "@(#)ttyname.c 5.6 (Berkeley) %G%"; |
0655850e KB |
10 | #endif /* LIBC_SCCS and not lint */ |
11 | ||
12 | #include <sys/types.h> | |
c9f0b4d5 | 13 | #include <sys/dir.h> |
b11ade1b | 14 | #include <sys/stat.h> |
0655850e | 15 | #include <sgtty.h> |
8733b48c | 16 | #include <paths.h> |
b11ade1b BJ |
17 | |
18 | char * | |
0655850e KB |
19 | ttyname(fd) |
20 | int fd; | |
b11ade1b | 21 | { |
0655850e KB |
22 | register struct direct *dirp; |
23 | register DIR *dp; | |
24 | struct stat sb1, sb2; | |
25 | struct sgttyb ttyb; | |
8733b48c | 26 | static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV; |
0655850e | 27 | char *rval, *strcpy(); |
b11ade1b | 28 | |
0655850e | 29 | if (ioctl(fd, TIOCGETP, &ttyb) < 0) |
b11ade1b | 30 | return(NULL); |
0655850e | 31 | if (fstat(fd, &sb1) < 0 || (sb1.st_mode&S_IFMT) != S_IFCHR) |
b11ade1b | 32 | return(NULL); |
8733b48c | 33 | if ((dp = opendir(_PATH_DEV)) == NULL) |
b11ade1b | 34 | return(NULL); |
0655850e KB |
35 | for (rval = NULL; dirp = readdir(dp);) { |
36 | if (dirp->d_ino != sb1.st_ino) | |
b11ade1b | 37 | continue; |
8733b48c | 38 | (void)strcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name); |
0655850e | 39 | if (stat(buf, &sb2) < 0 || sb1.st_dev != sb2.st_dev || |
6f9ee7de | 40 | sb1.st_ino != sb2.st_ino) |
b11ade1b | 41 | continue; |
0655850e KB |
42 | closedir(dp); |
43 | rval = buf; | |
44 | break; | |
b11ade1b | 45 | } |
0655850e KB |
46 | closedir(dp); |
47 | return(rval); | |
b11ade1b | 48 | } |