| 1 | #include <unistd.h> |
| 2 | #include <errno.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include "syscall.h" |
| 5 | |
| 6 | int ttyname_r(int fd, char *name, size_t size) |
| 7 | { |
| 8 | 	struct stat st1, st2; |
| 9 | 	char procname[sizeof "/proc/self/fd/" + 3*sizeof(int) + 2]; |
| 10 | 	ssize_t l; |
| 11 | |
| 12 | 	if (!isatty(fd)) return errno; |
| 13 | |
| 14 | 	__procfdname(procname, fd); |
| 15 | 	l = readlink(procname, name, size); |
| 16 | |
| 17 | 	if (l < 0) return errno; |
| 18 | 	else if (l == size) return ERANGE; |
| 19 | |
| 20 | 	name[l] = 0; |
| 21 | |
| 22 | 	if (stat(name, &st1) || fstat(fd, &st2)) |
| 23 | 		return errno; |
| 24 | 	if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 25 | 		return ENODEV; |
| 26 | |
| 27 | 	return 0; |
| 28 | } |