Loading...
gen/FreeBSD/ttyname.c.patch Libc-763.13 Libc-391.2.10
--- Libc/Libc-763.13/gen/FreeBSD/ttyname.c.patch
+++ Libc/Libc-391.2.10/gen/FreeBSD/ttyname.c.patch
@@ -1,22 +1,26 @@
---- ttyname.c.orig	2010-05-01 19:18:09.000000000 -0700
-+++ ttyname.c	2010-05-03 10:18:47.000000000 -0700
-@@ -48,11 +48,14 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/tty
+Index: ttyname.c
+===================================================================
+RCS file: /cvs/root/Libc/gen/FreeBSD/ttyname.c,v
+retrieving revision 1.3
+diff -u -d -b -w -p -u -r1.3 ttyname.c
+--- ttyname.c	2004/11/25 19:38:02	1.3
++++ ttyname.c	2004/12/12 03:51:44
+@@ -48,10 +48,14 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/tty
  #include <string.h>
  #include <paths.h>
  #include <pthread.h>
++#if __DARWIN_UNIX03
 +#include <errno.h>
++#endif /* __DARWIN_UNIX03 */
  #include "un-namespace.h"
  
  #include "libc_private.h"
  
--static char buf[sizeof(_PATH_DEV) + MAXNAMLEN];
 +#ifndef BUILDING_VARIANT
-+static pthread_once_t ttyname_buf_control = PTHREAD_ONCE_INIT;
-+static char *buf = NULL;
+ static char buf[sizeof(_PATH_DEV) + MAXNAMLEN];
  static char *ttyname_threaded(int fd);
  static char *ttyname_unthreaded(int fd);
- 
-@@ -71,31 +74,63 @@ ttyname(int fd)
+@@ -71,31 +75,54 @@ ttyname(int fd)
  		ret = ttyname_threaded(fd);
  	return (ret);
  }
@@ -32,9 +36,9 @@
  {
  	struct stat	sb;
 -	char		*rval;
+ 
+-	rval = NULL;
 -
--	rval = NULL;
- 
 +#if __DARWIN_UNIX03
 +	if (_fstat(fd, &sb) < 0)
 +		return (EBADF);
@@ -43,47 +47,37 @@
 -		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);
++	/* Must have enough room */
++	if (len <= sizeof(_PATH_DEV))
 +		return (ERANGE);
 +#else /* !__DARWIN_UNIX03 */
 +	/* Must be a terminal. */
 +	if (!isatty(fd))
 +		return (NULL);
 +	/* Must be a character device. */
-+	if (_fstat(fd, &sb))
+ 	if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
+-		return (rval);
 +		return (NULL);
-+	if (!S_ISCHR(sb.st_mode)) {
-+		errno = ENOTTY;
+ 	/* Must have enough room */
+ 	if (len <= sizeof(_PATH_DEV))
+-		return (rval);
 +		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);
-+	strlcpy(thrbuf, _PATH_DEV, len);
++	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 */
  }
@@ -92,30 +86,7 @@
  static char *
  ttyname_threaded(int fd)
  {
-@@ -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 {
+@@ -124,7 +151,11 @@ ttyname_threaded(int fd)
  			return (NULL);
  		}
  	}
@@ -124,42 +95,18 @@
 +#else /* !__DARWIN_UNIX03 */
  	return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN));
 +#endif /* __DARWIN_UNIX03 */
-+}
-+
-+static void
-+ttyname_buf_allocate(void)
-+{
-+	buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN);
  }
  
  static char *
-@@ -137,11 +188,25 @@ ttyname_unthreaded(int fd)
- 	if (tcgetattr(fd, &ttyb) < 0)
+@@ -141,7 +172,9 @@ ttyname_unthreaded(int fd)
  		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);
-+	}
-+
-+	if (pthread_once(&ttyname_buf_control, ttyname_buf_allocate)
-+		|| !buf) {
-+		errno = ENOMEM;
-+		return (NULL);
-+	}
  
--	strcpy(buf, _PATH_DEV);
+ 	strcpy(buf, _PATH_DEV);
 -	devname_r(sb.st_rdev, S_IFCHR,
 -	    buf + strlen(buf), sizeof(buf) - strlen(buf));
-+	strlcpy(buf, _PATH_DEV, sizeof(_PATH_DEV) + MAXNAMLEN);
 +	if (devname_r(sb.st_rdev, S_IFCHR,
-+		buf + strlen(buf), sizeof(_PATH_DEV) + MAXNAMLEN - strlen(buf)) == NULL) {
-+		errno = ERANGE;
++	    buf + strlen(buf), sizeof(buf) - strlen(buf)) == NULL)
 +		return (NULL);
-+	}
  	return (buf);
  }
 +#endif /* !BUILDING_VARIANT */