]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - sysdeps/unix/sysv/linux/ttyname.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / ttyname.c
index 0973925cca5d65c36ddebfd2b9557feb2b0bd522..8d808ce373d3ae7647f70c33a73e532533d58958 100644 (file)
@@ -1,20 +1,19 @@
-/* Copyright (C) 1991,92,93,96,97,98,99,2000 Free Software Foundation, Inc.
+/* Copyright (C) 1991-2014 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #include <errno.h>
 #include <limits.h>
 #include <dirent.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <termios.h>
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
 
-#include <stdio-common/_itoa.h>
+#include <_itoa.h>
+#include <kernel-features.h>
 
+#if 0
+/* Is this used anywhere?  It is not exported.  */
 char *__ttyname;
+#endif
 
 static char *getttyname (const char *dev, dev_t mydev,
-                        ino_t myino, int save, int *dostat)
+                        ino64_t myino, int save, int *dostat)
      internal_function;
 
 
-static char *getttyname_name;
+libc_freeres_ptr (static char *getttyname_name);
 
 static char *
-internal_function
-getttyname (const char *dev, dev_t mydev, ino_t myino, int save, int *dostat)
+internal_function attribute_compat_text_section
+getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat)
 {
   static size_t namelen;
-  struct stat st;
+  struct stat64 st;
   DIR *dirstream;
-  struct dirent *d;
+  struct dirent64 *d;
   size_t devlen = strlen (dev) + 1;
 
-  dirstream = opendir (dev);
+  dirstream = __opendir (dev);
   if (dirstream == NULL)
     {
       *dostat = -1;
       return NULL;
     }
 
-  while ((d = readdir (dirstream)) != NULL)
-    if (((ino_t) d->d_fileno == myino || *dostat)
+  /* Prepare for the loop.  If we already have a buffer copy the directory
+     name we look at into it.  */
+  if (devlen < namelen)
+    *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
+
+  while ((d = __readdir64 (dirstream)) != NULL)
+    if ((d->d_fileno == myino || *dostat)
        && strcmp (d->d_name, "stdin")
        && strcmp (d->d_name, "stdout")
        && strcmp (d->d_name, "stderr"))
@@ -76,16 +85,18 @@ getttyname (const char *dev, dev_t mydev, ino_t myino, int save, int *dostat)
            *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
          }
        memcpy (&getttyname_name[devlen], d->d_name, dlen);
-       if (__xstat (_STAT_VER, getttyname_name, &st) == 0
+       if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0
 #ifdef _STATBUF_ST_RDEV
            && S_ISCHR (st.st_mode) && st.st_rdev == mydev
 #else
-           && (ino_t) d->d_fileno == myino && st.st_dev == mydev
+           && d->d_fileno == myino && st.st_dev == mydev
 #endif
           )
          {
            (void) __closedir (dirstream);
+#if 0
            __ttyname = getttyname_name;
+#endif
            __set_errno (save);
            return getttyname_name;
          }
@@ -98,7 +109,7 @@ getttyname (const char *dev, dev_t mydev, ino_t myino, int save, int *dostat)
 
 
 /* Static buffer in `ttyname'.  */
-static char *ttyname_buf;
+libc_freeres_ptr (static char *ttyname_buf);
 
 
 /* Return the pathname of the terminal FD is open on, or NULL on errors.
@@ -108,13 +119,18 @@ ttyname (int fd)
 {
   static size_t buflen;
   char procname[30];
-  struct stat st, st1;
+  struct stat64 st, st1;
   int dostat = 0;
   char *name;
   int save = errno;
-  int len;
+  struct termios term;
+
+  /* isatty check, tcgetattr is used because it sets the correct
+     errno (EBADF resp. ENOTTY) on error.  */
+  if (__builtin_expect (__tcgetattr (fd, &term) < 0, 0))
+    return NULL;
 
-  if (!__isatty (fd))
+  if (__fxstat64 (_STAT_VER, fd, &st) < 0)
     return NULL;
 
   /* We try using the /proc filesystem.  */
@@ -131,22 +147,39 @@ ttyname (int fd)
        }
     }
 
-  len = __readlink (procname, ttyname_buf, buflen);
-  if (len != -1
-      /* This is for Linux 2.0.  */
-      && ttyname_buf[0] != '[')
+  ssize_t len = __readlink (procname, ttyname_buf, buflen);
+  if (__builtin_expect (len != -1, 1))
     {
-      if (len >= buflen)
+      if ((size_t) len >= buflen)
        return NULL;
+
+#define UNREACHABLE_LEN strlen ("(unreachable)")
+      if (len > UNREACHABLE_LEN
+         && memcmp (ttyname_buf, "(unreachable)", UNREACHABLE_LEN) == 0)
+       {
+         memmove (ttyname_buf, ttyname_buf + UNREACHABLE_LEN,
+                  len - UNREACHABLE_LEN);
+         len -= UNREACHABLE_LEN;
+       }
+
       /* readlink need not terminate the string.  */
       ttyname_buf[len] = '\0';
-      return ttyname_buf;
-    }
 
-  if (__fxstat (_STAT_VER, fd, &st) < 0)
-    return NULL;
+      /* Verify readlink result, fall back on iterating through devices.  */
+      if (ttyname_buf[0] == '/'
+         && __xstat64 (_STAT_VER, ttyname_buf, &st1) == 0
+#ifdef _STATBUF_ST_RDEV
+         && S_ISCHR (st1.st_mode)
+         && st1.st_rdev == st.st_rdev
+#else
+         && st1.st_ino == st.st_ino
+         && st1.st_dev == st.st_dev
+#endif
+         )
+       return ttyname_buf;
+    }
 
-  if (__xstat (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
+  if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
     {
 #ifdef _STATBUF_ST_RDEV
       name = getttyname ("/dev/pts", st.st_rdev, st.st_ino, save, &dostat);
@@ -181,12 +214,3 @@ ttyname (int fd)
 
   return name;
 }
-
-
-static void
-free_mem (void)
-{
-  free (ttyname_buf);
-  free (getttyname_name);
-}
-text_set_element (__libc_subfreeres, free_mem);