]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/ttyname.c.patch
Libc-763.12.tar.gz
[apple/libc.git] / gen / FreeBSD / ttyname.c.patch
CommitLineData
1f2f436a
A
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
3d9156a7
A
4 #include <string.h>
5 #include <paths.h>
6 #include <pthread.h>
3d9156a7 7+#include <errno.h>
3d9156a7
A
8 #include "un-namespace.h"
9
10 #include "libc_private.h"
11
1f2f436a 12-static char buf[sizeof(_PATH_DEV) + MAXNAMLEN];
3d9156a7 13+#ifndef BUILDING_VARIANT
1f2f436a
A
14+static pthread_once_t ttyname_buf_control = PTHREAD_ONCE_INIT;
15+static char *buf = NULL;
3d9156a7
A
16 static char *ttyname_threaded(int fd);
17 static char *ttyname_unthreaded(int fd);
1f2f436a
A
18
19@@ -71,31 +74,63 @@ ttyname(int fd)
3d9156a7
A
20 ret = ttyname_threaded(fd);
21 return (ret);
22 }
23+#endif /* !BUILDING_VARIANT */
24
25+#if __DARWIN_UNIX03
26+int
27+#else /* !__DARWIN_UNIX03 */
28 char *
29-ttyname_r(int fd, char *buf, size_t len)
30+#endif /* __DARWIN_UNIX03 */
31+ttyname_r(int fd, char *thrbuf, size_t len)
32 {
33 struct stat sb;
34- char *rval;
3d9156a7 35-
224c7076
A
36- rval = NULL;
37
3d9156a7
A
38+#if __DARWIN_UNIX03
39+ if (_fstat(fd, &sb) < 0)
40+ return (EBADF);
41 /* Must be a terminal. */
42 if (!isatty(fd))
43- return (rval);
44+ return (ENOTTY);
45 /* Must be a character device. */
224c7076
A
46- if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
47- return (rval);
3d9156a7
A
48+ if (!S_ISCHR(sb.st_mode))
49+ return (ENOTTY);
224c7076
A
50 /* Must have enough room */
51 if (len <= sizeof(_PATH_DEV))
52- return (rval);
3d9156a7
A
53+ return (ERANGE);
54+#else /* !__DARWIN_UNIX03 */
55+ /* Must be a terminal. */
56+ if (!isatty(fd))
57+ return (NULL);
58+ /* Must be a character device. */
224c7076 59+ if (_fstat(fd, &sb))
3d9156a7 60+ return (NULL);
224c7076
A
61+ if (!S_ISCHR(sb.st_mode)) {
62+ errno = ENOTTY;
3d9156a7 63+ return (NULL);
224c7076
A
64+ }
65+ /* Must have enough room */
66+ if (len <= sizeof(_PATH_DEV)) {
67+ errno = ERANGE;
68+ return (NULL);
69+ }
3d9156a7
A
70+#endif /* __DARWIN_UNIX03 */
71
72- strcpy(buf, _PATH_DEV);
73- devname_r(sb.st_rdev, S_IFCHR,
74- buf + strlen(buf), sizeof(buf) - strlen(buf));
75- return (buf);
1f2f436a 76+ strlcpy(thrbuf, _PATH_DEV, len);
3d9156a7
A
77+ if (devname_r(sb.st_rdev, S_IFCHR,
78+ thrbuf + strlen(thrbuf), len - strlen(thrbuf)) == NULL)
79+#if __DARWIN_UNIX03
80+ return (ERANGE);
81+ return (0);
82+#else /* !__DARWIN_UNIX03 */
224c7076
A
83+ {
84+ errno = ERANGE;
3d9156a7 85+ return (NULL);
224c7076 86+ }
3d9156a7
A
87+ return (thrbuf);
88+#endif /* __DARWIN_UNIX03 */
89 }
90
91+#ifndef BUILDING_VARIANT
92 static char *
93 ttyname_threaded(int fd)
94 {
1f2f436a 95@@ -104,8 +139,12 @@ ttyname_threaded(int fd)
224c7076
A
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 */
34e8f829 101+ ttyname_key = __LIBC_PTHREAD_KEY_TTYNAME;
224c7076
A
102+ if (pthread_key_init_np(ttyname_key, free)) {
103+ int save = errno;
104 _pthread_mutex_unlock(&ttyname_lock);
105+ errno = save;
106 return (NULL);
107 }
108 ttyname_init = 1;
1f2f436a 109@@ -117,14 +156,26 @@ ttyname_threaded(int fd)
224c7076
A
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) {
113+ int save = errno;
114 free(buf);
115+ errno = save;
116 return (NULL);
117 }
118 } else {
3d9156a7
A
119 return (NULL);
120 }
121 }
122+#if __DARWIN_UNIX03
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 */
1f2f436a
A
127+}
128+
129+static void
130+ttyname_buf_allocate(void)
131+{
132+ buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN);
3d9156a7
A
133 }
134
135 static char *
1f2f436a 136@@ -137,11 +188,25 @@ ttyname_unthreaded(int fd)
224c7076 137 if (tcgetattr(fd, &ttyb) < 0)
3d9156a7 138 return (NULL);
224c7076
A
139 /* Must be a character device. */
140- if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
141+ if (_fstat(fd, &sb))
1f2f436a 142+ return (NULL);
224c7076
A
143+ if (!S_ISCHR(sb.st_mode)) {
144+ errno = ENOTTY;
1f2f436a
A
145 return (NULL);
146+ }
147+
148+ if (pthread_once(&ttyname_buf_control, ttyname_buf_allocate)
149+ || !buf) {
150+ errno = ENOMEM;
224c7076
A
151+ return (NULL);
152+ }
3d9156a7 153
1f2f436a 154- strcpy(buf, _PATH_DEV);
3d9156a7
A
155- devname_r(sb.st_rdev, S_IFCHR,
156- buf + strlen(buf), sizeof(buf) - strlen(buf));
1f2f436a 157+ strlcpy(buf, _PATH_DEV, sizeof(_PATH_DEV) + MAXNAMLEN);
3d9156a7 158+ if (devname_r(sb.st_rdev, S_IFCHR,
1f2f436a 159+ buf + strlen(buf), sizeof(_PATH_DEV) + MAXNAMLEN - strlen(buf)) == NULL) {
224c7076 160+ errno = ERANGE;
3d9156a7 161+ return (NULL);
224c7076 162+ }
3d9156a7
A
163 return (buf);
164 }
165+#endif /* !BUILDING_VARIANT */