Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / ttyname_r.c
bloba03d012f8d2ebdf550902958788a2d5f7068e47f
1 /* Copyright (C) 1991-2014 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>
30 #include <kernel-features.h>
32 static int getttyname_r (char *buf, size_t buflen,
33 dev_t mydev, ino64_t myino, int save,
34 int *dostat) internal_function;
36 static int
37 internal_function attribute_compat_text_section
38 getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino,
39 int save, int *dostat)
41 struct stat64 st;
42 DIR *dirstream;
43 struct dirent64 *d;
44 size_t devlen = strlen (buf);
46 dirstream = __opendir (buf);
47 if (dirstream == NULL)
49 *dostat = -1;
50 return errno;
53 while ((d = __readdir64 (dirstream)) != NULL)
54 if ((d->d_fileno == myino || *dostat)
55 && strcmp (d->d_name, "stdin")
56 && strcmp (d->d_name, "stdout")
57 && strcmp (d->d_name, "stderr"))
59 char *cp;
60 size_t needed = _D_EXACT_NAMLEN (d) + 1;
62 if (needed > buflen)
64 *dostat = -1;
65 (void) __closedir (dirstream);
66 __set_errno (ERANGE);
67 return ERANGE;
70 cp = __stpncpy (buf + devlen, d->d_name, needed);
71 cp[0] = '\0';
73 if (__xstat64 (_STAT_VER, buf, &st) == 0
74 #ifdef _STATBUF_ST_RDEV
75 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
76 #else
77 && d->d_fileno == myino && st.st_dev == mydev
78 #endif
81 (void) __closedir (dirstream);
82 __set_errno (save);
83 return 0;
87 (void) __closedir (dirstream);
88 __set_errno (save);
89 /* It is not clear what to return in this case. `isatty' says FD
90 refers to a TTY but no entry in /dev has this inode. */
91 return ENOTTY;
94 /* Store at most BUFLEN character of the pathname of the terminal FD is
95 open on in BUF. Return 0 on success, otherwise an error number. */
96 int
97 __ttyname_r (int fd, char *buf, size_t buflen)
99 char procname[30];
100 struct stat64 st, st1;
101 int dostat = 0;
102 int save = errno;
104 /* Test for the absolute minimal size. This makes life easier inside
105 the loop. */
106 if (!buf)
108 __set_errno (EINVAL);
109 return EINVAL;
112 if (buflen < sizeof ("/dev/pts/"))
114 __set_errno (ERANGE);
115 return ERANGE;
118 /* isatty check, tcgetattr is used because it sets the correct
119 errno (EBADF resp. ENOTTY) on error. */
120 struct termios term;
121 if (__builtin_expect (__tcgetattr (fd, &term) < 0, 0))
122 return errno;
124 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
125 return errno;
127 /* We try using the /proc filesystem. */
128 *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
130 ssize_t ret = __readlink (procname, buf, buflen - 1);
131 if (__builtin_expect (ret == -1 && errno == ENAMETOOLONG, 0))
133 __set_errno (ERANGE);
134 return ERANGE;
137 if (__builtin_expect (ret != -1, 1))
139 #define UNREACHABLE_LEN strlen ("(unreachable)")
140 if (ret > UNREACHABLE_LEN
141 && memcmp (buf, "(unreachable)", UNREACHABLE_LEN) == 0)
143 memmove (buf, buf + UNREACHABLE_LEN, ret - UNREACHABLE_LEN);
144 ret -= UNREACHABLE_LEN;
147 /* readlink need not terminate the string. */
148 buf[ret] = '\0';
150 /* Verify readlink result, fall back on iterating through devices. */
151 if (buf[0] == '/'
152 && __xstat64 (_STAT_VER, buf, &st1) == 0
153 #ifdef _STATBUF_ST_RDEV
154 && S_ISCHR (st1.st_mode)
155 && st1.st_rdev == st.st_rdev
156 #else
157 && st1.st_ino == st.st_ino
158 && st1.st_dev == st.st_dev
159 #endif
161 return 0;
164 /* Prepare the result buffer. */
165 memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/"));
166 buflen -= sizeof ("/dev/pts/") - 1;
168 if (__xstat64 (_STAT_VER, buf, &st1) == 0 && S_ISDIR (st1.st_mode))
170 #ifdef _STATBUF_ST_RDEV
171 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
172 &dostat);
173 #else
174 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
175 &dostat);
176 #endif
178 else
180 __set_errno (save);
181 ret = ENOENT;
184 if (ret && dostat != -1)
186 buf[sizeof ("/dev/") - 1] = '\0';
187 buflen += sizeof ("pts/") - 1;
188 #ifdef _STATBUF_ST_RDEV
189 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
190 &dostat);
191 #else
192 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
193 &dostat);
194 #endif
197 if (ret && dostat != -1)
199 buf[sizeof ("/dev/") - 1] = '\0';
200 dostat = 1;
201 #ifdef _STATBUF_ST_RDEV
202 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino,
203 save, &dostat);
204 #else
205 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino,
206 save, &dostat);
207 #endif
210 return ret;
213 weak_alias (__ttyname_r, ttyname_r)