+@@ -104,8 +139,12 @@ ttyname_threaded(int fd)
+ 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 = __LIBC_PTHREAD_KEY_TTYNAME;
++ 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,26 @@ ttyname_threaded(int fd)
+ 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 {