1 /* Copyright (C) 1991, 92, 93, 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 char *__ttyname
= NULL
;
31 static char * getttyname
__P ((int fd
, dev_t mydev
, ino_t myino
,
32 int save
, int *dostat
)) internal_function
;
36 getttyname (fd
, mydev
, myino
, save
, dostat
)
43 static const char dev
[] = "/dev";
45 static size_t namelen
= 0;
50 dirstream
= opendir (dev
);
51 if (dirstream
== NULL
)
57 while ((d
= readdir (dirstream
)) != NULL
)
58 if (((ino_t
) d
->d_fileno
== myino
|| *dostat
)
59 && strcmp (d
->d_name
, "stdin")
60 && strcmp (d
->d_name
, "stdout")
61 && strcmp (d
->d_name
, "stderr"))
63 size_t dlen
= _D_ALLOC_NAMLEN (d
);
64 if (sizeof (dev
) + dlen
> namelen
)
67 namelen
= 2 * (sizeof (dev
) + dlen
); /* Big enough. */
68 name
= malloc (namelen
);
72 /* Perhaps it helps to free the directory stream buffer. */
73 (void) closedir (dirstream
);
76 *((char *) __mempcpy (name
, dev
, sizeof (dev
) - 1)) = '/';
78 (void) __mempcpy (&name
[sizeof (dev
)], d
->d_name
, dlen
);
79 if (stat (name
, &st
) == 0
80 #ifdef _STATBUF_ST_RDEV
81 && S_ISCHR (st
.st_mode
) && st
.st_rdev
== mydev
83 && (ino_t
) d
->d_fileno
== myino
&& st
.st_dev
== mydev
87 (void) closedir (dirstream
);
94 (void) closedir (dirstream
);
99 /* Return the pathname of the terminal FD is open on, or NULL on errors.
100 The returned storage is good only until the next call to this function. */
113 if (fstat (fd
, &st
) < 0)
116 #ifdef _STATBUF_ST_RDEV
117 name
= getttyname (fd
, st
.st_rdev
, st
.st_ino
, save
, &dostat
);
119 name
= getttyname (fd
, st
.st_dev
, st
.st_ino
, save
, &dostat
);
122 if (!name
&& dostat
!= -1)
125 #ifdef _STATBUF_ST_RDEV
126 name
= getttyname (fd
, st
.st_rdev
, st
.st_ino
, save
, &dostat
);
128 name
= getttyname (fd
, st
.st_dev
, st
.st_ino
, save
, &dostat
);