1 /* Copyright (C) 1998, 2000, 2001, 2002 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.
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.
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.
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/>. */
24 #include <sys/ioctl.h>
26 #include <sys/sysmacros.h>
29 #include <bits/uClibc_uintmaxtostr.h>
32 #if !defined __UNIX98PTY_ONLY__
34 /* Check if DEV corresponds to a master pseudo terminal device. */
35 #define MASTER_P(Dev) \
37 || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
38 || (major ((Dev)) >= 128 && major ((Dev)) < 136))
40 /* Check if DEV corresponds to a slave pseudo terminal device. */
41 #define SLAVE_P(Dev) \
43 || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
44 || (major ((Dev)) >= 136 && major ((Dev)) < 144))
46 /* Note that major number 4 corresponds to the old BSD style pseudo
47 terminal devices. As of Linux 2.1.115 these are no longer
48 supported. They have been replaced by major numbers 2 (masters)
51 /* The are declared in getpt.c. */
52 extern const char __libc_ptyname1[] attribute_hidden;
53 extern const char __libc_ptyname2[] attribute_hidden;
57 /* Directory where we can find the slave pty nodes. */
58 #define _PATH_DEVPTS "/dev/pts/"
60 /* Store at most BUFLEN characters of the pathname of the slave pseudo
61 terminal associated with the master FD is open on in BUF.
62 Return 0 on success, otherwise an error number. */
63 int ptsname_r (int fd, char *buf, size_t buflen)
65 int save_errno = errno;
66 #if !defined __UNIX98PTY_ONLY__
77 #if !defined __UNIX98PTY_ONLY__
83 #elif !defined TIOCGPTN
84 # error "__UNIX98PTY_ONLY__ enabled but TIOCGPTN ioctl not supported by your kernel."
87 if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
89 /* Buffer we use to print the number in. */
90 char numbuf[__BUFLEN_INT10TOSTR];
91 static const char devpts[] = _PATH_DEVPTS;
94 p = _int10tostr(&numbuf[sizeof numbuf - 1], ptyno);
96 if (buflen < sizeof(devpts) + (size_t)(&numbuf[sizeof(numbuf) - 1] - p))
102 strcpy (buf, devpts);
104 /* Note: Don't bother with stat on the slave name and checking the
105 driver's major device number - the ioctl above succeeded so
106 we know the fd was a Unix'98 master and the /dev/pts/ prefix
107 is set by definition. If the name isn't really a slave PTY,
108 the system is misconfigured anyway - something else will fail
115 #if defined __UNIX98PTY_ONLY__
118 /* If the ioctl fails it wasn't a Unix 98 master PTY */
123 # if defined TIOCGPTN
124 else if (errno == EINVAL)
129 if (buflen < strlen (_PATH_TTY) + 3)
135 if (fstat (fd, &st) < 0)
138 /* Check if FD really is a master pseudo terminal. */
139 if (! MASTER_P (st.st_rdev))
145 ptyno = minor (st.st_rdev);
146 /* This is for the old BSD pseudo terminals. As of Linux
147 2.1.115 these are no longer supported. */
148 if (major (st.st_rdev) == 4)
151 if (ptyno / 16 >= strlen (__libc_ptyname1))
157 strcpy (buf, _PATH_TTY);
158 p = buf + strlen (buf);
159 p[0] = __libc_ptyname1[ptyno / 16];
160 p[1] = __libc_ptyname2[ptyno % 16];
164 if (stat(buf, &st) < 0)
167 /* Check if the name we're about to return really corresponds to a
168 slave pseudo terminal. */
169 if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
171 /* This really is a configuration problem. */
180 libc_hidden_def(ptsname_r)
182 /* Return the pathname of the pseudo terminal slave assoicated with
183 the master FD is open on, or NULL on errors.
184 The returned storage is good until the next call to this function. */
188 static char buffer[sizeof (_PATH_DEVPTS) + 20];
190 return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;