]> 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 a32d1a6605dc444c86ae900742fc0ab66dcc95d7..8d808ce373d3ae7647f70c33a73e532533d58958 100644 (file)
@@ -1,20 +1,19 @@
-/* Copyright (C) 1991, 92, 93, 96, 97, 98, 99 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>
 
-char *__ttyname = NULL;
+#if 0
+/* Is this used anywhere?  It is not exported.  */
+char *__ttyname;
+#endif
 
-static char * getttyname __P ((const char *dev, dev_t mydev,
-                              ino_t myino, int save, int *dostat))
+static char *getttyname (const char *dev, dev_t mydev,
+                        ino64_t myino, int save, int *dostat)
      internal_function;
 
+
+libc_freeres_ptr (static char *getttyname_name);
+
 static char *
-internal_function
-getttyname (dev, mydev, myino, save, dostat)
-     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 char *name;
-  static size_t namelen = 0;
-  struct stat st;
+  static size_t namelen;
+  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"))
@@ -66,31 +72,33 @@ getttyname (dev, mydev, myino, save, dostat)
        size_t dlen = _D_ALLOC_NAMLEN (d);
        if (devlen + dlen > namelen)
          {
-           free (name);
+           free (getttyname_name);
            namelen = 2 * (devlen + dlen); /* Big enough.  */
-           name = malloc (namelen);
-           if (! name)
+           getttyname_name = malloc (namelen);
+           if (! getttyname_name)
              {
                *dostat = -1;
                /* Perhaps it helps to free the directory stream buffer.  */
                (void) __closedir (dirstream);
                return NULL;
              }
-           *((char *) __mempcpy (name, dev, devlen - 1)) = '/';
+           *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
          }
-       memcpy (&name[devlen], d->d_name, dlen);
-       if (__xstat (_STAT_VER, name, &st) == 0
+       memcpy (&getttyname_name[devlen], d->d_name, dlen);
+       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);
-           __ttyname = name;
+#if 0
+           __ttyname = getttyname_name;
+#endif
            __set_errno (save);
-           return name;
+           return getttyname_name;
          }
       }
 
@@ -99,22 +107,30 @@ getttyname (dev, mydev, myino, save, dostat)
   return NULL;
 }
 
+
+/* Static buffer in `ttyname'.  */
+libc_freeres_ptr (static char *ttyname_buf);
+
+
 /* Return the pathname of the terminal FD is open on, or NULL on errors.
    The returned storage is good only until the next call to this function.  */
 char *
-ttyname (fd)
-     int fd;
+ttyname (int fd)
 {
-  static char *buf;
-  static size_t buflen = 0;
-  static int dev_pts_available = 1;
+  static size_t buflen;
   char procname[30];
-  struct stat st, st1;
+  struct stat64 st, st1;
   int dostat = 0;
   char *name;
   int save = errno;
+  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.  */
@@ -123,24 +139,47 @@ ttyname (fd)
   if (buflen == 0)
     {
       buflen = 4095;
-      buf = (char *) malloc (buflen + 1);
-      if (buf == NULL)
+      ttyname_buf = (char *) malloc (buflen + 1);
+      if (ttyname_buf == NULL)
        {
          buflen = 0;
          return NULL;
        }
     }
 
-  if (__readlink (procname, buf, buflen) != -1
-      /* This is for Linux 2.0.  */
-      && buf[0] != '[')
-    return buf;
+  ssize_t len = __readlink (procname, ttyname_buf, buflen);
+  if (__builtin_expect (len != -1, 1))
+    {
+      if ((size_t) len >= buflen)
+       return NULL;
 
-  if (__fxstat (_STAT_VER, fd, &st) < 0)
-    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';
+
+      /* 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 (dev_pts_available
-      && __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);
@@ -152,7 +191,6 @@ ttyname (fd)
     {
       __set_errno (save);
       name = NULL;
-      dev_pts_available = 1;
     }
 
   if (!name && dostat != -1)