malloc-h: New module.
[gnulib.git] / lib / ptsname_r.c
blob0008b95304d1d4d1ba0d795ac6af35d5bc8ab3ee
1 /* Determine name of the slave side of a pseudo-terminal.
2 Copyright (C) 1998, 2002, 2010-2020 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/>. */
17 #include <config.h>
19 #include <stdlib.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
27 #ifdef _LIBC
28 # include <paths.h>
29 #else
30 # ifndef _PATH_TTY
31 # define _PATH_TTY "/dev/tty"
32 # endif
33 # ifndef _PATH_DEV
34 # define _PATH_DEV "/dev/"
35 # endif
37 # undef __set_errno
38 # undef __stat
39 # undef __ttyname_r
40 # undef __ptsname_r
42 # define __set_errno(e) errno = (e)
43 # define __isatty isatty
44 # define __stat stat
45 # define __ttyname_r ttyname_r
46 # define __ptsname_r ptsname_r
48 #endif
50 /* Get the major, minor macros. */
51 #if MAJOR_IN_MKDEV
52 # include <sys/mkdev.h>
53 #endif
54 #if MAJOR_IN_SYSMACROS
55 # include <sys/sysmacros.h>
56 #endif
58 #ifdef __sun
59 /* Get ioctl() and 'struct strioctl'. */
60 # include <stropts.h>
61 /* Get ISPTM. */
62 # include <sys/stream.h>
63 # include <sys/ptms.h>
64 # include <stdio.h>
65 #endif
67 #if defined _AIX || defined __osf__
68 /* Get ioctl(), ISPTM. */
69 # include <sys/ioctl.h>
70 # include <stdio.h>
71 #endif
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. */
77 int
78 __ptsname_r (int fd, char *buf, size_t buflen)
79 #undef ptsname_r
81 #if HAVE_ESSENTIALLY_WORKING_PTSNAME_R
82 int ret = ptsname_r (fd, buf, buflen);
83 if (ret == 0)
84 return 0;
85 else
86 return errno;
87 #else
88 int save_errno = errno;
89 struct stat st;
91 if (buf == NULL)
93 __set_errno (EINVAL);
94 return EINVAL;
97 # if defined __sun /* Solaris */
98 if (fstat (fd, &st) < 0)
99 return errno;
100 if (!(S_ISCHR (st.st_mode) && major (st.st_rdev) == 0))
102 errno = ENOTTY;
103 return errno;
106 /* Master ptys can be recognized through a STREAMS ioctl. See
107 "STREAMS-based Pseudo-Terminal Subsystem"
108 <https://docs.oracle.com/cd/E18752_01/html/816-4855/termsub15-44781.html>
109 and "STREAMS ioctl commands"
110 <https://docs.oracle.com/cd/E18752_01/html/816-5177/streamio-7i.html>
112 struct strioctl ioctl_arg;
113 ioctl_arg.ic_cmd = ISPTM;
114 ioctl_arg.ic_timout = 0;
115 ioctl_arg.ic_len = 0;
116 ioctl_arg.ic_dp = NULL;
118 if (ioctl (fd, I_STR, &ioctl_arg) < 0)
120 errno = ENOTTY;
121 return errno;
125 char tmpbuf[9 + 10 + 1];
126 int n = sprintf (tmpbuf, "/dev/pts/%u", minor (st.st_rdev));
127 if (n >= buflen)
129 errno = ERANGE;
130 return errno;
132 memcpy (buf, tmpbuf, n + 1);
134 # elif defined _AIX || defined __osf__ /* AIX, OSF/1 */
135 /* This implementation returns /dev/pts/N, like ptsname() does.
136 Whereas the generic implementation below returns /dev/ttypN.
137 Both are correct, but let's be consistent with ptsname(). */
138 if (fstat (fd, &st) < 0)
139 return errno;
140 if (!S_ISCHR (st.st_mode))
142 errno = ENOTTY;
143 return errno;
146 int ret;
147 int dev;
148 char tmpbuf[9 + 10 + 1];
149 int n;
150 # ifdef _AIX
151 ret = ioctl (fd, ISPTM, &dev);
152 # endif
153 # ifdef __osf__
154 ret = ioctl (fd, ISPTM, NULL);
155 dev = ret;
156 # endif
157 if (ret < 0)
159 errno = ENOTTY;
160 return errno;
162 n = sprintf (tmpbuf, "/dev/pts/%u", minor (dev));
163 if (n >= buflen)
165 errno = ERANGE;
166 return errno;
168 memcpy (buf, tmpbuf, n + 1);
170 # else
171 if (!__isatty (fd))
173 # if ISATTY_FAILS_WITHOUT_SETTING_ERRNO && defined F_GETFL /* IRIX, Solaris */
174 /* Set errno. */
175 if (fcntl (fd, F_GETFL) != -1)
176 errno = ENOTTY;
177 # else
178 /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */
179 # endif
180 return errno;
183 if (buflen < strlen (_PATH_TTY) + 3)
185 __set_errno (ERANGE);
186 return ERANGE;
189 int err = __ttyname_r (fd, buf, buflen);
190 if (err != 0)
192 __set_errno (err);
193 return errno;
196 if (strncmp(buf, "/dev/pts/", strlen("/dev/pts/")) != 0)
197 buf[sizeof (_PATH_DEV) - 1] = 't';
198 # endif
200 if (__stat (buf, &st) < 0 && errno != EOVERFLOW)
201 return errno;
203 __set_errno (save_errno);
204 return 0;
205 #endif