Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / ttyname.c
blob838c979223bbe5c0ab0e8cb73e453539d876402d
1 /* Copyright (C) 1991-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <limits.h>
20 #include <stddef.h>
21 #include <dirent.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <termios.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <stdlib.h>
29 #include <_itoa.h>
31 #if 0
32 /* Is this used anywhere? It is not exported. */
33 char *__ttyname;
34 #endif
36 static char *getttyname (const char *dev, dev_t mydev,
37 ino64_t myino, int save, int *dostat)
38 internal_function;
41 libc_freeres_ptr (static char *getttyname_name);
43 static char *
44 internal_function attribute_compat_text_section
45 getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat)
47 static size_t namelen;
48 struct stat64 st;
49 DIR *dirstream;
50 struct dirent64 *d;
51 size_t devlen = strlen (dev) + 1;
53 dirstream = __opendir (dev);
54 if (dirstream == NULL)
56 *dostat = -1;
57 return NULL;
60 /* Prepare for the loop. If we already have a buffer copy the directory
61 name we look at into it. */
62 if (devlen < namelen)
63 *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
65 while ((d = __readdir64 (dirstream)) != NULL)
66 if ((d->d_fileno == myino || *dostat)
67 && strcmp (d->d_name, "stdin")
68 && strcmp (d->d_name, "stdout")
69 && strcmp (d->d_name, "stderr"))
71 size_t dlen = _D_ALLOC_NAMLEN (d);
72 if (devlen + dlen > namelen)
74 free (getttyname_name);
75 namelen = 2 * (devlen + dlen); /* Big enough. */
76 getttyname_name = malloc (namelen);
77 if (! getttyname_name)
79 *dostat = -1;
80 /* Perhaps it helps to free the directory stream buffer. */
81 (void) __closedir (dirstream);
82 return NULL;
84 *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
86 memcpy (&getttyname_name[devlen], d->d_name, dlen);
87 if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0
88 #ifdef _STATBUF_ST_RDEV
89 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
90 #else
91 && d->d_fileno == myino && st.st_dev == mydev
92 #endif
95 (void) __closedir (dirstream);
96 #if 0
97 __ttyname = getttyname_name;
98 #endif
99 __set_errno (save);
100 return getttyname_name;
104 (void) __closedir (dirstream);
105 __set_errno (save);
106 return NULL;
110 /* Static buffer in `ttyname'. */
111 libc_freeres_ptr (static char *ttyname_buf);
114 /* Return the pathname of the terminal FD is open on, or NULL on errors.
115 The returned storage is good only until the next call to this function. */
116 char *
117 ttyname (int fd)
119 static size_t buflen;
120 char procname[30];
121 struct stat64 st, st1;
122 int dostat = 0;
123 char *name;
124 int save = errno;
125 struct termios term;
127 /* isatty check, tcgetattr is used because it sets the correct
128 errno (EBADF resp. ENOTTY) on error. */
129 if (__glibc_unlikely (__tcgetattr (fd, &term) < 0))
130 return NULL;
132 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
133 return NULL;
135 /* We try using the /proc filesystem. */
136 *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
138 if (buflen == 0)
140 buflen = 4095;
141 ttyname_buf = (char *) malloc (buflen + 1);
142 if (ttyname_buf == NULL)
144 buflen = 0;
145 return NULL;
149 ssize_t len = __readlink (procname, ttyname_buf, buflen);
150 if (__glibc_likely (len != -1))
152 if ((size_t) len >= buflen)
153 return NULL;
155 #define UNREACHABLE_LEN strlen ("(unreachable)")
156 if (len > UNREACHABLE_LEN
157 && memcmp (ttyname_buf, "(unreachable)", UNREACHABLE_LEN) == 0)
159 memmove (ttyname_buf, ttyname_buf + UNREACHABLE_LEN,
160 len - UNREACHABLE_LEN);
161 len -= UNREACHABLE_LEN;
164 /* readlink need not terminate the string. */
165 ttyname_buf[len] = '\0';
167 /* Verify readlink result, fall back on iterating through devices. */
168 if (ttyname_buf[0] == '/'
169 && __xstat64 (_STAT_VER, ttyname_buf, &st1) == 0
170 #ifdef _STATBUF_ST_RDEV
171 && S_ISCHR (st1.st_mode)
172 && st1.st_rdev == st.st_rdev
173 #else
174 && st1.st_ino == st.st_ino
175 && st1.st_dev == st.st_dev
176 #endif
178 return ttyname_buf;
181 if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
183 #ifdef _STATBUF_ST_RDEV
184 name = getttyname ("/dev/pts", st.st_rdev, st.st_ino, save, &dostat);
185 #else
186 name = getttyname ("/dev/pts", st.st_dev, st.st_ino, save, &dostat);
187 #endif
189 else
191 __set_errno (save);
192 name = NULL;
195 if (!name && dostat != -1)
197 #ifdef _STATBUF_ST_RDEV
198 name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
199 #else
200 name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
201 #endif
204 if (!name && dostat != -1)
206 dostat = 1;
207 #ifdef _STATBUF_ST_RDEV
208 name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
209 #else
210 name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
211 #endif
214 return name;