1 /* Copyright (C) 1991,92,93,1995-2001,2003,2006 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <sys/types.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
;
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
)
45 size_t devlen
= strlen (buf
);
47 dirstream
= __opendir (buf
);
48 if (dirstream
== NULL
)
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"))
61 size_t needed
= _D_EXACT_NAMLEN (d
) + 1;
66 (void) __closedir (dirstream
);
71 cp
= __stpncpy (buf
+ devlen
, d
->d_name
, needed
);
74 if (__xstat64 (_STAT_VER
, buf
, &st
) == 0
75 #ifdef _STATBUF_ST_RDEV
76 && S_ISCHR (st
.st_mode
) && st
.st_rdev
== mydev
78 && d
->d_fileno
== myino
&& st
.st_dev
== mydev
82 (void) __closedir (dirstream
);
88 (void) __closedir (dirstream
);
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. */
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. */
98 __ttyname_r (int fd
, char *buf
, size_t buflen
)
101 struct stat64 st
, st1
;
105 /* Test for the absolute minimal size. This makes life easier inside
109 __set_errno (EINVAL
);
113 if (buflen
< sizeof ("/dev/pts/"))
115 __set_errno (ERANGE
);
119 /* isatty check, tcgetattr is used because it sets the correct
120 errno (EBADF resp. ENOTTY) on error. */
122 if (__builtin_expect (__tcgetattr (fd
, &term
) < 0, 0))
125 /* We try using the /proc filesystem. */
126 *_fitoa_word (fd
, __stpcpy (procname
, "/proc/self/fd/"), 10, 0) = '\0';
128 ssize_t ret
= __readlink (procname
, buf
, buflen
- 1);
129 if (__builtin_expect (ret
== -1 && errno
== ENOENT
, 0))
135 if (__builtin_expect (ret
== -1 && errno
== ENAMETOOLONG
, 0))
137 __set_errno (ERANGE
);
141 if (__builtin_expect (ret
!= -1
142 #ifndef __ASSUME_PROC_SELF_FD_SYMLINK
143 /* This is for Linux 2.0. */
152 if (__fxstat64 (_STAT_VER
, fd
, &st
) < 0)
155 /* Prepare the result buffer. */
156 memcpy (buf
, "/dev/pts/", sizeof ("/dev/pts/"));
157 buflen
-= sizeof ("/dev/pts/") - 1;
159 if (__xstat64 (_STAT_VER
, buf
, &st1
) == 0 && S_ISDIR (st1
.st_mode
))
161 #ifdef _STATBUF_ST_RDEV
162 ret
= getttyname_r (buf
, buflen
, st
.st_rdev
, st
.st_ino
, save
,
165 ret
= getttyname_r (buf
, buflen
, st
.st_dev
, st
.st_ino
, save
,
175 if (ret
&& dostat
!= -1)
177 buf
[sizeof ("/dev/") - 1] = '\0';
178 buflen
+= sizeof ("pts/") - 1;
179 #ifdef _STATBUF_ST_RDEV
180 ret
= getttyname_r (buf
, buflen
, st
.st_rdev
, st
.st_ino
, save
,
183 ret
= getttyname_r (buf
, buflen
, st
.st_dev
, st
.st_ino
, save
,
188 if (ret
&& dostat
!= -1)
190 buf
[sizeof ("/dev/") - 1] = '\0';
192 #ifdef _STATBUF_ST_RDEV
193 ret
= getttyname_r (buf
, buflen
, st
.st_rdev
, st
.st_ino
,
196 ret
= getttyname_r (buf
, buflen
, st
.st_dev
, st
.st_ino
,
204 weak_alias (__ttyname_r
, ttyname_r
)