1 --- ttyname.c.orig 2010-05-01 19:18:09.000000000 -0700
2 +++ ttyname.c 2010-05-03 10:18:47.000000000 -0700
3 @@ -48,11 +48,14 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/tty
8 #include "un-namespace.h"
10 #include "libc_private.h"
12 -static char buf[sizeof(_PATH_DEV) + MAXNAMLEN];
13 +#ifndef BUILDING_VARIANT
14 +static pthread_once_t ttyname_buf_control = PTHREAD_ONCE_INIT;
15 +static char *buf = NULL;
16 static char *ttyname_threaded(int fd);
17 static char *ttyname_unthreaded(int fd);
19 @@ -71,31 +74,63 @@ ttyname(int fd)
20 ret = ttyname_threaded(fd);
23 +#endif /* !BUILDING_VARIANT */
27 +#else /* !__DARWIN_UNIX03 */
29 -ttyname_r(int fd, char *buf, size_t len)
30 +#endif /* __DARWIN_UNIX03 */
31 +ttyname_r(int fd, char *thrbuf, size_t len)
39 + if (_fstat(fd, &sb) < 0)
41 /* Must be a terminal. */
45 /* Must be a character device. */
46 - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
48 + if (!S_ISCHR(sb.st_mode))
50 /* Must have enough room */
51 if (len <= sizeof(_PATH_DEV))
54 +#else /* !__DARWIN_UNIX03 */
55 + /* Must be a terminal. */
58 + /* Must be a character device. */
59 + if (_fstat(fd, &sb))
61 + if (!S_ISCHR(sb.st_mode)) {
65 + /* Must have enough room */
66 + if (len <= sizeof(_PATH_DEV)) {
70 +#endif /* __DARWIN_UNIX03 */
72 - strcpy(buf, _PATH_DEV);
73 - devname_r(sb.st_rdev, S_IFCHR,
74 - buf + strlen(buf), sizeof(buf) - strlen(buf));
76 + strlcpy(thrbuf, _PATH_DEV, len);
77 + if (devname_r(sb.st_rdev, S_IFCHR,
78 + thrbuf + strlen(thrbuf), len - strlen(thrbuf)) == NULL)
82 +#else /* !__DARWIN_UNIX03 */
88 +#endif /* __DARWIN_UNIX03 */
91 +#ifndef BUILDING_VARIANT
93 ttyname_threaded(int fd)
95 @@ -104,8 +139,12 @@ ttyname_threaded(int fd)
96 if (ttyname_init == 0) {
97 _pthread_mutex_lock(&ttyname_lock);
98 if (ttyname_init == 0) {
99 - if (_pthread_key_create(&ttyname_key, free)) {
100 + /* __PTK_LIBC_TTYNAME_KEY */
101 + ttyname_key = __LIBC_PTHREAD_KEY_TTYNAME;
102 + if (pthread_key_init_np(ttyname_key, free)) {
104 _pthread_mutex_unlock(&ttyname_lock);
109 @@ -117,14 +156,26 @@ ttyname_threaded(int fd)
110 if ((buf = _pthread_getspecific(ttyname_key)) == NULL) {
111 if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) {
112 if (_pthread_setspecific(ttyname_key, buf) != 0) {
123 + return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN) == 0 ? buf : NULL);
124 +#else /* !__DARWIN_UNIX03 */
125 return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN));
126 +#endif /* __DARWIN_UNIX03 */
130 +ttyname_buf_allocate(void)
132 + buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN);
136 @@ -137,11 +188,25 @@ ttyname_unthreaded(int fd)
137 if (tcgetattr(fd, &ttyb) < 0)
139 /* Must be a character device. */
140 - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
141 + if (_fstat(fd, &sb))
143 + if (!S_ISCHR(sb.st_mode)) {
148 + if (pthread_once(&ttyname_buf_control, ttyname_buf_allocate)
154 - strcpy(buf, _PATH_DEV);
155 - devname_r(sb.st_rdev, S_IFCHR,
156 - buf + strlen(buf), sizeof(buf) - strlen(buf));
157 + strlcpy(buf, _PATH_DEV, sizeof(_PATH_DEV) + MAXNAMLEN);
158 + if (devname_r(sb.st_rdev, S_IFCHR,
159 + buf + strlen(buf), sizeof(_PATH_DEV) + MAXNAMLEN - strlen(buf)) == NULL) {
165 +#endif /* !BUILDING_VARIANT */