]>
Commit | Line | Data |
---|---|---|
224c7076 A |
1 | /* |
2 | * Copyright (c) 1988, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | |
35 | static char sccsid[] = "@(#)ttyname.c 8.2 (Berkeley) 1/27/94"; | |
36 | #endif /* LIBC_SCCS and not lint */ | |
37 | #include <sys/cdefs.h> | |
38 | __FBSDID("$FreeBSD: src/lib/libc/gen/ttyname.c,v 1.16 2004/01/06 18:26:14 nectar Exp $"); | |
39 | ||
40 | #include "namespace.h" | |
41 | #include <sys/types.h> | |
42 | #include <sys/stat.h> | |
43 | #include <fcntl.h> | |
44 | #include <dirent.h> | |
45 | #include <stdlib.h> | |
46 | #include <termios.h> | |
47 | #include <unistd.h> | |
48 | #include <string.h> | |
49 | #include <paths.h> | |
50 | #include <pthread.h> | |
51 | #include <errno.h> | |
52 | #include "un-namespace.h" | |
53 | ||
54 | #include "libc_private.h" | |
55 | ||
56 | #ifndef BUILDING_VARIANT | |
57 | static char buf[sizeof(_PATH_DEV) + MAXNAMLEN]; | |
58 | static char *ttyname_threaded(int fd); | |
59 | static char *ttyname_unthreaded(int fd); | |
60 | ||
61 | static pthread_mutex_t ttyname_lock = PTHREAD_MUTEX_INITIALIZER; | |
62 | static pthread_key_t ttyname_key; | |
63 | static int ttyname_init = 0; | |
224c7076 A |
64 | |
65 | char * | |
66 | ttyname(int fd) | |
67 | { | |
68 | char *ret; | |
69 | ||
70 | if (__isthreaded == 0) | |
71 | ret = ttyname_unthreaded(fd); | |
72 | else | |
73 | ret = ttyname_threaded(fd); | |
74 | return (ret); | |
75 | } | |
76 | #endif /* !BUILDING_VARIANT */ | |
77 | ||
78 | #if __DARWIN_UNIX03 | |
79 | int | |
80 | #else /* !__DARWIN_UNIX03 */ | |
81 | char * | |
82 | #endif /* __DARWIN_UNIX03 */ | |
83 | ttyname_r(int fd, char *thrbuf, size_t len) | |
84 | { | |
85 | struct stat sb; | |
86 | ||
87 | #if __DARWIN_UNIX03 | |
88 | if (_fstat(fd, &sb) < 0) | |
89 | return (EBADF); | |
90 | /* Must be a terminal. */ | |
91 | if (!isatty(fd)) | |
92 | return (ENOTTY); | |
93 | /* Must be a character device. */ | |
94 | if (!S_ISCHR(sb.st_mode)) | |
95 | return (ENOTTY); | |
96 | /* Must have enough room */ | |
97 | if (len <= sizeof(_PATH_DEV)) | |
98 | return (ERANGE); | |
99 | #else /* !__DARWIN_UNIX03 */ | |
100 | /* Must be a terminal. */ | |
101 | if (!isatty(fd)) | |
102 | return (NULL); | |
103 | /* Must be a character device. */ | |
104 | if (_fstat(fd, &sb)) | |
105 | return (NULL); | |
106 | if (!S_ISCHR(sb.st_mode)) { | |
107 | errno = ENOTTY; | |
108 | return (NULL); | |
109 | } | |
110 | /* Must have enough room */ | |
111 | if (len <= sizeof(_PATH_DEV)) { | |
112 | errno = ERANGE; | |
113 | return (NULL); | |
114 | } | |
115 | #endif /* __DARWIN_UNIX03 */ | |
116 | ||
117 | strcpy(thrbuf, _PATH_DEV); | |
118 | if (devname_r(sb.st_rdev, S_IFCHR, | |
119 | thrbuf + strlen(thrbuf), len - strlen(thrbuf)) == NULL) | |
120 | #if __DARWIN_UNIX03 | |
121 | return (ERANGE); | |
122 | return (0); | |
123 | #else /* !__DARWIN_UNIX03 */ | |
124 | { | |
125 | errno = ERANGE; | |
126 | return (NULL); | |
127 | } | |
128 | return (thrbuf); | |
129 | #endif /* __DARWIN_UNIX03 */ | |
130 | } | |
131 | ||
132 | #ifndef BUILDING_VARIANT | |
133 | static char * | |
134 | ttyname_threaded(int fd) | |
135 | { | |
136 | char *buf; | |
137 | ||
138 | if (ttyname_init == 0) { | |
139 | _pthread_mutex_lock(&ttyname_lock); | |
140 | if (ttyname_init == 0) { | |
141 | /* __PTK_LIBC_TTYNAME_KEY */ | |
34e8f829 | 142 | ttyname_key = __LIBC_PTHREAD_KEY_TTYNAME; |
224c7076 A |
143 | if (pthread_key_init_np(ttyname_key, free)) { |
144 | int save = errno; | |
145 | _pthread_mutex_unlock(&ttyname_lock); | |
146 | errno = save; | |
147 | return (NULL); | |
148 | } | |
149 | ttyname_init = 1; | |
150 | } | |
151 | _pthread_mutex_unlock(&ttyname_lock); | |
152 | } | |
153 | ||
154 | /* Must have thread specific data field to put data */ | |
155 | if ((buf = _pthread_getspecific(ttyname_key)) == NULL) { | |
156 | if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) { | |
157 | if (_pthread_setspecific(ttyname_key, buf) != 0) { | |
158 | int save = errno; | |
159 | free(buf); | |
160 | errno = save; | |
161 | return (NULL); | |
162 | } | |
163 | } else { | |
164 | return (NULL); | |
165 | } | |
166 | } | |
167 | #if __DARWIN_UNIX03 | |
168 | return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN) == 0 ? buf : NULL); | |
169 | #else /* !__DARWIN_UNIX03 */ | |
170 | return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN)); | |
171 | #endif /* __DARWIN_UNIX03 */ | |
172 | } | |
173 | ||
174 | static char * | |
175 | ttyname_unthreaded(int fd) | |
176 | { | |
177 | struct stat sb; | |
178 | struct termios ttyb; | |
179 | ||
180 | /* Must be a terminal. */ | |
181 | if (tcgetattr(fd, &ttyb) < 0) | |
182 | return (NULL); | |
183 | /* Must be a character device. */ | |
184 | if (_fstat(fd, &sb)) | |
185 | return (NULL); | |
186 | if (!S_ISCHR(sb.st_mode)) { | |
187 | errno = ENOTTY; | |
188 | return (NULL); | |
189 | } | |
190 | ||
191 | strcpy(buf, _PATH_DEV); | |
192 | if (devname_r(sb.st_rdev, S_IFCHR, | |
193 | buf + strlen(buf), sizeof(buf) - strlen(buf)) == NULL) { | |
194 | errno = ERANGE; | |
195 | return (NULL); | |
196 | } | |
197 | return (buf); | |
198 | } | |
199 | #endif /* !BUILDING_VARIANT */ |