]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/ttyname.c.patch
Libc-498.1.5.tar.gz
[apple/libc.git] / gen / FreeBSD / ttyname.c.patch
1 --- ttyname.c.orig 2007-02-05 14:38:48.000000000 -0800
2 +++ ttyname.c 2007-02-05 14:40:13.000000000 -0800
3 @@ -48,10 +48,12 @@
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 +#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);
16 @@ -59,6 +61,7 @@
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;
21
22 char *
23 ttyname(int fd)
24 @@ -71,31 +74,63 @@
25 ret = ttyname_threaded(fd);
26 return (ret);
27 }
28 +#endif /* !BUILDING_VARIANT */
29
30 +#if __DARWIN_UNIX03
31 +int
32 +#else /* !__DARWIN_UNIX03 */
33 char *
34 -ttyname_r(int fd, char *buf, size_t len)
35 +#endif /* __DARWIN_UNIX03 */
36 +ttyname_r(int fd, char *thrbuf, size_t len)
37 {
38 struct stat sb;
39 - char *rval;
40 -
41 - rval = NULL;
42
43 +#if __DARWIN_UNIX03
44 + if (_fstat(fd, &sb) < 0)
45 + return (EBADF);
46 /* Must be a terminal. */
47 if (!isatty(fd))
48 - return (rval);
49 + return (ENOTTY);
50 /* Must be a character device. */
51 - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
52 - return (rval);
53 + if (!S_ISCHR(sb.st_mode))
54 + return (ENOTTY);
55 /* Must have enough room */
56 if (len <= sizeof(_PATH_DEV))
57 - return (rval);
58 + return (ERANGE);
59 +#else /* !__DARWIN_UNIX03 */
60 + /* Must be a terminal. */
61 + if (!isatty(fd))
62 + return (NULL);
63 + /* Must be a character device. */
64 + if (_fstat(fd, &sb))
65 + return (NULL);
66 + if (!S_ISCHR(sb.st_mode)) {
67 + errno = ENOTTY;
68 + return (NULL);
69 + }
70 + /* Must have enough room */
71 + if (len <= sizeof(_PATH_DEV)) {
72 + errno = ERANGE;
73 + return (NULL);
74 + }
75 +#endif /* __DARWIN_UNIX03 */
76
77 - strcpy(buf, _PATH_DEV);
78 - devname_r(sb.st_rdev, S_IFCHR,
79 - buf + strlen(buf), sizeof(buf) - strlen(buf));
80 - return (buf);
81 + strcpy(thrbuf, _PATH_DEV);
82 + if (devname_r(sb.st_rdev, S_IFCHR,
83 + thrbuf + strlen(thrbuf), len - strlen(thrbuf)) == NULL)
84 +#if __DARWIN_UNIX03
85 + return (ERANGE);
86 + return (0);
87 +#else /* !__DARWIN_UNIX03 */
88 + {
89 + errno = ERANGE;
90 + return (NULL);
91 + }
92 + return (thrbuf);
93 +#endif /* __DARWIN_UNIX03 */
94 }
95
96 +#ifndef BUILDING_VARIANT
97 static char *
98 ttyname_threaded(int fd)
99 {
100 @@ -104,8 +139,12 @@
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)) {
108 + int save = errno;
109 _pthread_mutex_unlock(&ttyname_lock);
110 + errno = save;
111 return (NULL);
112 }
113 ttyname_init = 1;
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) {
118 + int save = errno;
119 free(buf);
120 + errno = save;
121 return (NULL);
122 }
123 } else {
124 return (NULL);
125 }
126 }
127 +#if __DARWIN_UNIX03
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 */
132 }
133
134 static char *
135 @@ -137,11 +182,19 @@
136 if (tcgetattr(fd, &ttyb) < 0)
137 return (NULL);
138 /* Must be a character device. */
139 - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
140 + if (_fstat(fd, &sb))
141 return (NULL);
142 + if (!S_ISCHR(sb.st_mode)) {
143 + errno = ENOTTY;
144 + return (NULL);
145 + }
146
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) {
152 + errno = ERANGE;
153 + return (NULL);
154 + }
155 return (buf);
156 }
157 +#endif /* !BUILDING_VARIANT */