Module Name:    src
Committed By:   christos
Date:           Wed Jan  8 02:15:42 UTC 2014

Modified Files:
        src/lib/libc/stdlib: Makefile.inc ptsname.3 pty.c

Log Message:
add ptsname_r


To generate a diff of this commit:
cvs rdiff -u -r1.80 -r1.81 src/lib/libc/stdlib/Makefile.inc
cvs rdiff -u -r1.6 -r1.7 src/lib/libc/stdlib/ptsname.3
cvs rdiff -u -r1.2 -r1.3 src/lib/libc/stdlib/pty.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/stdlib/Makefile.inc
diff -u src/lib/libc/stdlib/Makefile.inc:1.80 src/lib/libc/stdlib/Makefile.inc:1.81
--- src/lib/libc/stdlib/Makefile.inc:1.80	Sun Dec  1 23:39:10 2013
+++ src/lib/libc/stdlib/Makefile.inc	Tue Jan  7 21:15:42 2014
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.inc,v 1.80 2013/12/02 04:39:10 lneto Exp $
+#	$NetBSD: Makefile.inc,v 1.81 2014/01/08 02:15:42 christos Exp $
 #	from: @(#)Makefile.inc	8.3 (Berkeley) 2/4/95
 
 # stdlib sources
@@ -70,6 +70,7 @@ MLINKS+=insque.3 remque.3
 MLINKS+=lsearch.3 lfind.3
 MLINKS+=malloc.3 calloc.3 malloc.3 realloc.3 malloc.3 free.3
 MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3
+MLINKS+=ptsname.3 ptsname_r.3
 MLINKS+=rand.3 rand_r.3
 MLINKS+=rand.3 srand.3
 MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3

Index: src/lib/libc/stdlib/ptsname.3
diff -u src/lib/libc/stdlib/ptsname.3:1.6 src/lib/libc/stdlib/ptsname.3:1.7
--- src/lib/libc/stdlib/ptsname.3:1.6	Fri Oct 19 06:44:34 2012
+++ src/lib/libc/stdlib/ptsname.3	Tue Jan  7 21:15:42 2014
@@ -1,4 +1,4 @@
-.\" $NetBSD: ptsname.3,v 1.6 2012/10/19 10:44:34 apb Exp $
+.\" $NetBSD: ptsname.3,v 1.7 2014/01/08 02:15:42 christos Exp $
 .\"
 .\" Copyright (c) 2004 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -27,11 +27,12 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd May 25, 2004
+.Dd January 7, 2014
 .Dt PTSNAME 3
 .Os
 .Sh NAME
-.Nm ptsname
+.Nm ptsname ,
+.Nm ptsname_r
 .Nd get the pathname of the slave pseudo-terminal device
 .Sh LIBRARY
 .Lb libc
@@ -39,6 +40,8 @@
 .In stdlib.h
 .Ft char *
 .Fn ptsname "int masterfd"
+.Ft char *
+.Fn ptsname_r "int masterfd" "char *buf" "size_t buflen"
 .Sh DESCRIPTION
 The
 .Fn ptsname
@@ -48,6 +51,23 @@ that corresponds to the master pseudo-te
 The
 .Fn ptsname
 function is not reentrant or thread-safe.
+.Pp
+The
+.Fn ptsname_r
+function
+places the pathname of the slave pseudo-terminal device that corresponds
+to the master pseudo-terminal device associated with
+.Fa masterfd 
+int the
+.Fa buf
+argument copying up to
+.Fa buflen
+characters.
+The
+.Fa buf
+is always
+.Dv NUL
+terminated.
 .Sh RETURN VALUES
 If successful,
 .Fn ptsname
@@ -60,10 +80,25 @@ will return
 and
 .Va errno
 is set to indicate the error.
+.Pp
+If successful,
+.Fn ptsname_r
+places a nul-terminated string containing the pathname
+of the slave pseudo-terminal device
+in
+.Fa buf
+and returns
+.Dv 0 .
+If an error occurs
+.Fn ptsname_r
+will return
+an error number number indicating what went wrong.
 .Sh ERRORS
 The
 .Fn ptsname
-function will fail if:
+and
+.Fn ptsname_r
+functions will fail if:
 .Bl -tag -width Er
 .It Bq Er EACCESS
 the corresponding pseudo-terminal device could not be accessed.
@@ -74,6 +109,18 @@ is not a valid descriptor.
 .Fa masterfd
 is not associated with a master pseudo-terminal device.
 .El
+.Pp
+In addition the
+.Fn ptsname_r
+function
+will return:
+.Bl -tag -width Er
+.It Bq Er ENOSPC
+the name of the pseudo-terminal is longer than
+.Fa bufsiz
+characters plus the terminating
+.Dv NUL .
+.El
 .Sh NOTES
 The error returns of
 .Fn ptsname
@@ -87,8 +134,23 @@ function is equivalent to:
 	struct ptmget pm;
 	return ioctl(masterfd, TIOCPTSNAME, \*[Am]pm) == -1 ? NULL : pm.sn;
 .Ed
+.Pp
+Both the
+.Fn ptsname
+and
+.Fn ptsname_r
+functions will also return the name of the slave pseudo-terminal if a file
+descriptor to the slave pseudo-terminal is passed to
+.Fa masterfd .
+.Pp
+This is a convenient extension because it allows one to use the file descriptor
+obtained by
+.Xr open 2
+.Pa /dev/tty
+to obtain the name of the pseudo-terminal for the current process.
 .Sh SEE ALSO
 .Xr ioctl 2 ,
+.Xr open 2 ,
 .Xr grantpt 3 ,
 .Xr posix_openpt 3 ,
 .Xr unlockpt 3

Index: src/lib/libc/stdlib/pty.c
diff -u src/lib/libc/stdlib/pty.c:1.2 src/lib/libc/stdlib/pty.c:1.3
--- src/lib/libc/stdlib/pty.c:1.2	Mon Apr 28 16:23:00 2008
+++ src/lib/libc/stdlib/pty.c	Tue Jan  7 21:15:42 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: pty.c,v 1.2 2008/04/28 20:23:00 martin Exp $	*/
+/*	$NetBSD: pty.c,v 1.3 2014/01/08 02:15:42 christos Exp $	*/
 
 /*-
  * Copyright (c) 2004 The NetBSD Foundation, Inc.
@@ -31,11 +31,13 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: pty.c,v 1.2 2008/04/28 20:23:00 martin Exp $");
+__RCSID("$NetBSD: pty.c,v 1.3 2014/01/08 02:15:42 christos Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
 #include <fcntl.h>
+#include <string.h>
+#include <errno.h>
 #include <stdlib.h>
 #include <sys/ioctl.h>
 
@@ -64,3 +66,14 @@ ptsname(int fildes)
 
 	return pm.sn;
 }
+
+int
+ptsname_r(int fildes, char *buf, size_t buflen) {
+	struct ptmget pm;
+
+	if (ioctl(fildes, TIOCPTSNAME, &pm) == -1)
+		return errno;
+	if (strlcpy(buf, pm.sn, buflen) > buflen)
+		return ENOSPC;
+	return 0;
+}

Reply via email to