]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/ttyname.c.patch
Libc-391.4.1.tar.gz
[apple/libc.git] / gen / FreeBSD / ttyname.c.patch
1 Index: ttyname.c
2 ===================================================================
3 RCS file: /cvs/root/Libc/gen/FreeBSD/ttyname.c,v
4 retrieving revision 1.3
5 diff -u -d -b -w -p -u -r1.3 ttyname.c
6 --- ttyname.c 2004/11/25 19:38:02 1.3
7 +++ ttyname.c 2004/12/12 03:51:44
8 @@ -48,10 +48,14 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/tty
9 #include <string.h>
10 #include <paths.h>
11 #include <pthread.h>
12 +#if __DARWIN_UNIX03
13 +#include <errno.h>
14 +#endif /* __DARWIN_UNIX03 */
15 #include "un-namespace.h"
16
17 #include "libc_private.h"
18
19 +#ifndef BUILDING_VARIANT
20 static char buf[sizeof(_PATH_DEV) + MAXNAMLEN];
21 static char *ttyname_threaded(int fd);
22 static char *ttyname_unthreaded(int fd);
23 @@ -71,31 +75,54 @@ ttyname(int fd)
24 ret = ttyname_threaded(fd);
25 return (ret);
26 }
27 +#endif /* !BUILDING_VARIANT */
28
29 +#if __DARWIN_UNIX03
30 +int
31 +#else /* !__DARWIN_UNIX03 */
32 char *
33 -ttyname_r(int fd, char *buf, size_t len)
34 +#endif /* __DARWIN_UNIX03 */
35 +ttyname_r(int fd, char *thrbuf, size_t len)
36 {
37 struct stat sb;
38 - char *rval;
39
40 - rval = NULL;
41 -
42 +#if __DARWIN_UNIX03
43 + if (_fstat(fd, &sb) < 0)
44 + return (EBADF);
45 /* Must be a terminal. */
46 if (!isatty(fd))
47 - return (rval);
48 + return (ENOTTY);
49 /* Must be a character device. */
50 + if (!S_ISCHR(sb.st_mode))
51 + return (ENOTTY);
52 + /* Must have enough room */
53 + if (len <= sizeof(_PATH_DEV))
54 + return (ERANGE);
55 +#else /* !__DARWIN_UNIX03 */
56 + /* Must be a terminal. */
57 + if (!isatty(fd))
58 + return (NULL);
59 + /* Must be a character device. */
60 if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
61 - return (rval);
62 + return (NULL);
63 /* Must have enough room */
64 if (len <= sizeof(_PATH_DEV))
65 - return (rval);
66 + return (NULL);
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 + return (NULL);
81 + return (thrbuf);
82 +#endif /* __DARWIN_UNIX03 */
83 }
84
85 +#ifndef BUILDING_VARIANT
86 static char *
87 ttyname_threaded(int fd)
88 {
89 @@ -124,7 +151,11 @@ ttyname_threaded(int fd)
90 return (NULL);
91 }
92 }
93 +#if __DARWIN_UNIX03
94 + return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN) == 0 ? buf : NULL);
95 +#else /* !__DARWIN_UNIX03 */
96 return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN));
97 +#endif /* __DARWIN_UNIX03 */
98 }
99
100 static char *
101 @@ -141,7 +172,9 @@ ttyname_unthreaded(int fd)
102 return (NULL);
103
104 strcpy(buf, _PATH_DEV);
105 - devname_r(sb.st_rdev, S_IFCHR,
106 - buf + strlen(buf), sizeof(buf) - strlen(buf));
107 + if (devname_r(sb.st_rdev, S_IFCHR,
108 + buf + strlen(buf), sizeof(buf) - strlen(buf)) == NULL)
109 + return (NULL);
110 return (buf);
111 }
112 +#endif /* !BUILDING_VARIANT */