1 /* Determine name of the slave side of a pseudo-terminal.
2 Copyright (C) 1998, 2002, 2010-2017 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
31 # define _PATH_TTY "/dev/tty"
34 # define _PATH_DEV "/dev/"
42 # define __set_errno(e) errno = (e)
43 # define __isatty isatty
45 # define __ttyname_r ttyname_r
46 # define __ptsname_r ptsname_r
50 /* Get the major, minor macros. */
52 # include <sys/mkdev.h>
54 #if MAJOR_IN_SYSMACROS
55 # include <sys/sysmacros.h>
59 /* Get ioctl() and 'struct strioctl'. */
62 # include <sys/stream.h>
63 # include <sys/ptms.h>
67 #if defined _AIX || defined __osf__
68 /* Get ioctl(), ISPTM. */
69 # include <sys/ioctl.h>
74 /* Store at most BUFLEN characters of the pathname of the slave pseudo
75 terminal associated with the master FD is open on in BUF.
76 Return 0 on success, otherwise an error number. */
78 __ptsname_r (int fd
, char *buf
, size_t buflen
)
80 int save_errno
= errno
;
90 #if defined __sun /* Solaris */
91 if (fstat (fd
, &st
) < 0)
93 if (!(S_ISCHR (st
.st_mode
) && major (st
.st_rdev
) == 0))
99 /* Master ptys can be recognized through a STREAMS ioctl. See
100 "STREAMS-based Pseudo-Terminal Subsystem"
101 <http://docs.oracle.com/cd/E18752_01/html/816-4855/termsub15-44781.html>
102 and "STREAMS ioctl commands"
103 <http://docs.oracle.com/cd/E18752_01/html/816-5177/streamio-7i.html>
105 struct strioctl ioctl_arg
;
106 ioctl_arg
.ic_cmd
= ISPTM
;
107 ioctl_arg
.ic_timout
= 0;
108 ioctl_arg
.ic_len
= 0;
109 ioctl_arg
.ic_dp
= NULL
;
111 if (ioctl (fd
, I_STR
, &ioctl_arg
) < 0)
118 char tmpbuf
[9 + 10 + 1];
119 int n
= sprintf (tmpbuf
, "/dev/pts/%u", minor (st
.st_rdev
));
125 memcpy (buf
, tmpbuf
, n
+ 1);
127 #elif defined _AIX || defined __osf__ /* AIX, OSF/1 */
128 /* This implementation returns /dev/pts/N, like ptsname() does.
129 Whereas the generic implementation below returns /dev/ttypN.
130 Both are correct, but let's be consistent with ptsname(). */
131 if (fstat (fd
, &st
) < 0)
133 if (!S_ISCHR (st
.st_mode
))
141 char tmpbuf
[9 + 10 + 1];
144 ret
= ioctl (fd
, ISPTM
, &dev
);
147 ret
= ioctl (fd
, ISPTM
, NULL
);
155 n
= sprintf (tmpbuf
, "/dev/pts/%u", minor (dev
));
161 memcpy (buf
, tmpbuf
, n
+ 1);
166 #if ISATTY_FAILS_WITHOUT_SETTING_ERRNO && defined F_GETFL /* IRIX, Solaris */
168 if (fcntl (fd
, F_GETFL
) != -1)
171 /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */
176 if (buflen
< strlen (_PATH_TTY
) + 3)
178 __set_errno (ERANGE
);
182 err
= __ttyname_r (fd
, buf
, buflen
);
189 if (strncmp(buf
, "/dev/pts/", strlen("/dev/pts/")) != 0)
190 buf
[sizeof (_PATH_DEV
) - 1] = 't';
193 if (__stat (buf
, &st
) < 0)
196 __set_errno (save_errno
);