]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/ttyname.c
2.5-18.1
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / ttyname.c
CommitLineData
0ecb606c 1/* Copyright (C) 1991,92,93,1996-2002,2006 Free Software Foundation, Inc.
45139d5f
UD
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
45139d5f
UD
8
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
41bdb6e2 12 Lesser General Public License for more details.
45139d5f 13
41bdb6e2
AJ
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
17 02111-1307 USA. */
45139d5f
UD
18
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>
0ecb606c 25#include <termios.h>
45139d5f
UD
26#include <unistd.h>
27#include <string.h>
28#include <stdlib.h>
29
c5e340c7 30#include <stdio-common/_itoa.h>
0ecb606c 31#include <kernel-features.h>
c5e340c7 32
6455d255
UD
33#if 0
34/* Is this used anywhere? It is not exported. */
c4563d2d 35char *__ttyname;
6455d255 36#endif
45139d5f 37
b8fd5502 38static char *getttyname (const char *dev, dev_t mydev,
8edf6e0d 39 ino64_t myino, int save, int *dostat)
45139d5f
UD
40 internal_function;
41
b8fd5502 42
c877418f 43libc_freeres_ptr (static char *getttyname_name);
b8fd5502 44
45139d5f 45static char *
0ecb606c 46internal_function attribute_compat_text_section
8edf6e0d 47getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat)
45139d5f 48{
c4563d2d 49 static size_t namelen;
8edf6e0d 50 struct stat64 st;
45139d5f 51 DIR *dirstream;
2958e6cc 52 struct dirent64 *d;
45139d5f
UD
53 size_t devlen = strlen (dev) + 1;
54
4aebaa6b 55 dirstream = __opendir (dev);
45139d5f
UD
56 if (dirstream == NULL)
57 {
58 *dostat = -1;
59 return NULL;
60 }
61
2958e6cc
UD
62 while ((d = __readdir64 (dirstream)) != NULL)
63 if ((d->d_fileno == myino || *dostat)
45139d5f
UD
64 && strcmp (d->d_name, "stdin")
65 && strcmp (d->d_name, "stdout")
66 && strcmp (d->d_name, "stderr"))
67 {
68 size_t dlen = _D_ALLOC_NAMLEN (d);
69 if (devlen + dlen > namelen)
70 {
b8fd5502 71 free (getttyname_name);
45139d5f 72 namelen = 2 * (devlen + dlen); /* Big enough. */
b8fd5502
UD
73 getttyname_name = malloc (namelen);
74 if (! getttyname_name)
45139d5f
UD
75 {
76 *dostat = -1;
77 /* Perhaps it helps to free the directory stream buffer. */
50304ef0 78 (void) __closedir (dirstream);
45139d5f
UD
79 return NULL;
80 }
b8fd5502 81 *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
45139d5f 82 }
b8fd5502 83 memcpy (&getttyname_name[devlen], d->d_name, dlen);
8edf6e0d 84 if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0
45139d5f
UD
85#ifdef _STATBUF_ST_RDEV
86 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
87#else
2958e6cc 88 && d->d_fileno == myino && st.st_dev == mydev
45139d5f
UD
89#endif
90 )
91 {
50304ef0 92 (void) __closedir (dirstream);
6455d255 93#if 0
b8fd5502 94 __ttyname = getttyname_name;
6455d255 95#endif
45139d5f 96 __set_errno (save);
b8fd5502 97 return getttyname_name;
45139d5f
UD
98 }
99 }
100
50304ef0 101 (void) __closedir (dirstream);
45139d5f
UD
102 __set_errno (save);
103 return NULL;
104}
105
b8fd5502
UD
106
107/* Static buffer in `ttyname'. */
c877418f 108libc_freeres_ptr (static char *ttyname_buf);
b8fd5502
UD
109
110
45139d5f
UD
111/* Return the pathname of the terminal FD is open on, or NULL on errors.
112 The returned storage is good only until the next call to this function. */
113char *
bcaad6ee 114ttyname (int fd)
45139d5f 115{
c4563d2d 116 static size_t buflen;
c5e340c7 117 char procname[30];
8edf6e0d 118 struct stat64 st, st1;
45139d5f 119 int dostat = 0;
7d1de115 120 char *name;
45139d5f 121 int save = errno;
0ecb606c 122 struct termios term;
45139d5f 123
0ecb606c
JJ
124 /* isatty check, tcgetattr is used because it sets the correct
125 errno (EBADF resp. ENOTTY) on error. */
126 if (__builtin_expect (__tcgetattr (fd, &term) < 0, 0))
45139d5f
UD
127 return NULL;
128
c5e340c7
UD
129 /* We try using the /proc filesystem. */
130 *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
131
132 if (buflen == 0)
133 {
134 buflen = 4095;
b8fd5502
UD
135 ttyname_buf = (char *) malloc (buflen + 1);
136 if (ttyname_buf == NULL)
c5e340c7
UD
137 {
138 buflen = 0;
139 return NULL;
140 }
141 }
142
0ecb606c
JJ
143 ssize_t len = __readlink (procname, ttyname_buf, buflen);
144 if (__builtin_expect (len == -1 && errno == ENOENT, 0))
145 {
146 __set_errno (EBADF);
147 return NULL;
148 }
149
150 if (__builtin_expect (len != -1
151#ifndef __ASSUME_PROC_SELF_FD_SYMLINK
152 /* This is for Linux 2.0. */
153 && ttyname_buf[0] != '['
154#endif
155 , 1))
7081e0a3 156 {
57b36a0a 157 if ((size_t) len >= buflen)
7081e0a3
UD
158 return NULL;
159 /* readlink need not terminate the string. */
b8fd5502
UD
160 ttyname_buf[len] = '\0';
161 return ttyname_buf;
7081e0a3 162 }
c5e340c7 163
8edf6e0d 164 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
45139d5f
UD
165 return NULL;
166
8edf6e0d 167 if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
45139d5f
UD
168 {
169#ifdef _STATBUF_ST_RDEV
7d1de115 170 name = getttyname ("/dev/pts", st.st_rdev, st.st_ino, save, &dostat);
45139d5f 171#else
7d1de115 172 name = getttyname ("/dev/pts", st.st_dev, st.st_ino, save, &dostat);
45139d5f 173#endif
7d1de115
UD
174 }
175 else
176 {
177 __set_errno (save);
178 name = NULL;
45139d5f
UD
179 }
180
181 if (!name && dostat != -1)
182 {
183#ifdef _STATBUF_ST_RDEV
160698e2 184 name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
45139d5f 185#else
160698e2 186 name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
45139d5f
UD
187#endif
188 }
189
190 if (!name && dostat != -1)
191 {
192 dostat = 1;
193#ifdef _STATBUF_ST_RDEV
160698e2 194 name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
45139d5f 195#else
160698e2 196 name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
45139d5f
UD
197#endif
198 }
199
200 return name;
201}