Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / ttyname_r.c
bloba12849a7772dd85384bb604ab2263ee0fdbd5458
1 /* Copyright (C) 1991-1993,1995-2001,2003,2006,2010
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <dirent.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <termios.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #include <stdio-common/_itoa.h>
31 #include <kernel-features.h>
33 static int getttyname_r (char *buf, size_t buflen,
34 dev_t mydev, ino64_t myino, int save,
35 int *dostat) internal_function;
37 static int
38 internal_function attribute_compat_text_section
39 getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino,
40 int save, int *dostat)
42 struct stat64 st;
43 DIR *dirstream;
44 struct dirent64 *d;
45 size_t devlen = strlen (buf);
47 dirstream = __opendir (buf);
48 if (dirstream == NULL)
50 *dostat = -1;
51 return errno;
54 while ((d = __readdir64 (dirstream)) != NULL)
55 if ((d->d_fileno == myino || *dostat)
56 && strcmp (d->d_name, "stdin")
57 && strcmp (d->d_name, "stdout")
58 && strcmp (d->d_name, "stderr"))
60 char *cp;
61 size_t needed = _D_EXACT_NAMLEN (d) + 1;
63 if (needed > buflen)
65 *dostat = -1;
66 (void) __closedir (dirstream);
67 __set_errno (ERANGE);
68 return ERANGE;
71 cp = __stpncpy (buf + devlen, d->d_name, needed);
72 cp[0] = '\0';
74 if (__xstat64 (_STAT_VER, buf, &st) == 0
75 #ifdef _STATBUF_ST_RDEV
76 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
77 #else
78 && d->d_fileno == myino && st.st_dev == mydev
79 #endif
82 (void) __closedir (dirstream);
83 __set_errno (save);
84 return 0;
88 (void) __closedir (dirstream);
89 __set_errno (save);
90 /* It is not clear what to return in this case. `isatty' says FD
91 refers to a TTY but no entry in /dev has this inode. */
92 return ENOTTY;
95 /* Store at most BUFLEN character of the pathname of the terminal FD is
96 open on in BUF. Return 0 on success, otherwise an error number. */
97 int
98 __ttyname_r (int fd, char *buf, size_t buflen)
100 char procname[30];
101 struct stat64 st, st1;
102 int dostat = 0;
103 int save = errno;
105 /* Test for the absolute minimal size. This makes life easier inside
106 the loop. */
107 if (!buf)
109 __set_errno (EINVAL);
110 return EINVAL;
113 if (buflen < sizeof ("/dev/pts/"))
115 __set_errno (ERANGE);
116 return ERANGE;
119 /* isatty check, tcgetattr is used because it sets the correct
120 errno (EBADF resp. ENOTTY) on error. */
121 struct termios term;
122 if (__builtin_expect (__tcgetattr (fd, &term) < 0, 0))
123 return errno;
125 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
126 return errno;
128 /* We try using the /proc filesystem. */
129 *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
131 ssize_t ret = __readlink (procname, buf, buflen - 1);
132 if (__builtin_expect (ret == -1 && errno == ENOENT, 0))
134 __set_errno (EBADF);
135 return EBADF;
138 if (__builtin_expect (ret == -1 && errno == ENAMETOOLONG, 0))
140 __set_errno (ERANGE);
141 return ERANGE;
144 if (__builtin_expect (ret != -1
145 #ifndef __ASSUME_PROC_SELF_FD_SYMLINK
146 /* This is for Linux 2.0. */
147 && buf[0] != '['
148 #endif
149 , 1))
151 #define UNREACHABLE_LEN strlen ("(unreachable)")
152 if (ret > UNREACHABLE_LEN
153 && memcmp (buf, "(unreachable)", UNREACHABLE_LEN) == 0)
155 memmove (buf, buf + UNREACHABLE_LEN, ret - UNREACHABLE_LEN);
156 ret -= UNREACHABLE_LEN;
159 /* readlink need not terminate the string. */
160 buf[ret] = '\0';
162 /* Verify readlink result, fall back on iterating through devices. */
163 if (buf[0] == '/'
164 && __xstat64 (_STAT_VER, buf, &st1) == 0
165 #ifdef _STATBUF_ST_RDEV
166 && S_ISCHR (st1.st_mode)
167 && st1.st_rdev == st.st_rdev
168 #else
169 && st1.st_ino == st.st_ino
170 && st1.st_dev == st.st_dev
171 #endif
173 return 0;
176 /* Prepare the result buffer. */
177 memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/"));
178 buflen -= sizeof ("/dev/pts/") - 1;
180 if (__xstat64 (_STAT_VER, buf, &st1) == 0 && S_ISDIR (st1.st_mode))
182 #ifdef _STATBUF_ST_RDEV
183 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
184 &dostat);
185 #else
186 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
187 &dostat);
188 #endif
190 else
192 __set_errno (save);
193 ret = ENOENT;
196 if (ret && dostat != -1)
198 buf[sizeof ("/dev/") - 1] = '\0';
199 buflen += sizeof ("pts/") - 1;
200 #ifdef _STATBUF_ST_RDEV
201 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
202 &dostat);
203 #else
204 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
205 &dostat);
206 #endif
209 if (ret && dostat != -1)
211 buf[sizeof ("/dev/") - 1] = '\0';
212 dostat = 1;
213 #ifdef _STATBUF_ST_RDEV
214 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino,
215 save, &dostat);
216 #else
217 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino,
218 save, &dostat);
219 #endif
222 return ret;
225 weak_alias (__ttyname_r, ttyname_r)