]>
Commit | Line | Data |
---|---|---|
45139d5f UD |
1 | /* Copyright (C) 1991, 92, 93, 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 | ||
29 | char *__ttyname = NULL; | |
30 | ||
31 | static char * getttyname __P ((const char *dev, int fd, dev_t mydev, | |
32 | ino_t myino, int save, int *dostat)) | |
33 | internal_function; | |
34 | ||
35 | static char * | |
36 | internal_function | |
37 | getttyname (dev, fd, mydev, myino, save, dostat) | |
38 | const char *dev; | |
39 | int fd; | |
40 | dev_t mydev; | |
41 | ino_t myino; | |
42 | int save; | |
43 | int *dostat; | |
44 | { | |
45 | static char *name; | |
46 | static size_t namelen = 0; | |
47 | struct stat st; | |
48 | DIR *dirstream; | |
49 | struct dirent *d; | |
50 | size_t devlen = strlen (dev) + 1; | |
51 | ||
52 | dirstream = opendir (dev); | |
53 | if (dirstream == NULL) | |
54 | { | |
55 | *dostat = -1; | |
56 | return NULL; | |
57 | } | |
58 | ||
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")) | |
64 | { | |
65 | size_t dlen = _D_ALLOC_NAMLEN (d); | |
66 | if (devlen + dlen > namelen) | |
67 | { | |
68 | free (name); | |
69 | namelen = 2 * (devlen + dlen); /* Big enough. */ | |
70 | name = malloc (namelen); | |
71 | if (! name) | |
72 | { | |
73 | *dostat = -1; | |
74 | /* Perhaps it helps to free the directory stream buffer. */ | |
50304ef0 | 75 | (void) __closedir (dirstream); |
45139d5f UD |
76 | return NULL; |
77 | } | |
78 | *((char *) __mempcpy (name, dev, devlen - 1)) = '/'; | |
79 | } | |
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 | |
84 | #else | |
85 | && (ino_t) d->d_fileno == myino && st.st_dev == mydev | |
86 | #endif | |
87 | ) | |
88 | { | |
50304ef0 | 89 | (void) __closedir (dirstream); |
45139d5f UD |
90 | __ttyname = name; |
91 | __set_errno (save); | |
92 | return name; | |
93 | } | |
94 | } | |
95 | ||
50304ef0 | 96 | (void) __closedir (dirstream); |
45139d5f UD |
97 | __set_errno (save); |
98 | return NULL; | |
99 | } | |
100 | ||
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. */ | |
103 | char * | |
104 | ttyname (fd) | |
105 | int fd; | |
106 | { | |
107 | struct stat st, st1; | |
108 | int dostat = 0; | |
109 | char *name; | |
110 | int save = errno; | |
111 | ||
112 | if (!__isatty (fd)) | |
113 | return NULL; | |
114 | ||
115 | if (fstat (fd, &st) < 0) | |
116 | return NULL; | |
117 | ||
118 | if (stat ("/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode)) | |
119 | { | |
120 | #ifdef _STATBUF_ST_RDEV | |
121 | name = getttyname ("/dev/pts", fd, st.st_rdev, st.st_ino, save, &dostat); | |
122 | #else | |
123 | name = getttyname ("/dev/pts", fd, st.st_dev, st.st_ino, save, &dostat); | |
124 | #endif | |
125 | } | |
126 | else | |
127 | { | |
128 | __set_errno (save); | |
129 | name = NULL; | |
130 | } | |
131 | ||
132 | if (!name && dostat != -1) | |
133 | { | |
134 | #ifdef _STATBUF_ST_RDEV | |
135 | name = getttyname ("/dev", fd, st.st_rdev, st.st_ino, save, &dostat); | |
136 | #else | |
137 | name = getttyname ("/dev", fd, st.st_dev, st.st_ino, save, &dostat); | |
138 | #endif | |
139 | } | |
140 | ||
141 | if (!name && dostat != -1) | |
142 | { | |
143 | dostat = 1; | |
144 | #ifdef _STATBUF_ST_RDEV | |
145 | name = getttyname ("/dev", fd, st.st_rdev, st.st_ino, save, &dostat); | |
146 | #else | |
147 | name = getttyname ("/dev", fd, st.st_dev, st.st_ino, save, &dostat); | |
148 | #endif | |
149 | } | |
150 | ||
151 | return name; | |
152 | } |