pthread-cond: Fix compilation error on native Windows.
[gnulib.git] / lib / ptsname_r.c
blobcc1ae495ec75ef0276b833dd21472c8247887401
1 /* Determine name of the slave side of a pseudo-terminal.
2 Copyright (C) 1998, 2002, 2010-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <stdlib.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
28 #ifndef _PATH_TTY
29 # define _PATH_TTY "/dev/tty"
30 #endif
31 #ifndef _PATH_DEV
32 # define _PATH_DEV "/dev/"
33 #endif
35 /* Get the major, minor macros. */
36 #if MAJOR_IN_MKDEV
37 # include <sys/mkdev.h>
38 #endif
39 #if MAJOR_IN_SYSMACROS
40 # include <sys/sysmacros.h>
41 #endif
43 #ifdef __sun
44 /* Get ioctl() and 'struct strioctl'. */
45 # include <stropts.h>
46 /* Get ISPTM. */
47 # include <sys/stream.h>
48 # include <sys/ptms.h>
49 # include <stdio.h>
50 #endif
52 #if defined _AIX || defined __osf__
53 /* Get ioctl(), ISPTM. */
54 # include <sys/ioctl.h>
55 # include <stdio.h>
56 #endif
58 #if defined __DragonFly__
59 /* Get fdevname_r(). */
60 # include <stdlib.h>
61 #endif
63 /* Store at most BUFLEN characters of the pathname of the slave pseudo
64 terminal associated with the master FD is open on in BUF.
65 Return 0 on success, otherwise an error number. */
66 int
67 ptsname_r (int fd, char *buf, size_t buflen)
68 #undef ptsname_r
70 #if HAVE_ESSENTIALLY_WORKING_PTSNAME_R
71 # if defined __NetBSD__
72 char tmpbuf[32];
73 if (buf == NULL)
74 return EINVAL;
75 if (buflen >= sizeof (tmpbuf))
76 /* ERANGE should not happen in this case. */
77 return ptsname_r (fd, buf, buflen);
78 else
80 int ret = ptsname_r (fd, tmpbuf, sizeof (tmpbuf));
81 if (ret != 0)
82 return ret;
83 else
85 size_t len = strlen (tmpbuf);
86 if (len >= buflen)
87 return ERANGE;
88 memcpy (buf, tmpbuf, len + 1);
89 return 0;
92 # else
93 int ret = ptsname_r (fd, buf, buflen);
94 if (ret == 0)
95 return 0;
96 else
97 return errno;
98 # endif
99 #elif defined __DragonFly__
100 int saved_errno = errno;
101 char tmpbuf[5 + 4 + 10 + 1];
102 int ret;
103 int n;
104 if (buf == NULL)
106 errno = EINVAL;
107 return errno;
109 /* The result of fdevname_r is typically of the form ptm/N. */
110 ret = fdevname_r (fd, tmpbuf + 5, sizeof (tmpbuf) - 5);
111 if (ret < 0 || strncmp (tmpbuf + 5, "ptm/", 4) != 0)
113 errno = ENOTTY;
114 return errno;
116 /* Turn it into /dev/pts/N. */
117 memcpy (tmpbuf, "/dev/pts/", 5 + 4);
118 n = strlen (tmpbuf);
119 if (n >= buflen)
121 errno = ERANGE;
122 return errno;
124 memcpy (buf, tmpbuf, n + 1);
125 /* Don't do a final stat(), since the file name /dev/pts/N does not actually
126 exist. */
127 errno = saved_errno;
128 return 0;
129 #else
130 int saved_errno = errno;
131 struct stat st;
133 if (buf == NULL)
135 errno = EINVAL;
136 return errno;
139 # if defined __sun /* Solaris */
140 if (fstat (fd, &st) < 0)
141 return errno;
142 if (!S_ISCHR (st.st_mode))
144 errno = ENOTTY;
145 return errno;
148 /* Master ptys can be recognized through a STREAMS ioctl. See
149 "STREAMS-based Pseudo-Terminal Subsystem"
150 <https://docs.oracle.com/cd/E18752_01/html/816-4855/termsub15-44781.html>
151 and "STREAMS ioctl commands"
152 <https://docs.oracle.com/cd/E18752_01/html/816-5177/streamio-7i.html>
154 struct strioctl ioctl_arg;
155 ioctl_arg.ic_cmd = ISPTM;
156 ioctl_arg.ic_timout = 0;
157 ioctl_arg.ic_len = 0;
158 ioctl_arg.ic_dp = NULL;
160 if (ioctl (fd, I_STR, &ioctl_arg) < 0)
162 errno = ENOTTY;
163 return errno;
167 char tmpbuf[9 + 10 + 1];
168 int n = sprintf (tmpbuf, "/dev/pts/%u", minor (st.st_rdev));
169 if (n >= buflen)
171 errno = ERANGE;
172 return errno;
174 memcpy (buf, tmpbuf, n + 1);
176 # elif defined _AIX || defined __osf__ /* AIX, OSF/1 */
177 /* This implementation returns /dev/pts/N, like ptsname() does.
178 Whereas the generic implementation below returns /dev/ttypN.
179 Both are correct, but let's be consistent with ptsname(). */
180 if (fstat (fd, &st) < 0)
181 return errno;
182 if (!S_ISCHR (st.st_mode))
184 errno = ENOTTY;
185 return errno;
188 int ret;
189 int dev;
190 char tmpbuf[9 + 10 + 1];
191 int n;
192 # ifdef _AIX
193 ret = ioctl (fd, ISPTM, &dev);
194 # endif
195 # ifdef __osf__
196 ret = ioctl (fd, ISPTM, NULL);
197 dev = ret;
198 # endif
199 if (ret < 0)
201 errno = ENOTTY;
202 return errno;
204 n = sprintf (tmpbuf, "/dev/pts/%u", minor (dev));
205 if (n >= buflen)
207 errno = ERANGE;
208 return errno;
210 memcpy (buf, tmpbuf, n + 1);
212 # else
213 if (!isatty (fd))
215 # if ISATTY_FAILS_WITHOUT_SETTING_ERRNO && defined F_GETFL /* IRIX, Solaris */
216 /* Set errno. */
217 if (fcntl (fd, F_GETFL) != -1)
218 errno = ENOTTY;
219 # else
220 /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */
221 # endif
222 return errno;
225 if (buflen < strlen (_PATH_TTY) + 3)
227 errno = ERANGE;
228 return errno;
231 int err = ttyname_r (fd, buf, buflen);
232 if (err != 0)
234 errno = err;
235 return errno;
238 if (strncmp(buf, "/dev/pts/", strlen("/dev/pts/")) != 0)
239 buf[sizeof (_PATH_DEV) - 1] = 't';
240 # endif
242 if (stat (buf, &st) < 0 && errno != EOVERFLOW)
243 return errno;
245 errno = saved_errno;
246 return 0;
247 #endif