Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | --- ttyname.c.orig 2007-02-05 14:38:48.000000000 -0800 +++ ttyname.c 2007-02-05 14:40:13.000000000 -0800 @@ -48,10 +48,12 @@ #include <string.h> #include <paths.h> #include <pthread.h> +#include <errno.h> #include "un-namespace.h" #include "libc_private.h" +#ifndef BUILDING_VARIANT static char buf[sizeof(_PATH_DEV) + MAXNAMLEN]; static char *ttyname_threaded(int fd); static char *ttyname_unthreaded(int fd); @@ -59,6 +61,7 @@ static pthread_mutex_t ttyname_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_key_t ttyname_key; static int ttyname_init = 0; +extern int __pthread_tsd_first; char * ttyname(int fd) @@ -71,31 +74,63 @@ ret = ttyname_threaded(fd); return (ret); } +#endif /* !BUILDING_VARIANT */ +#if __DARWIN_UNIX03 +int +#else /* !__DARWIN_UNIX03 */ char * -ttyname_r(int fd, char *buf, size_t len) +#endif /* __DARWIN_UNIX03 */ +ttyname_r(int fd, char *thrbuf, size_t len) { struct stat sb; - char *rval; - - rval = NULL; +#if __DARWIN_UNIX03 + if (_fstat(fd, &sb) < 0) + return (EBADF); /* Must be a terminal. */ if (!isatty(fd)) - return (rval); + return (ENOTTY); /* Must be a character device. */ - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode)) - return (rval); + if (!S_ISCHR(sb.st_mode)) + return (ENOTTY); /* Must have enough room */ if (len <= sizeof(_PATH_DEV)) - return (rval); + return (ERANGE); +#else /* !__DARWIN_UNIX03 */ + /* Must be a terminal. */ + if (!isatty(fd)) + return (NULL); + /* Must be a character device. */ + if (_fstat(fd, &sb)) + return (NULL); + if (!S_ISCHR(sb.st_mode)) { + errno = ENOTTY; + return (NULL); + } + /* Must have enough room */ + if (len <= sizeof(_PATH_DEV)) { + errno = ERANGE; + return (NULL); + } +#endif /* __DARWIN_UNIX03 */ - strcpy(buf, _PATH_DEV); - devname_r(sb.st_rdev, S_IFCHR, - buf + strlen(buf), sizeof(buf) - strlen(buf)); - return (buf); + strcpy(thrbuf, _PATH_DEV); + if (devname_r(sb.st_rdev, S_IFCHR, + thrbuf + strlen(thrbuf), len - strlen(thrbuf)) == NULL) +#if __DARWIN_UNIX03 + return (ERANGE); + return (0); +#else /* !__DARWIN_UNIX03 */ + { + errno = ERANGE; + return (NULL); + } + return (thrbuf); +#endif /* __DARWIN_UNIX03 */ } +#ifndef BUILDING_VARIANT static char * ttyname_threaded(int fd) { @@ -104,8 +139,12 @@ if (ttyname_init == 0) { _pthread_mutex_lock(&ttyname_lock); if (ttyname_init == 0) { - if (_pthread_key_create(&ttyname_key, free)) { + /* __PTK_LIBC_TTYNAME_KEY */ + ttyname_key = __pthread_tsd_first+1; + if (pthread_key_init_np(ttyname_key, free)) { + int save = errno; _pthread_mutex_unlock(&ttyname_lock); + errno = save; return (NULL); } ttyname_init = 1; @@ -117,14 +156,20 @@ if ((buf = _pthread_getspecific(ttyname_key)) == NULL) { if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) { if (_pthread_setspecific(ttyname_key, buf) != 0) { + int save = errno; free(buf); + errno = save; return (NULL); } } else { return (NULL); } } +#if __DARWIN_UNIX03 + return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN) == 0 ? buf : NULL); +#else /* !__DARWIN_UNIX03 */ return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN)); +#endif /* __DARWIN_UNIX03 */ } static char * @@ -137,11 +182,19 @@ if (tcgetattr(fd, &ttyb) < 0) return (NULL); /* Must be a character device. */ - if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode)) + if (_fstat(fd, &sb)) return (NULL); + if (!S_ISCHR(sb.st_mode)) { + errno = ENOTTY; + return (NULL); + } strcpy(buf, _PATH_DEV); - devname_r(sb.st_rdev, S_IFCHR, - buf + strlen(buf), sizeof(buf) - strlen(buf)); + if (devname_r(sb.st_rdev, S_IFCHR, + buf + strlen(buf), sizeof(buf) - strlen(buf)) == NULL) { + errno = ERANGE; + return (NULL); + } return (buf); } +#endif /* !BUILDING_VARIANT */ |