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. */
23 #include <sys/types.h>
29 static 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
;
35 getttyname_r (fd
, buf
, buflen
, mydev
, myino
, save
, dostat
)
47 size_t devlen
= strlen (buf
);
49 dirstream
= __opendir (buf
);
50 if (dirstream
== NULL
)
56 while ((d
= __readdir (dirstream
)) != NULL
)
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"))
63 size_t needed
= _D_EXACT_NAMLEN (d
) + 1;
68 (void) __closedir (dirstream
);
73 cp
= __stpncpy (buf
+ devlen
, d
->d_name
, needed
);
76 if (stat (buf
, &st
) == 0
77 #ifdef _STATBUF_ST_RDEV
78 && S_ISCHR (st
.st_mode
) && st
.st_rdev
== mydev
80 && (ino_t
) d
->d_fileno
== myino
&& st
.st_dev
== mydev
84 (void) __closedir (dirstream
);
90 (void) __closedir (dirstream
);
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. */
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. */
100 __ttyname_r (fd
, buf
, buflen
)
110 /* Test for the absolute minimal size. This makes life easier inside
114 __set_errno (EINVAL
);
118 if (buflen
< sizeof ("/dev/pts/"))
120 __set_errno (ERANGE
);
126 __set_errno (ENOTTY
);
130 if (fstat (fd
, &st
) < 0)
133 /* Prepare the result buffer. */
134 memcpy (buf
, "/dev/pts/", sizeof ("/dev/pts/"));
135 buflen
-= sizeof ("/dev/pts/") - 1;
137 if (stat (buf
, &st1
) == 0 && S_ISDIR (st1
.st_mode
))
139 #ifdef _STATBUF_ST_RDEV
140 ret
= getttyname_r (fd
, buf
, buflen
, st
.st_rdev
, st
.st_ino
, save
,
143 ret
= getttyname_r (fd
, buf
, buflen
, st
.st_dev
, st
.st_ino
, save
,
153 if (ret
&& dostat
!= -1)
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
,
161 ret
= getttyname_r (fd
, buf
, buflen
, st
.st_dev
, st
.st_ino
, save
,
166 if (ret
&& dostat
!= -1)
168 buf
[sizeof ("/dev/") - 1] = '\0';
170 #ifdef _STATBUF_ST_RDEV
171 ret
= getttyname_r (fd
, buf
, buflen
, st
.st_rdev
, st
.st_ino
,
174 ret
= getttyname_r (fd
, buf
, buflen
, st
.st_dev
, st
.st_ino
,
182 weak_alias (__ttyname_r
, ttyname_r
)