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/>. */
22 #include <sys/types.h>
32 /* Is this used anywhere? It is not exported. */
36 static char *getttyname (const char *dev
, dev_t mydev
,
37 ino64_t myino
, int save
, int *dostat
)
41 libc_freeres_ptr (static char *getttyname_name
);
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
;
51 size_t devlen
= strlen (dev
) + 1;
53 dirstream
= __opendir (dev
);
54 if (dirstream
== NULL
)
60 /* Prepare for the loop. If we already have a buffer copy the directory
61 name we look at into it. */
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
)
80 /* Perhaps it helps to free the directory stream buffer. */
81 (void) __closedir (dirstream
);
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
91 && d
->d_fileno
== myino
&& st
.st_dev
== mydev
95 (void) __closedir (dirstream
);
97 __ttyname
= getttyname_name
;
100 return getttyname_name
;
104 (void) __closedir (dirstream
);
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. */
119 static size_t buflen
;
121 struct stat64 st
, st1
;
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))
132 if (__fxstat64 (_STAT_VER
, fd
, &st
) < 0)
135 /* We try using the /proc filesystem. */
136 *_fitoa_word (fd
, __stpcpy (procname
, "/proc/self/fd/"), 10, 0) = '\0';
141 ttyname_buf
= (char *) malloc (buflen
+ 1);
142 if (ttyname_buf
== NULL
)
149 ssize_t len
= __readlink (procname
, ttyname_buf
, buflen
);
150 if (__glibc_likely (len
!= -1))
152 if ((size_t) len
>= buflen
)
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
174 && st1
.st_ino
== st
.st_ino
175 && st1
.st_dev
== st
.st_dev
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
);
186 name
= getttyname ("/dev/pts", st
.st_dev
, st
.st_ino
, save
, &dostat
);
195 if (!name
&& dostat
!= -1)
197 #ifdef _STATBUF_ST_RDEV
198 name
= getttyname ("/dev", st
.st_rdev
, st
.st_ino
, save
, &dostat
);
200 name
= getttyname ("/dev", st
.st_dev
, st
.st_ino
, save
, &dostat
);
204 if (!name
&& dostat
!= -1)
207 #ifdef _STATBUF_ST_RDEV
208 name
= getttyname ("/dev", st
.st_rdev
, st
.st_ino
, save
, &dostat
);
210 name
= getttyname ("/dev", st
.st_dev
, st
.st_ino
, save
, &dostat
);