]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/ttyname.c
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 ((const char *dev
, int fd
, dev_t mydev
,
32 ino_t myino
, int save
, int *dostat
))
37 getttyname (dev
, fd
, mydev
, myino
, save
, dostat
)
46 static size_t namelen
= 0;
50 size_t devlen
= strlen (dev
) + 1;
52 dirstream
= opendir (dev
);
53 if (dirstream
== NULL
)
59 while ((d
= readdir (dirstream
)) != NULL
)
60 if (((ino_t
) d
->d_fileno
== myino
|| *dostat
)
61 && strcmp (d
->d_name
, "stdin")
62 && strcmp (d
->d_name
, "stdout")
63 && strcmp (d
->d_name
, "stderr"))
65 size_t dlen
= _D_ALLOC_NAMLEN (d
);
66 if (devlen
+ dlen
> namelen
)
69 namelen
= 2 * (devlen
+ dlen
); /* Big enough. */
70 name
= malloc (namelen
);
74 /* Perhaps it helps to free the directory stream buffer. */
75 (void) closedir (dirstream
);
78 *((char *) __mempcpy (name
, dev
, devlen
- 1)) = '/';
80 memcpy (&name
[devlen
], d
->d_name
, dlen
);
81 if (stat (name
, &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
);
96 (void) closedir (dirstream
);
101 /* Return the pathname of the terminal FD is open on, or NULL on errors.
102 The returned storage is good only until the next call to this function. */
115 if (fstat (fd
, &st
) < 0)
118 if (stat ("/dev/pts", &st1
) == 0 && S_ISDIR (st1
.st_mode
))
120 #ifdef _STATBUF_ST_RDEV
121 name
= getttyname ("/dev/pts", fd
, st
.st_rdev
, st
.st_ino
, save
, &dostat
);
123 name
= getttyname ("/dev/pts", fd
, st
.st_dev
, st
.st_ino
, save
, &dostat
);
132 if (!name
&& dostat
!= -1)
134 #ifdef _STATBUF_ST_RDEV
135 name
= getttyname ("/dev", fd
, st
.st_rdev
, st
.st_ino
, save
, &dostat
);
137 name
= getttyname ("/dev", fd
, st
.st_dev
, st
.st_ino
, save
, &dostat
);
141 if (!name
&& dostat
!= -1)
144 #ifdef _STATBUF_ST_RDEV
145 name
= getttyname ("/dev", fd
, st
.st_rdev
, st
.st_ino
, save
, &dostat
);
147 name
= getttyname ("/dev", fd
, st
.st_dev
, st
.st_ino
, save
, &dostat
);