1181905Sed/*-
2181905Sed * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3181905Sed * All rights reserved.
4181905Sed *
5181905Sed * Portions of this software were developed under sponsorship from Snow
6181905Sed * B.V., the Netherlands.
7181905Sed *
8181905Sed * Redistribution and use in source and binary forms, with or without
9181905Sed * modification, are permitted provided that the following conditions
10181905Sed * are met:
11181905Sed * 1. Redistributions of source code must retain the above copyright
12181905Sed *    notice, this list of conditions and the following disclaimer.
13181905Sed * 2. Redistributions in binary form must reproduce the above copyright
14181905Sed *    notice, this list of conditions and the following disclaimer in the
15181905Sed *    documentation and/or other materials provided with the distribution.
16181905Sed *
17181905Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18181905Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19181905Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20181905Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21181905Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22181905Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23181905Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24181905Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25181905Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26181905Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27181905Sed * SUCH DAMAGE.
28181905Sed */
29181905Sed
30181905Sed#include <sys/cdefs.h>
31181905Sed#ifndef lint
32181905Sed__FBSDID("$FreeBSD$");
33181905Sed#endif /* not lint */
34181905Sed
35181905Sed#include "namespace.h"
36181905Sed#include <sys/param.h>
37181905Sed#include <sys/ioctl.h>
38181905Sed
39181905Sed#include <errno.h>
40181905Sed#include <paths.h>
41188497Sed#include <stdlib.h>
42181905Sed#include "un-namespace.h"
43181905Sed
44181905Sed/*
45181905Sed * __isptmaster():  return whether the file descriptor refers to a
46181905Sed *                  pseudo-terminal master device.
47181905Sed */
48181905Sedstatic int
49181905Sed__isptmaster(int fildes)
50181905Sed{
51181905Sed
52181905Sed	if (_ioctl(fildes, TIOCPTMASTER) == 0)
53181905Sed		return (0);
54181905Sed
55181905Sed	if (errno != EBADF)
56181905Sed		errno = EINVAL;
57181905Sed
58181905Sed	return (-1);
59181905Sed}
60181905Sed
61181905Sed/*
62181905Sed * In our implementation, grantpt() and unlockpt() don't actually have
63181905Sed * any use, because PTY's are created on the fly and already have proper
64181905Sed * permissions upon creation.
65181905Sed *
66181905Sed * Just make sure `fildes' actually points to a real PTY master device.
67181905Sed */
68181905Sed__strong_reference(__isptmaster, grantpt);
69181905Sed__strong_reference(__isptmaster, unlockpt);
70181905Sed
71181905Sed/*
72181905Sed * ptsname():  return the pathname of the slave pseudo-terminal device
73181905Sed *             associated with the specified master.
74181905Sed */
75181905Sedchar *
76181905Sedptsname(int fildes)
77181905Sed{
78181905Sed	static char pt_slave[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
79181905Sed	char *ret = NULL;
80181905Sed
81181905Sed	/* Make sure fildes points to a master device. */
82181905Sed	if (__isptmaster(fildes) != 0)
83181905Sed		goto done;
84223576Sed
85188497Sed	if (fdevname_r(fildes, pt_slave + (sizeof _PATH_DEV - 1),
86188497Sed	    sizeof pt_slave - (sizeof _PATH_DEV - 1)) != NULL)
87181905Sed		ret = pt_slave;
88181905Sed
89240412Semastedone:
90181905Sed	return (ret);
91181905Sed}
92