1 /* Test of ptsname_r(3).
2 Copyright (C) 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 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
18 may "optimize" the null_ptr function, when its result gets passed to a
19 function that has an argument declared as _GL_ARG_NONNULL. */
20 #define _GL_ARG_NONNULL(params)
26 #include "signature.h"
27 SIGNATURE_CHECK (ptsname_r
, int, (int, char *, size_t));
37 #include "same-inode.h"
39 #if GNULIB_defined_ptsname_r
40 # include "null-ptr.h"
45 /* Compare two slave names.
46 On some systems, there are hard links in the /dev/ directory.
47 For example, on OSF/1 5.1,
48 /dev/ttyp0 == /dev/pts/0
49 /dev/ttyp9 == /dev/pts/9
50 /dev/ttypa == /dev/pts/10
51 /dev/ttype == /dev/pts/14
54 same_slave (const char *slave_name1
, const char *slave_name2
)
59 return (strcmp (slave_name1
, slave_name2
) == 0
60 || (stat (slave_name1
, &statbuf1
) >= 0
61 && stat (slave_name2
, &statbuf2
) >= 0
62 && SAME_INODE (statbuf1
, statbuf2
)));
66 test_errors (int fd
, const char *slave
)
76 if (buflen_max
> sizeof buffer
)
77 buflen_max
= sizeof buffer
;
78 for (buflen
= 0; buflen
<= buflen_max
; buflen
++)
80 memset (buffer
, 'X', sizeof buffer
);
81 result
= ptsname_r (fd
, buffer
, buflen
);
85 ASSERT (buffer
[0] == '/');
90 ASSERT (result
== ERANGE
);
91 ASSERT (buffer
[0] == 'X');
95 /* This test works only if the ptsname_r implementation comes from gnulib.
96 If it comes from libc, we have no way to prevent gcc from "optimizing"
97 the null_ptr function in invalid ways. See
98 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93156>. */
99 #if GNULIB_defined_ptsname_r
100 result
= ptsname_r (fd
, null_ptr (), 0);
101 ASSERT (result
!= 0);
102 ASSERT (result
== EINVAL
);
110 /* Declare failure if test takes too long, by using default abort
111 caused by SIGALRM. */
113 signal (SIGALRM
, SIG_DFL
);
121 result
= ptsname_r (-1, buffer
, sizeof buffer
);
122 ASSERT (result
!= 0);
123 ASSERT (result
== EBADF
|| result
== ENOTTY
);
131 /* Open the controlling tty of the current process. */
132 fd
= open ("/dev/tty", O_RDONLY
);
135 fprintf (stderr
, "Skipping test: cannot open controlling tty\n");
139 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
140 /* The result is usually NULL, because /dev/tty is a slave, not a
144 ASSERT (memcmp (buffer
, "/dev/", 5) == 0);
151 /* Solaris has BSD-style /dev/pty[p-r][0-9a-f] files, but the function
152 ptsname() does not work on them. */
158 /* Open a pty master. */
159 fd
= open ("/dev/ptmx", O_RDWR
| O_NOCTTY
);
162 fprintf (stderr
, "Skipping test: cannot open pseudo-terminal\n");
166 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
167 ASSERT (result
== 0);
168 ASSERT (memcmp (buffer
, "/dev/pts/", 9) == 0);
170 test_errors (fd
, buffer
);
176 /* AIX has BSD-style /dev/ptyp[0-9a-f] files, but the modern way to open
177 a pty is to go through /dev/ptc. */
183 /* Open a pty master. */
184 fd
= open ("/dev/ptc", O_RDWR
| O_NOCTTY
);
187 fprintf (stderr
, "Skipping test: cannot open pseudo-terminal\n");
191 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
192 ASSERT (result
== 0);
193 ASSERT (memcmp (buffer
, "/dev/pts/", 9) == 0);
195 test_errors (fd
, buffer
);
197 /* This call hangs on AIX. */
203 /* Try various master names of Mac OS X: /dev/pty[p-w][0-9a-f] */
208 for (char1
= 'p'; char1
<= 'w'; char1
++)
209 for (char2
= '0'; char2
<= 'f'; (char2
== '9' ? char2
= 'a' : char2
++))
211 char master_name
[32];
214 sprintf (master_name
, "/dev/pty%c%c", char1
, char2
);
215 fd
= open (master_name
, O_RDONLY
);
222 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
223 ASSERT (result
== 0);
224 sprintf (slave_name
, "/dev/tty%c%c", char1
, char2
);
225 ASSERT (same_slave (buffer
, slave_name
));
227 test_errors (fd
, buffer
);
229 /* This call hangs on AIX. */
235 /* Try various master names of *BSD: /dev/pty[p-sP-S][0-9a-v] */
241 for (upper
= 0; upper
<= 1; upper
++)
242 for (char1
= (upper
? 'P' : 'p'); char1
<= (upper
? 'S' : 's'); char1
++)
243 for (char2
= '0'; char2
<= 'v'; (char2
== '9' ? char2
= 'a' : char2
++))
245 char master_name
[32];
248 sprintf (master_name
, "/dev/pty%c%c", char1
, char2
);
249 fd
= open (master_name
, O_RDONLY
);
256 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
257 ASSERT (result
== 0);
258 sprintf (slave_name
, "/dev/tty%c%c", char1
, char2
);
259 ASSERT (same_slave (buffer
, slave_name
));
261 test_errors (fd
, buffer
);