]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/ttyname.c
* include/libc-symbols.h (__libc_freeres_fn_section, libc_freeres_fn):
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / ttyname.c
1 /* Copyright (C) 1991,92,93,1996-2001,2002 Free Software Foundation, Inc.
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
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.
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
12 Lesser General Public License for more details.
13
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. */
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>
25 #include <unistd.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include <stdio-common/_itoa.h>
30
31 #if 0
32 /* Is this used anywhere? It is not exported. */
33 char *__ttyname;
34 #endif
35
36 static char *getttyname (const char *dev, dev_t mydev,
37 ino64_t myino, int save, int *dostat)
38 internal_function;
39
40
41 libc_freeres_ptr (static char *getttyname_name);
42
43 static char *
44 internal_function
45 getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat)
46 {
47 static size_t namelen;
48 struct stat64 st;
49 DIR *dirstream;
50 struct dirent64 *d;
51 size_t devlen = strlen (dev) + 1;
52
53 dirstream = __opendir (dev);
54 if (dirstream == NULL)
55 {
56 *dostat = -1;
57 return NULL;
58 }
59
60 while ((d = __readdir64 (dirstream)) != NULL)
61 if ((d->d_fileno == myino || *dostat)
62 && strcmp (d->d_name, "stdin")
63 && strcmp (d->d_name, "stdout")
64 && strcmp (d->d_name, "stderr"))
65 {
66 size_t dlen = _D_ALLOC_NAMLEN (d);
67 if (devlen + dlen > namelen)
68 {
69 free (getttyname_name);
70 namelen = 2 * (devlen + dlen); /* Big enough. */
71 getttyname_name = malloc (namelen);
72 if (! getttyname_name)
73 {
74 *dostat = -1;
75 /* Perhaps it helps to free the directory stream buffer. */
76 (void) __closedir (dirstream);
77 return NULL;
78 }
79 *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
80 }
81 memcpy (&getttyname_name[devlen], d->d_name, dlen);
82 if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0
83 #ifdef _STATBUF_ST_RDEV
84 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
85 #else
86 && d->d_fileno == myino && st.st_dev == mydev
87 #endif
88 )
89 {
90 (void) __closedir (dirstream);
91 #if 0
92 __ttyname = getttyname_name;
93 #endif
94 __set_errno (save);
95 return getttyname_name;
96 }
97 }
98
99 (void) __closedir (dirstream);
100 __set_errno (save);
101 return NULL;
102 }
103
104
105 /* Static buffer in `ttyname'. */
106 libc_freeres_ptr (static char *ttyname_buf);
107
108
109 /* Return the pathname of the terminal FD is open on, or NULL on errors.
110 The returned storage is good only until the next call to this function. */
111 char *
112 ttyname (int fd)
113 {
114 static size_t buflen;
115 char procname[30];
116 struct stat64 st, st1;
117 int dostat = 0;
118 char *name;
119 int save = errno;
120 int len;
121
122 if (!__isatty (fd))
123 return NULL;
124
125 /* We try using the /proc filesystem. */
126 *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
127
128 if (buflen == 0)
129 {
130 buflen = 4095;
131 ttyname_buf = (char *) malloc (buflen + 1);
132 if (ttyname_buf == NULL)
133 {
134 buflen = 0;
135 return NULL;
136 }
137 }
138
139 len = __readlink (procname, ttyname_buf, buflen);
140 if (len != -1
141 /* This is for Linux 2.0. */
142 && ttyname_buf[0] != '[')
143 {
144 if ((size_t) len >= buflen)
145 return NULL;
146 /* readlink need not terminate the string. */
147 ttyname_buf[len] = '\0';
148 return ttyname_buf;
149 }
150
151 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
152 return NULL;
153
154 if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
155 {
156 #ifdef _STATBUF_ST_RDEV
157 name = getttyname ("/dev/pts", st.st_rdev, st.st_ino, save, &dostat);
158 #else
159 name = getttyname ("/dev/pts", st.st_dev, st.st_ino, save, &dostat);
160 #endif
161 }
162 else
163 {
164 __set_errno (save);
165 name = NULL;
166 }
167
168 if (!name && dostat != -1)
169 {
170 #ifdef _STATBUF_ST_RDEV
171 name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
172 #else
173 name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
174 #endif
175 }
176
177 if (!name && dostat != -1)
178 {
179 dostat = 1;
180 #ifdef _STATBUF_ST_RDEV
181 name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
182 #else
183 name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
184 #endif
185 }
186
187 return name;
188 }