]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/ttyname.c.patch
Libc-763.11.tar.gz
[apple/libc.git] / gen / FreeBSD / ttyname.c.patch
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
4 #include <string.h>
5 #include <paths.h>
6 #include <pthread.h>
7 +#include <errno.h>
8 #include "un-namespace.h"
9
10 #include "libc_private.h"
11
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);
18
19 @@ -71,31 +74,63 @@ ttyname(int fd)
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;
35 -
36 - rval = NULL;
37
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. */
46 - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
47 - return (rval);
48 + if (!S_ISCHR(sb.st_mode))
49 + return (ENOTTY);
50 /* Must have enough room */
51 if (len <= sizeof(_PATH_DEV))
52 - return (rval);
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. */
59 + if (_fstat(fd, &sb))
60 + return (NULL);
61 + if (!S_ISCHR(sb.st_mode)) {
62 + errno = ENOTTY;
63 + return (NULL);
64 + }
65 + /* Must have enough room */
66 + if (len <= sizeof(_PATH_DEV)) {
67 + errno = ERANGE;
68 + return (NULL);
69 + }
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);
76 + strlcpy(thrbuf, _PATH_DEV, len);
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 */
83 + {
84 + errno = ERANGE;
85 + return (NULL);
86 + }
87 + return (thrbuf);
88 +#endif /* __DARWIN_UNIX03 */
89 }
90
91 +#ifndef BUILDING_VARIANT
92 static char *
93 ttyname_threaded(int fd)
94 {
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)) {
103 + int save = errno;
104 _pthread_mutex_unlock(&ttyname_lock);
105 + errno = save;
106 return (NULL);
107 }
108 ttyname_init = 1;
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) {
113 + int save = errno;
114 free(buf);
115 + errno = save;
116 return (NULL);
117 }
118 } else {
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 */
127 +}
128 +
129 +static void
130 +ttyname_buf_allocate(void)
131 +{
132 + buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN);
133 }
134
135 static char *
136 @@ -137,11 +188,25 @@ ttyname_unthreaded(int fd)
137 if (tcgetattr(fd, &ttyb) < 0)
138 return (NULL);
139 /* Must be a character device. */
140 - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
141 + if (_fstat(fd, &sb))
142 + return (NULL);
143 + if (!S_ISCHR(sb.st_mode)) {
144 + errno = ENOTTY;
145 return (NULL);
146 + }
147 +
148 + if (pthread_once(&ttyname_buf_control, ttyname_buf_allocate)
149 + || !buf) {
150 + errno = ENOMEM;
151 + return (NULL);
152 + }
153
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) {
160 + errno = ERANGE;
161 + return (NULL);
162 + }
163 return (buf);
164 }
165 +#endif /* !BUILDING_VARIANT */