From c5e340c71ba6f4563ca5fa245baa82b6363ddb2e Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Thu, 29 Oct 1998 15:17:25 +0000 Subject: Update. 1998-10-29 Ulrich Drepper * sysdeps/unix/sysv/linux/ttyname_r.c (ttyname_r): Try reading /prof/self/fd/FD first. * sysdeps/unix/sysv/linux/ttyname.c (ttyname): Likewise. * stdio-common/_itoa.h (_fitoa_word): New inline function. Write formatted number starting at given position and return pointer to following byte. (_fitoa): Likewise, for long long. --- sysdeps/unix/sysv/linux/ttyname.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'sysdeps/unix/sysv/linux/ttyname.c') diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c index 91f0d7a40f..40b006a8cd 100644 --- a/sysdeps/unix/sysv/linux/ttyname.c +++ b/sysdeps/unix/sysv/linux/ttyname.c @@ -26,6 +26,8 @@ #include #include +#include + char *__ttyname = NULL; static char * getttyname __P ((const char *dev, int fd, dev_t mydev, @@ -104,6 +106,9 @@ char * ttyname (fd) int fd; { + static char *buf; + static size_t buflen = 0; + char procname[30]; struct stat st, st1; int dostat = 0; char *name; @@ -112,6 +117,23 @@ ttyname (fd) if (!__isatty (fd)) return NULL; + /* We try using the /proc filesystem. */ + *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0'; + + if (buflen == 0) + { + buflen = 4095; + buf = (char *) malloc (buflen + 1); + if (buf == NULL) + { + buflen = 0; + return NULL; + } + } + + if (__readlink (procname, buf, buflen) != -1) + return buf; + if (fstat (fd, &st) < 0) return NULL; -- cgit v1.2.3