1/* Copyright (C) 1998-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <paths.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/ioctl.h>
24#include <sys/stat.h>
25#include <sys/sysmacros.h>
26#include <termios.h>
27#include <unistd.h>
28
29#include <_itoa.h>
30
31/* Check if DEV corresponds to a master pseudo terminal device. */
32#define MASTER_P(Dev) \
33 (major ((Dev)) == 2 \
34 || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
35 || (major ((Dev)) >= 128 && major ((Dev)) < 136))
36
37/* Check if DEV corresponds to a slave pseudo terminal device. */
38#define SLAVE_P(Dev) \
39 (major ((Dev)) == 3 \
40 || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
41 || (major ((Dev)) >= 136 && major ((Dev)) < 144))
42
43/* Note that major number 4 corresponds to the old BSD style pseudo
44 terminal devices. As of Linux 2.1.115 these are no longer
45 supported. They have been replaced by major numbers 2 (masters)
46 and 3 (slaves). */
47
48/* Directory where we can find the slave pty nodes. */
49#define _PATH_DEVPTS "/dev/pts/"
50
51/* The are declared in getpt.c. */
52extern const char __libc_ptyname1[] attribute_hidden;
53extern const char __libc_ptyname2[] attribute_hidden;
54
55/* Static buffer for `ptsname'. */
56static char buffer[sizeof (_PATH_DEVPTS) + 20];
57
58
59/* Return the pathname of the pseudo terminal slave associated with
60 the master FD is open on, or NULL on errors.
61 The returned storage is good until the next call to this function. */
62char *
63ptsname (int fd)
64{
65 return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
66}
67
68
69int
70__ptsname_internal (int fd, char *buf, size_t buflen, struct stat64 *stp)
71{
72 int save_errno = errno;
73 unsigned int ptyno;
74
75 if (!__isatty (fd))
76 {
77 __set_errno (ENOTTY);
78 return ENOTTY;
79 }
80
81#ifdef TIOCGPTN
82 if (__ioctl (fd, TIOCGPTN, &ptyno) == 0)
83 {
84 /* Buffer we use to print the number in. For a maximum size for
85 `int' of 8 bytes we never need more than 20 digits. */
86 char numbuf[21];
87 const char *devpts = _PATH_DEVPTS;
88 const size_t devptslen = strlen (_PATH_DEVPTS);
89 char *p;
90
91 numbuf[sizeof (numbuf) - 1] = '\0';
92 p = _itoa_word (ptyno, &numbuf[sizeof (numbuf) - 1], 10, 0);
93
94 if (buflen < devptslen + (&numbuf[sizeof (numbuf)] - p))
95 {
96 __set_errno (ERANGE);
97 return ERANGE;
98 }
99
100 memcpy (__stpcpy (buf, devpts), p, &numbuf[sizeof (numbuf)] - p);
101 }
102 else if (errno != EINVAL)
103 return errno;
104 else
105#endif
106 {
107 char *p;
108
109 if (buflen < strlen (_PATH_TTY) + 3)
110 {
111 __set_errno (ERANGE);
112 return ERANGE;
113 }
114
115 if (__fxstat64 (_STAT_VER, fd, stp) < 0)
116 return errno;
117
118 /* Check if FD really is a master pseudo terminal. */
119 if (! MASTER_P (stp->st_rdev))
120 {
121 __set_errno (ENOTTY);
122 return ENOTTY;
123 }
124
125 ptyno = minor (stp->st_rdev);
126
127 if (ptyno / 16 >= strlen (__libc_ptyname1))
128 {
129 __set_errno (ENOTTY);
130 return ENOTTY;
131 }
132
133 p = __stpcpy (buf, _PATH_TTY);
134 p[0] = __libc_ptyname1[ptyno / 16];
135 p[1] = __libc_ptyname2[ptyno % 16];
136 p[2] = '\0';
137 }
138
139 if (__xstat64 (_STAT_VER, buf, stp) < 0)
140 return errno;
141
142 /* Check if the name we're about to return really corresponds to a
143 slave pseudo terminal. */
144 if (! S_ISCHR (stp->st_mode) || ! SLAVE_P (stp->st_rdev))
145 {
146 /* This really is a configuration problem. */
147 __set_errno (ENOTTY);
148 return ENOTTY;
149 }
150
151 __set_errno (save_errno);
152 return 0;
153}
154
155
156/* Store at most BUFLEN characters of the pathname of the slave pseudo
157 terminal associated with the master FD is open on in BUF.
158 Return 0 on success, otherwise an error number. */
159int
160__ptsname_r (int fd, char *buf, size_t buflen)
161{
162 struct stat64 st;
163 return __ptsname_internal (fd, buf, buflen, &st);
164}
165weak_alias (__ptsname_r, ptsname_r)
166