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 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.
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.
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
23 #include <sys/types.h>
30 # define MIN(a, b) ((a) < (b) ? (a) : (b))
33 static const char dev
[] = "/dev";
35 static int getttyname_r
__P ((int fd
, char *buf
, size_t buflen
,
36 dev_t mydev
, ino_t myino
, int save
,
37 int *dostat
)) internal_function
;
41 getttyname_r (fd
, buf
, buflen
, mydev
, myino
, save
, dostat
)
54 dirstream
= __opendir (dev
);
55 if (dirstream
== NULL
)
61 while ((d
= __readdir (dirstream
)) != NULL
)
62 if (((ino_t
) d
->d_fileno
== myino
|| *dostat
)
63 && strcmp (d
->d_name
, "stdin")
64 && strcmp (d
->d_name
, "stdout")
65 && strcmp (d
->d_name
, "stderr"))
68 size_t needed
= _D_EXACT_NAMLEN (d
) + 1;
73 (void) __closedir (dirstream
);
78 cp
= __stpncpy (&buf
[sizeof (dev
)], d
->d_name
, needed
);
81 if (stat (buf
, &st
) == 0
82 #ifdef _STATBUF_ST_RDEV
83 && S_ISCHR (st
.st_mode
) && st
.st_rdev
== mydev
85 && (ino_t
) d
->d_fileno
== myino
&& st
.st_dev
== mydev
89 (void) __closedir (dirstream
);
95 (void) __closedir (dirstream
);
97 /* It is not clear what to return in this case. `isatty' says FD
98 refers to a TTY but no entry in /dev has this inode. */
102 /* Store at most BUFLEN character of the pathname of the terminal FD is
103 open on in BUF. Return 0 on success, otherwise an error number. */
105 __ttyname_r (fd
, buf
, buflen
)
115 /* Test for the absolute minimal size. This makes life easier inside
119 __set_errno (EINVAL
);
123 if (buflen
< (int) (sizeof (dev
) + 1))
125 __set_errno (ERANGE
);
131 __set_errno (ENOTTY
);
135 if (fstat (fd
, &st
) < 0)
138 /* Prepare the result buffer. */
139 memcpy (buf
, dev
, sizeof (dev
) - 1);
140 buf
[sizeof (dev
) - 1] = '/';
141 buflen
-= sizeof (dev
);
143 #ifdef _STATBUF_ST_RDEV
144 ret
= getttyname_r (fd
, buf
, buflen
, st
.st_rdev
, st
.st_ino
, save
,
147 ret
= getttyname_r (fd
, buf
, buflen
, st
.st_dev
, st
.st_ino
, save
,
151 if (ret
&& dostat
!= -1)
154 #ifdef _STATBUF_ST_RDEV
155 ret
= getttyname_r (fd
, buf
, buflen
, st
.st_rdev
, st
.st_ino
,
158 ret
= getttyname_r (fd
, buf
, buflen
, st
.st_dev
, st
.st_ino
,
166 weak_alias (__ttyname_r
, ttyname_r
)