Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src add ptsname_r



details:   https://anonhg.NetBSD.org/src/rev/865438c66c7c
branches:  trunk
changeset: 325829:865438c66c7c
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Jan 08 02:15:42 2014 +0000

description:
add ptsname_r

diffstat:

 include/stdlib.h             |   3 +-
 lib/libc/stdlib/Makefile.inc |   3 +-
 lib/libc/stdlib/ptsname.3    |  70 +++++++++++++++++++++++++++++++++++++++++--
 lib/libc/stdlib/pty.c        |  17 +++++++++-
 4 files changed, 85 insertions(+), 8 deletions(-)

diffs (200 lines):

diff -r 11e311cc10e9 -r 865438c66c7c include/stdlib.h
--- a/include/stdlib.h  Wed Jan 08 01:56:20 2014 +0000
+++ b/include/stdlib.h  Wed Jan 08 02:15:42 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: stdlib.h,v 1.106 2013/04/26 18:07:43 christos Exp $    */
+/*     $NetBSD: stdlib.h,v 1.107 2014/01/08 02:16:03 christos Exp $    */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -302,6 +302,7 @@
 int     heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
 int     mergesort(void *, size_t, size_t,
            int (*)(const void *, const void *));
+int     ptsname_r(int, char *, size_t);
 int     radixsort(const unsigned char **, int, const unsigned char *,
            unsigned);
 int     sradixsort(const unsigned char **, int, const unsigned char *,
diff -r 11e311cc10e9 -r 865438c66c7c lib/libc/stdlib/Makefile.inc
--- a/lib/libc/stdlib/Makefile.inc      Wed Jan 08 01:56:20 2014 +0000
+++ b/lib/libc/stdlib/Makefile.inc      Wed Jan 08 02:15:42 2014 +0000
@@ -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+=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
diff -r 11e311cc10e9 -r 865438c66c7c lib/libc/stdlib/ptsname.3
--- a/lib/libc/stdlib/ptsname.3 Wed Jan 08 01:56:20 2014 +0000
+++ b/lib/libc/stdlib/ptsname.3 Wed Jan 08 02:15:42 2014 +0000
@@ -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 @@
 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 @@
 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 @@
 .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 @@
        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
diff -r 11e311cc10e9 -r 865438c66c7c lib/libc/stdlib/pty.c
--- a/lib/libc/stdlib/pty.c     Wed Jan 08 01:56:20 2014 +0000
+++ b/lib/libc/stdlib/pty.c     Wed Jan 08 02:15:42 2014 +0000
@@ -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 @@
 
        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;
+}



Home | Main Index | Thread Index | Old Index