xref: /netbsd-src/lib/libc/stdlib/pty.c (revision c25b2b27092576bffcc4b3259941a94ab002afd3)
1*c25b2b27Schristos /*	$NetBSD: pty.c,v 1.4 2014/01/08 02:17:30 christos Exp $	*/
2892ad9caSchristos 
3892ad9caSchristos /*-
4892ad9caSchristos  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5892ad9caSchristos  * All rights reserved.
6892ad9caSchristos  *
7892ad9caSchristos  * This code is derived from software contributed to The NetBSD Foundation
8892ad9caSchristos  * by Christos Zoulas.
9892ad9caSchristos  *
10892ad9caSchristos  * Redistribution and use in source and binary forms, with or without
11892ad9caSchristos  * modification, are permitted provided that the following conditions
12892ad9caSchristos  * are met:
13892ad9caSchristos  * 1. Redistributions of source code must retain the above copyright
14892ad9caSchristos  *    notice, this list of conditions and the following disclaimer.
15892ad9caSchristos  * 2. Redistributions in binary form must reproduce the above copyright
16892ad9caSchristos  *    notice, this list of conditions and the following disclaimer in the
17892ad9caSchristos  *    documentation and/or other materials provided with the distribution.
18892ad9caSchristos  *
19892ad9caSchristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20892ad9caSchristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21892ad9caSchristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22892ad9caSchristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23892ad9caSchristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24892ad9caSchristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25892ad9caSchristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26892ad9caSchristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27892ad9caSchristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28892ad9caSchristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29892ad9caSchristos  * POSSIBILITY OF SUCH DAMAGE.
30892ad9caSchristos  */
31892ad9caSchristos 
32892ad9caSchristos #include <sys/cdefs.h>
33892ad9caSchristos #if defined(LIBC_SCCS) && !defined(lint)
34*c25b2b27Schristos __RCSID("$NetBSD: pty.c,v 1.4 2014/01/08 02:17:30 christos Exp $");
35892ad9caSchristos #endif /* LIBC_SCCS and not lint */
36892ad9caSchristos 
37892ad9caSchristos #include "namespace.h"
38892ad9caSchristos #include <fcntl.h>
39eabc0dd1Schristos #include <string.h>
40eabc0dd1Schristos #include <errno.h>
41892ad9caSchristos #include <stdlib.h>
42892ad9caSchristos #include <sys/ioctl.h>
43892ad9caSchristos 
44892ad9caSchristos int
grantpt(int fildes)45892ad9caSchristos grantpt(int fildes)
46892ad9caSchristos {
47892ad9caSchristos 
48892ad9caSchristos 	return ioctl(fildes, TIOCGRANTPT, 0);
49892ad9caSchristos }
50892ad9caSchristos 
51892ad9caSchristos int
52892ad9caSchristos /*ARGSUSED*/
unlockpt(int fildes)53892ad9caSchristos unlockpt(int fildes)
54892ad9caSchristos {
55892ad9caSchristos 
56892ad9caSchristos 	return 0;
57892ad9caSchristos }
58892ad9caSchristos 
59892ad9caSchristos char *
ptsname(int fildes)60892ad9caSchristos ptsname(int fildes)
61892ad9caSchristos {
62892ad9caSchristos 	static struct ptmget pm;
63892ad9caSchristos 
64892ad9caSchristos 	if (ioctl(fildes, TIOCPTSNAME, &pm) == -1)
65892ad9caSchristos 		return NULL;
66892ad9caSchristos 
67892ad9caSchristos 	return pm.sn;
68892ad9caSchristos }
69eabc0dd1Schristos 
70eabc0dd1Schristos int
ptsname_r(int fildes,char * buf,size_t buflen)71eabc0dd1Schristos ptsname_r(int fildes, char *buf, size_t buflen) {
72eabc0dd1Schristos 	struct ptmget pm;
73eabc0dd1Schristos 
74*c25b2b27Schristos 	if (buf == NULL)
75*c25b2b27Schristos 		return EINVAL;
76eabc0dd1Schristos 	if (ioctl(fildes, TIOCPTSNAME, &pm) == -1)
77eabc0dd1Schristos 		return errno;
78eabc0dd1Schristos 	if (strlcpy(buf, pm.sn, buflen) > buflen)
79*c25b2b27Schristos 		return ERANGE;
80eabc0dd1Schristos 	return 0;
81eabc0dd1Schristos }
82