about summary refs log tree commit diff
path: root/sysdeps/unix/sysv
diff options
context:
space:
mode:
Diffstat (limited to 'sysdeps/unix/sysv')
-rw-r--r--sysdeps/unix/sysv/linux/ttyname.c22
-rw-r--r--sysdeps/unix/sysv/linux/ttyname_r.c15
2 files changed, 37 insertions, 0 deletions
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 <string.h>
 #include <stdlib.h>
 
+#include <stdio-common/_itoa.h>
+
 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;
 
diff --git a/sysdeps/unix/sysv/linux/ttyname_r.c b/sysdeps/unix/sysv/linux/ttyname_r.c
index 9c859caa25..8306cf56c0 100644
--- a/sysdeps/unix/sysv/linux/ttyname_r.c
+++ b/sysdeps/unix/sysv/linux/ttyname_r.c
@@ -26,6 +26,8 @@
 #include <string.h>
 #include <stdlib.h>
 
+#include <stdio-common/_itoa.h>
+
 static int getttyname_r __P ((int fd, char *buf, size_t buflen,
 			      dev_t mydev, ino_t myino, int save,
 			      int *dostat)) internal_function;
@@ -102,6 +104,7 @@ __ttyname_r (fd, buf, buflen)
      char *buf;
      size_t buflen;
 {
+  char procname[30];
   struct stat st, st1;
   int dostat = 0;
   int save = errno;
@@ -127,6 +130,18 @@ __ttyname_r (fd, buf, buflen)
       return ENOTTY;
     }
 
+  /* We try using the /proc filesystem.  */
+  *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
+
+  ret = __readlink (procname, buf, buflen - 1);
+  if (ret != -1)
+    return 0;
+  if (errno == ENAMETOOLONG)
+    {
+      __set_errno (ERANGE);
+      return ERANGE;
+    }
+
   if (fstat (fd, &st) < 0)
     return errno;