1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Determine name of the slave side of a pseudo-terminal.
4 Copyright (C) 1998, 2002, 2010-2014 Free Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
33 # define _PATH_TTY "/dev/tty"
36 # define _PATH_DEV "/dev/"
39 # define __set_errno(e) errno = (e)
40 # define __isatty isatty
42 # define __ttyname_r ttyname_r
43 # define __ptsname_r ptsname_r
48 /* Get ioctl() and 'struct strioctl'. */
51 # include <sys/stream.h>
52 # include <sys/ptms.h>
53 /* Get the major, minor macros. */
54 # include <sys/sysmacros.h>
58 #if defined _AIX || defined __osf__
59 /* Get ioctl(), ISPTM. */
60 # include <sys/ioctl.h>
61 /* Get the major, minor macros. */
62 # include <sys/sysmacros.h>
67 /* Store at most BUFLEN characters of the pathname of the slave pseudo
68 terminal associated with the master FD is open on in BUF.
69 Return 0 on success, otherwise an error number. */
71 __ptsname_r (int fd, char *buf, size_t buflen)
73 int save_errno = errno;
83 #if defined __sun /* Solaris */
84 if (fstat (fd, &st) < 0)
86 if (!(S_ISCHR (st.st_mode) && major (st.st_rdev) == 0))
92 /* Master ptys can be recognized through a STREAMS ioctl. See
93 "STREAMS-based Pseudo-Terminal Subsystem"
94 <http://docs.oracle.com/cd/E18752_01/html/816-4855/termsub15-44781.html>
95 and "STREAMS ioctl commands"
96 <http://docs.oracle.com/cd/E18752_01/html/816-5177/streamio-7i.html>
98 struct strioctl ioctl_arg;
99 ioctl_arg.ic_cmd = ISPTM;
100 ioctl_arg.ic_timout = 0;
101 ioctl_arg.ic_len = 0;
102 ioctl_arg.ic_dp = NULL;
104 if (ioctl (fd, I_STR, &ioctl_arg) < 0)
111 char tmpbuf[9 + 10 + 1];
112 int n = sprintf (tmpbuf, "/dev/pts/%u", minor (st.st_rdev));
118 memcpy (buf, tmpbuf, n + 1);
120 #elif defined _AIX || defined __osf__ /* AIX, OSF/1 */
121 /* This implementation returns /dev/pts/N, like ptsname() does.
122 Whereas the generic implementation below returns /dev/ttypN.
123 Both are correct, but let's be consistent with ptsname(). */
124 if (fstat (fd, &st) < 0)
126 if (!S_ISCHR (st.st_mode))
134 char tmpbuf[9 + 10 + 1];
137 ret = ioctl (fd, ISPTM, &dev);
140 ret = ioctl (fd, ISPTM, NULL);
148 n = sprintf (tmpbuf, "/dev/pts/%u", minor (dev));
154 memcpy (buf, tmpbuf, n + 1);
159 #if ISATTY_FAILS_WITHOUT_SETTING_ERRNO && defined F_GETFL /* IRIX, Solaris */
161 if (fcntl (fd, F_GETFL) != -1)
164 /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */
169 if (buflen < strlen (_PATH_TTY) + 3)
171 __set_errno (ERANGE);
175 err = __ttyname_r (fd, buf, buflen);
182 if (strncmp(buf, "/dev/pts/", strlen("/dev/pts/")) != 0)
183 buf[sizeof (_PATH_DEV) - 1] = 't';
186 if (__stat (buf, &st) < 0)
189 __set_errno (save_errno);