Update.
[glibc.git] / sysdeps / unix / sysv / linux / ttyname_r.c
blob16fb7a0972ae1d9617e9572862534630defd4d7d
1 /* Copyright (C) 1991,92,93,95,96,97,98,99,2000 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 (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 (char *buf, size_t buflen, dev_t mydev, ino_t myino,
38 int save, int *dostat)
40 struct stat st;
41 DIR *dirstream;
42 struct dirent *d;
43 size_t devlen = strlen (buf);
45 dirstream = __opendir (buf);
46 if (dirstream == NULL)
48 *dostat = -1;
49 return errno;
52 while ((d = __readdir (dirstream)) != NULL)
53 if (((ino_t) d->d_fileno == myino || *dostat)
54 && strcmp (d->d_name, "stdin")
55 && strcmp (d->d_name, "stdout")
56 && strcmp (d->d_name, "stderr"))
58 char *cp;
59 size_t needed = _D_EXACT_NAMLEN (d) + 1;
61 if (needed > buflen)
63 *dostat = -1;
64 (void) __closedir (dirstream);
65 __set_errno (ERANGE);
66 return ERANGE;
69 cp = __stpncpy (buf + devlen, d->d_name, needed);
70 cp[0] = '\0';
72 if (__xstat (_STAT_VER, buf, &st) == 0
73 #ifdef _STATBUF_ST_RDEV
74 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
75 #else
76 && (ino_t) d->d_fileno == myino && st.st_dev == mydev
77 #endif
80 (void) __closedir (dirstream);
81 __set_errno (save);
82 return 0;
86 (void) __closedir (dirstream);
87 __set_errno (save);
88 /* It is not clear what to return in this case. `isatty' says FD
89 refers to a TTY but no entry in /dev has this inode. */
90 return ENOTTY;
93 /* Store at most BUFLEN character of the pathname of the terminal FD is
94 open on in BUF. Return 0 on success, otherwise an error number. */
95 int
96 __ttyname_r (int fd, char *buf, size_t buflen)
98 char procname[30];
99 struct stat st, st1;
100 int dostat = 0;
101 int save = errno;
102 int ret;
104 /* Test for the absolute minimal size. This makes life easier inside
105 the loop. */
106 if (!buf)
108 __set_errno (EINVAL);
109 return EINVAL;
112 if (buflen < sizeof ("/dev/pts/"))
114 __set_errno (ERANGE);
115 return ERANGE;
118 if (!__isatty (fd))
120 __set_errno (ENOTTY);
121 return ENOTTY;
124 /* We try using the /proc filesystem. */
125 *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
127 ret = __readlink (procname, buf, buflen - 1);
128 if (ret != -1 && buf[0] != '[')
130 buf[ret] = '\0';
131 return 0;
133 if (ret == -1 && errno == ENAMETOOLONG)
135 __set_errno (ERANGE);
136 return ERANGE;
139 if (__fxstat (_STAT_VER, fd, &st) < 0)
140 return errno;
142 /* Prepare the result buffer. */
143 memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/"));
144 buflen -= sizeof ("/dev/pts/") - 1;
146 if (__xstat (_STAT_VER, buf, &st1) == 0 && S_ISDIR (st1.st_mode))
148 #ifdef _STATBUF_ST_RDEV
149 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
150 &dostat);
151 #else
152 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
153 &dostat);
154 #endif
156 else
158 __set_errno (save);
159 ret = ENOENT;
162 if (ret && dostat != -1)
164 buf[sizeof ("/dev/") - 1] = '\0';
165 buflen += sizeof ("pts/") - 1;
166 #ifdef _STATBUF_ST_RDEV
167 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
168 &dostat);
169 #else
170 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
171 &dostat);
172 #endif
175 if (ret && dostat != -1)
177 buf[sizeof ("/dev/") - 1] = '\0';
178 dostat = 1;
179 #ifdef _STATBUF_ST_RDEV
180 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino,
181 save, &dostat);
182 #else
183 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino,
184 save, &dostat);
185 #endif
188 return ret;
191 weak_alias (__ttyname_r, ttyname_r)