Update.
[glibc.git] / sysdeps / unix / sysv / linux / ttyname_r.c
blob449942a96d8cd437ace5d3ab91dbbdb643432efe
1 /* Copyright (C) 1991, 92, 93, 95, 96, 97, 98 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
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>
29 #include <stdio-common/_itoa.h>
31 static int getttyname_r __P ((int fd, char *buf, size_t buflen,
32 dev_t mydev, ino_t myino, int save,
33 int *dostat)) internal_function;
35 static int
36 internal_function
37 getttyname_r (fd, buf, buflen, mydev, myino, save, dostat)
38 int fd;
39 char *buf;
40 size_t buflen;
41 dev_t mydev;
42 ino_t myino;
43 int save;
44 int *dostat;
46 struct stat st;
47 DIR *dirstream;
48 struct dirent *d;
49 size_t devlen = strlen (buf);
51 dirstream = __opendir (buf);
52 if (dirstream == NULL)
54 *dostat = -1;
55 return errno;
58 while ((d = __readdir (dirstream)) != NULL)
59 if (((ino_t) d->d_fileno == myino || *dostat)
60 && strcmp (d->d_name, "stdin")
61 && strcmp (d->d_name, "stdout")
62 && strcmp (d->d_name, "stderr"))
64 char *cp;
65 size_t needed = _D_EXACT_NAMLEN (d) + 1;
67 if (needed > buflen)
69 *dostat = -1;
70 (void) __closedir (dirstream);
71 __set_errno (ERANGE);
72 return ERANGE;
75 cp = __stpncpy (buf + devlen, d->d_name, needed);
76 cp[0] = '\0';
78 if (stat (buf, &st) == 0
79 #ifdef _STATBUF_ST_RDEV
80 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
81 #else
82 && (ino_t) d->d_fileno == myino && st.st_dev == mydev
83 #endif
86 (void) __closedir (dirstream);
87 __set_errno (save);
88 return 0;
92 (void) __closedir (dirstream);
93 __set_errno (save);
94 /* It is not clear what to return in this case. `isatty' says FD
95 refers to a TTY but no entry in /dev has this inode. */
96 return ENOTTY;
99 /* Store at most BUFLEN character of the pathname of the terminal FD is
100 open on in BUF. Return 0 on success, otherwise an error number. */
102 __ttyname_r (fd, buf, buflen)
103 int fd;
104 char *buf;
105 size_t buflen;
107 char procname[30];
108 struct stat st, st1;
109 int dostat = 0;
110 int save = errno;
111 int ret;
113 /* Test for the absolute minimal size. This makes life easier inside
114 the loop. */
115 if (!buf)
117 __set_errno (EINVAL);
118 return EINVAL;
121 if (buflen < sizeof ("/dev/pts/"))
123 __set_errno (ERANGE);
124 return ERANGE;
127 if (!__isatty (fd))
129 __set_errno (ENOTTY);
130 return ENOTTY;
133 /* We try using the /proc filesystem. */
134 *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
136 ret = __readlink (procname, buf, buflen - 1);
137 if (ret != -1 && buf[0] != '[')
138 return 0;
139 if (ret == -1 && errno == ENAMETOOLONG)
141 __set_errno (ERANGE);
142 return ERANGE;
145 if (fstat (fd, &st) < 0)
146 return errno;
148 /* Prepare the result buffer. */
149 memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/"));
150 buflen -= sizeof ("/dev/pts/") - 1;
152 if (stat (buf, &st1) == 0 && S_ISDIR (st1.st_mode))
154 #ifdef _STATBUF_ST_RDEV
155 ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino, save,
156 &dostat);
157 #else
158 ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino, save,
159 &dostat);
160 #endif
162 else
164 __set_errno (save);
165 ret = ENOENT;
168 if (ret && dostat != -1)
170 buf[sizeof ("/dev/") - 1] = '\0';
171 buflen += sizeof ("pts/") - 1;
172 #ifdef _STATBUF_ST_RDEV
173 ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino, save,
174 &dostat);
175 #else
176 ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino, save,
177 &dostat);
178 #endif
181 if (ret && dostat != -1)
183 buf[sizeof ("/dev/") - 1] = '\0';
184 dostat = 1;
185 #ifdef _STATBUF_ST_RDEV
186 ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino,
187 save, &dostat);
188 #else
189 ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino,
190 save, &dostat);
191 #endif
194 return ret;
197 weak_alias (__ttyname_r, ttyname_r)