]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/ttyname_r.c
(__pthread_trylock): Define inline. (__pthread_lock): Add extra parameter to declarat...
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / ttyname_r.c
CommitLineData
45139d5f
UD
1/* Copyright (C) 1991, 92, 93, 95, 96, 97, 98 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 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.
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 Library General Public License for more details.
13
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. */
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
29static int getttyname_r __P ((int fd, char *buf, size_t buflen,
30 dev_t mydev, ino_t myino, int save,
31 int *dostat)) internal_function;
32
33static int
34internal_function
35getttyname_r (fd, buf, buflen, mydev, myino, save, dostat)
36 int fd;
37 char *buf;
38 size_t buflen;
39 dev_t mydev;
40 ino_t myino;
41 int save;
42 int *dostat;
43{
44 struct stat st;
45 DIR *dirstream;
46 struct dirent *d;
47 size_t devlen = strlen (buf);
48
50304ef0 49 dirstream = __opendir (buf);
45139d5f
UD
50 if (dirstream == NULL)
51 {
52 *dostat = -1;
53 return errno;
54 }
55
50304ef0 56 while ((d = __readdir (dirstream)) != NULL)
45139d5f
UD
57 if (((ino_t) d->d_fileno == myino || *dostat)
58 && strcmp (d->d_name, "stdin")
59 && strcmp (d->d_name, "stdout")
60 && strcmp (d->d_name, "stderr"))
61 {
62 char *cp;
63 size_t needed = _D_EXACT_NAMLEN (d) + 1;
64
65 if (needed > buflen)
66 {
67 *dostat = -1;
50304ef0 68 (void) __closedir (dirstream);
45139d5f
UD
69 __set_errno (ERANGE);
70 return ERANGE;
71 }
72
73 cp = __stpncpy (buf + devlen, d->d_name, needed);
74 cp[0] = '\0';
75
76 if (stat (buf, &st) == 0
77#ifdef _STATBUF_ST_RDEV
78 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
79#else
80 && (ino_t) d->d_fileno == myino && st.st_dev == mydev
81#endif
82 )
83 {
50304ef0 84 (void) __closedir (dirstream);
45139d5f
UD
85 __set_errno (save);
86 return 0;
87 }
88 }
89
50304ef0 90 (void) __closedir (dirstream);
45139d5f
UD
91 __set_errno (save);
92 /* It is not clear what to return in this case. `isatty' says FD
93 refers to a TTY but no entry in /dev has this inode. */
94 return ENOTTY;
95}
96
97/* Store at most BUFLEN character of the pathname of the terminal FD is
98 open on in BUF. Return 0 on success, otherwise an error number. */
99int
100__ttyname_r (fd, buf, buflen)
101 int fd;
102 char *buf;
103 size_t buflen;
104{
105 struct stat st, st1;
106 int dostat = 0;
107 int save = errno;
108 int ret;
109
110 /* Test for the absolute minimal size. This makes life easier inside
111 the loop. */
112 if (!buf)
113 {
114 __set_errno (EINVAL);
115 return EINVAL;
116 }
117
118 if (buflen < sizeof ("/dev/pts/"))
119 {
120 __set_errno (ERANGE);
121 return ERANGE;
122 }
123
124 if (!__isatty (fd))
125 {
126 __set_errno (ENOTTY);
127 return ENOTTY;
128 }
129
130 if (fstat (fd, &st) < 0)
131 return errno;
132
133 /* Prepare the result buffer. */
134 memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/"));
135 buflen -= sizeof ("/dev/pts/") - 1;
136
137 if (stat (buf, &st1) == 0 && S_ISDIR (st1.st_mode))
138 {
139#ifdef _STATBUF_ST_RDEV
140 ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino, save,
141 &dostat);
142#else
143 ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino, save,
144 &dostat);
145#endif
146 }
147 else
148 {
149 __set_errno (save);
150 ret = ENOENT;
151 }
152
153 if (ret && dostat != -1)
154 {
155 buf[sizeof ("/dev/") - 1] = '\0';
156 buflen += sizeof ("pts/") - 1;
157#ifdef _STATBUF_ST_RDEV
158 ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino, save,
159 &dostat);
160#else
161 ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino, save,
162 &dostat);
163#endif
164 }
165
166 if (ret && dostat != -1)
167 {
168 buf[sizeof ("/dev/") - 1] = '\0';
169 dostat = 1;
170#ifdef _STATBUF_ST_RDEV
171 ret = getttyname_r (fd, buf, buflen, st.st_rdev, st.st_ino,
172 save, &dostat);
173#else
174 ret = getttyname_r (fd, buf, buflen, st.st_dev, st.st_ino,
175 save, &dostat);
176#endif
177 }
178
179 return ret;
180}
181
182weak_alias (__ttyname_r, ttyname_r)