1 --- ttyname.c.orig 2007-02-05 14:38:48.000000000 -0800
2 +++ ttyname.c 2007-02-05 14:40:13.000000000 -0800
8 #include "un-namespace.h"
10 #include "libc_private.h"
12 +#ifndef BUILDING_VARIANT
13 static char buf[sizeof(_PATH_DEV) + MAXNAMLEN];
14 static char *ttyname_threaded(int fd);
15 static char *ttyname_unthreaded(int fd);
17 static pthread_mutex_t ttyname_lock = PTHREAD_MUTEX_INITIALIZER;
18 static pthread_key_t ttyname_key;
19 static int ttyname_init = 0;
20 +extern int __pthread_tsd_first;
25 ret = ttyname_threaded(fd);
28 +#endif /* !BUILDING_VARIANT */
32 +#else /* !__DARWIN_UNIX03 */
34 -ttyname_r(int fd, char *buf, size_t len)
35 +#endif /* __DARWIN_UNIX03 */
36 +ttyname_r(int fd, char *thrbuf, size_t len)
44 + if (_fstat(fd, &sb) < 0)
46 /* Must be a terminal. */
50 /* Must be a character device. */
51 - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
53 + if (!S_ISCHR(sb.st_mode))
55 /* Must have enough room */
56 if (len <= sizeof(_PATH_DEV))
59 +#else /* !__DARWIN_UNIX03 */
60 + /* Must be a terminal. */
63 + /* Must be a character device. */
64 + if (_fstat(fd, &sb))
66 + if (!S_ISCHR(sb.st_mode)) {
70 + /* Must have enough room */
71 + if (len <= sizeof(_PATH_DEV)) {
75 +#endif /* __DARWIN_UNIX03 */
77 - strcpy(buf, _PATH_DEV);
78 - devname_r(sb.st_rdev, S_IFCHR,
79 - buf + strlen(buf), sizeof(buf) - strlen(buf));
81 + strcpy(thrbuf, _PATH_DEV);
82 + if (devname_r(sb.st_rdev, S_IFCHR,
83 + thrbuf + strlen(thrbuf), len - strlen(thrbuf)) == NULL)
87 +#else /* !__DARWIN_UNIX03 */
93 +#endif /* __DARWIN_UNIX03 */
96 +#ifndef BUILDING_VARIANT
98 ttyname_threaded(int fd)
101 if (ttyname_init == 0) {
102 _pthread_mutex_lock(&ttyname_lock);
103 if (ttyname_init == 0) {
104 - if (_pthread_key_create(&ttyname_key, free)) {
105 + /* __PTK_LIBC_TTYNAME_KEY */
106 + ttyname_key = __pthread_tsd_first+1;
107 + if (pthread_key_init_np(ttyname_key, free)) {
109 _pthread_mutex_unlock(&ttyname_lock);
114 @@ -117,14 +156,20 @@
115 if ((buf = _pthread_getspecific(ttyname_key)) == NULL) {
116 if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) {
117 if (_pthread_setspecific(ttyname_key, buf) != 0) {
128 + return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN) == 0 ? buf : NULL);
129 +#else /* !__DARWIN_UNIX03 */
130 return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN));
131 +#endif /* __DARWIN_UNIX03 */
135 @@ -137,11 +182,19 @@
136 if (tcgetattr(fd, &ttyb) < 0)
138 /* Must be a character device. */
139 - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
140 + if (_fstat(fd, &sb))
142 + if (!S_ISCHR(sb.st_mode)) {
147 strcpy(buf, _PATH_DEV);
148 - devname_r(sb.st_rdev, S_IFCHR,
149 - buf + strlen(buf), sizeof(buf) - strlen(buf));
150 + if (devname_r(sb.st_rdev, S_IFCHR,
151 + buf + strlen(buf), sizeof(buf) - strlen(buf)) == NULL) {
157 +#endif /* !BUILDING_VARIANT */