From owner-freebsd-threads@FreeBSD.ORG Fri Mar 18 01:30:07 2005 Return-Path: Delivered-To: freebsd-threads@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id CF00E16A4CE for ; Fri, 18 Mar 2005 01:30:07 +0000 (GMT) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7C67743D1D for ; Fri, 18 Mar 2005 01:30:07 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.13.3/8.13.3) with ESMTP id j2I1U7v1031410 for ; Fri, 18 Mar 2005 01:30:07 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.13.3/8.13.1/Submit) id j2I1U7Uk031396; Fri, 18 Mar 2005 01:30:07 GMT (envelope-from gnats) Date: Fri, 18 Mar 2005 01:30:07 GMT Message-Id: <200503180130.j2I1U7Uk031396@freefall.freebsd.org> To: freebsd-threads@FreeBSD.org From: Craig Rodrigues Subject: Re: threads/76938: include/unistd.h: ttyname_r prototype missing X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Craig Rodrigues List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 18 Mar 2005 01:30:08 -0000 The following reply was made to PR threads/76938; it has been noted by GNATS. From: Craig Rodrigues To: freebsd-gnats-submit@freebsd.org Cc: tmclaugh@sdf.lonestar.org Subject: Re: threads/76938: include/unistd.h: ttyname_r prototype missing Date: Thu, 17 Mar 2005 20:19:51 -0500 Hi, Poul-Henning recently modified ttyname, but still kept our non-standard ttyname_r, so I am submitting a modified patch. --- lib/libc/gen/ttyname.3.orig Wed Mar 9 17:13:58 2005 +++ lib/libc/gen/ttyname.3 Thu Mar 17 20:00:02 2005 @@ -37,6 +37,7 @@ .Os .Sh NAME .Nm ttyname , +.Nm ttyname_r , .Nm isatty , .Nm ttyslot .Nd get name of associated terminal (tty) from file descriptor @@ -46,7 +47,7 @@ .In unistd.h .Ft char * .Fn ttyname "int fd" -.Ft char * +.Ft int .Fn ttyname_r "int fd" "char *buf" "size_t len" .Ft int .Fn isatty "int fd" @@ -82,7 +83,13 @@ gets the related device name of a file descriptor for which .Fn isatty -is true +is true. +.Pp +.Fn ttyname +returns the name stored in a static buffer which will be overwritten +on subsequent calls. +.Fn ttyname_r +takes a buffer and length as arguments to avoid this problem. .Pp The .Fn ttyslot @@ -100,12 +107,25 @@ a .Dv NULL pointer is returned. +The +.Fn ttyname_r +function returns 0 if successful. Otherwise an error number is returned. .Pp The .Fn ttyslot function returns the unit number of the device file if found; otherwise the value zero is returned. +.Sh ERRORS +.Fn ttyname_r +may return the following error codes: +.Bl -tag -width Er +.It Bq Er ENOTTY +.Fa fd +is not a valid file descriptor. +.It Bq Er ERANGE +.Fa bufsize +is smaller than the length of the string to be returned. .Sh FILES .Bl -tag -width /etc/ttys -compact .It Pa /dev/\(** @@ -123,11 +143,6 @@ function appeared in .At v7 . -.Sh BUGS -The -.Fn ttyname -function leaves its result in an internal static object and returns -a pointer to that object. -Subsequent calls to -.Fn ttyname -will modify the same object. +.Fn ttyname_r +appeared in +.Fx 6.0 . --- lib/libc/gen/ttyname.c.orig Thu Mar 17 10:39:08 2005 +++ lib/libc/gen/ttyname.c Thu Mar 17 11:15:42 2005 @@ -50,6 +50,7 @@ #include #include #include +#include #include "un-namespace.h" #include "libc_private.h" @@ -60,34 +61,32 @@ static pthread_key_t ttyname_key; static int ttyname_init = 0; -char * +int ttyname_r(int fd, char *buf, size_t len) { struct stat sb; - char *rval; struct fiodgname_arg fgn; - rval = NULL; *buf = '\0'; /* 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); + return (ENOTTY); /* Must have enough room */ if (len <= sizeof(_PATH_DEV)) - return (rval); + return (ERANGE); strcpy(buf, _PATH_DEV); fgn.len = len - strlen(buf); fgn.buf = buf + strlen(buf); if (!_ioctl(fd, FIODGNAME, &fgn)) - return(buf); + return (EINVAL); devname_r(sb.st_rdev, S_IFCHR, buf + strlen(buf), sizeof(buf) - strlen(buf)); - return (buf); + return (0); } char * @@ -95,8 +94,12 @@ { char *buf; - if (__isthreaded == 0) - return (ttyname_r(fd, ttyname_buf, sizeof ttyname_buf)); + if (__isthreaded == 0) { + if (ttyname_r(fd, ttyname_buf, sizeof ttyname_buf) != 0) + return (NULL); + else + return (ttyname_buf); + } if (ttyname_init == 0) { _pthread_mutex_lock(&ttyname_lock); @@ -121,6 +124,7 @@ return (NULL); } } - return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN)); + ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN); + return (buf); } --- include/unistd.h.orig Thu Feb 17 17:37:41 2005 +++ include/unistd.h Thu Feb 17 17:38:19 2005 @@ -365,6 +365,7 @@ pid_t tcgetpgrp(int); int tcsetpgrp(int, pid_t); char *ttyname(int); +int ttyname_r(int, char *, size_t); int unlink(const char *); ssize_t write(int, const void *, size_t); -- Craig Rodrigues rodrigc@crodrigues.org