summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib
diff options
context:
space:
mode:
authorchristos <christos@NetBSD.org>2014-01-08 02:15:42 +0000
committerchristos <christos@NetBSD.org>2014-01-08 02:15:42 +0000
commitdc211c599f7fa59adee161fc5ab432e3fd69ff66 (patch)
tree053c3779752d521c4f6f66c94a7c7dd147f2564c /lib/libc/stdlib
parentc3e6408977b3b0f738470fce0d0eb43bbec0e2b7 (diff)
add ptsname_r
Diffstat (limited to 'lib/libc/stdlib')
-rw-r--r--lib/libc/stdlib/Makefile.inc3
-rw-r--r--lib/libc/stdlib/ptsname.370
-rw-r--r--lib/libc/stdlib/pty.c17
3 files changed, 83 insertions, 7 deletions
diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc
index d45966791dc..dbea35bf867 100644
--- a/lib/libc/stdlib/Makefile.inc
+++ b/lib/libc/stdlib/Makefile.inc
@@ -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
diff --git a/lib/libc/stdlib/ptsname.3 b/lib/libc/stdlib/ptsname.3
index 28933cb2d62..736081955ef 100644
--- a/lib/libc/stdlib/ptsname.3
+++ b/lib/libc/stdlib/ptsname.3
@@ -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-terminal device associated with
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
diff --git a/lib/libc/stdlib/pty.c b/lib/libc/stdlib/pty.c
index 05cef2d8580..e880fa94c87 100644
--- a/lib/libc/stdlib/pty.c
+++ b/lib/libc/stdlib/pty.c
@@ -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;
+}