]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/ttyname.c.patch
Libc-594.9.4.tar.gz
[apple/libc.git] / gen / FreeBSD / ttyname.c.patch
1 --- ttyname.c.orig 2008-10-09 21:30:31.000000000 -0700
2 +++ ttyname.c 2008-10-09 22:00:10.000000000 -0700
3 @@ -48,10 +48,12 @@ __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 +#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 @@ -71,31 +73,63 @@ ttyname(int fd)
17 ret = ttyname_threaded(fd);
18 return (ret);
19 }
20 +#endif /* !BUILDING_VARIANT */
21
22 +#if __DARWIN_UNIX03
23 +int
24 +#else /* !__DARWIN_UNIX03 */
25 char *
26 -ttyname_r(int fd, char *buf, size_t len)
27 +#endif /* __DARWIN_UNIX03 */
28 +ttyname_r(int fd, char *thrbuf, size_t len)
29 {
30 struct stat sb;
31 - char *rval;
32 -
33 - rval = NULL;
34
35 +#if __DARWIN_UNIX03
36 + if (_fstat(fd, &sb) < 0)
37 + return (EBADF);
38 /* Must be a terminal. */
39 if (!isatty(fd))
40 - return (rval);
41 + return (ENOTTY);
42 /* Must be a character device. */
43 - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
44 - return (rval);
45 + if (!S_ISCHR(sb.st_mode))
46 + return (ENOTTY);
47 /* Must have enough room */
48 if (len <= sizeof(_PATH_DEV))
49 - return (rval);
50 + return (ERANGE);
51 +#else /* !__DARWIN_UNIX03 */
52 + /* Must be a terminal. */
53 + if (!isatty(fd))
54 + return (NULL);
55 + /* Must be a character device. */
56 + if (_fstat(fd, &sb))
57 + return (NULL);
58 + if (!S_ISCHR(sb.st_mode)) {
59 + errno = ENOTTY;
60 + return (NULL);
61 + }
62 + /* Must have enough room */
63 + if (len <= sizeof(_PATH_DEV)) {
64 + errno = ERANGE;
65 + return (NULL);
66 + }
67 +#endif /* __DARWIN_UNIX03 */
68
69 - strcpy(buf, _PATH_DEV);
70 - devname_r(sb.st_rdev, S_IFCHR,
71 - buf + strlen(buf), sizeof(buf) - strlen(buf));
72 - return (buf);
73 + strcpy(thrbuf, _PATH_DEV);
74 + if (devname_r(sb.st_rdev, S_IFCHR,
75 + thrbuf + strlen(thrbuf), len - strlen(thrbuf)) == NULL)
76 +#if __DARWIN_UNIX03
77 + return (ERANGE);
78 + return (0);
79 +#else /* !__DARWIN_UNIX03 */
80 + {
81 + errno = ERANGE;
82 + return (NULL);
83 + }
84 + return (thrbuf);
85 +#endif /* __DARWIN_UNIX03 */
86 }
87
88 +#ifndef BUILDING_VARIANT
89 static char *
90 ttyname_threaded(int fd)
91 {
92 @@ -104,8 +138,12 @@ ttyname_threaded(int fd)
93 if (ttyname_init == 0) {
94 _pthread_mutex_lock(&ttyname_lock);
95 if (ttyname_init == 0) {
96 - if (_pthread_key_create(&ttyname_key, free)) {
97 + /* __PTK_LIBC_TTYNAME_KEY */
98 + ttyname_key = __LIBC_PTHREAD_KEY_TTYNAME;
99 + if (pthread_key_init_np(ttyname_key, free)) {
100 + int save = errno;
101 _pthread_mutex_unlock(&ttyname_lock);
102 + errno = save;
103 return (NULL);
104 }
105 ttyname_init = 1;
106 @@ -117,14 +155,20 @@ ttyname_threaded(int fd)
107 if ((buf = _pthread_getspecific(ttyname_key)) == NULL) {
108 if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) {
109 if (_pthread_setspecific(ttyname_key, buf) != 0) {
110 + int save = errno;
111 free(buf);
112 + errno = save;
113 return (NULL);
114 }
115 } else {
116 return (NULL);
117 }
118 }
119 +#if __DARWIN_UNIX03
120 + return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN) == 0 ? buf : NULL);
121 +#else /* !__DARWIN_UNIX03 */
122 return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN));
123 +#endif /* __DARWIN_UNIX03 */
124 }
125
126 static char *
127 @@ -137,11 +181,19 @@ ttyname_unthreaded(int fd)
128 if (tcgetattr(fd, &ttyb) < 0)
129 return (NULL);
130 /* Must be a character device. */
131 - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
132 + if (_fstat(fd, &sb))
133 return (NULL);
134 + if (!S_ISCHR(sb.st_mode)) {
135 + errno = ENOTTY;
136 + return (NULL);
137 + }
138
139 strcpy(buf, _PATH_DEV);
140 - devname_r(sb.st_rdev, S_IFCHR,
141 - buf + strlen(buf), sizeof(buf) - strlen(buf));
142 + if (devname_r(sb.st_rdev, S_IFCHR,
143 + buf + strlen(buf), sizeof(buf) - strlen(buf)) == NULL) {
144 + errno = ERANGE;
145 + return (NULL);
146 + }
147 return (buf);
148 }
149 +#endif /* !BUILDING_VARIANT */