1 /* Test of ptsname_r(3).
2 Copyright (C) 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 <http://www.gnu.org/licenses/>. */
21 #include "signature.h"
22 SIGNATURE_CHECK (ptsname_r
, int, (int, char *, size_t));
32 #include "same-inode.h"
37 /* Compare two slave names.
38 On some systems, there are hard links in the /dev/ directory.
39 For example, on OSF/1 5.1,
40 /dev/ttyp0 == /dev/pts/0
41 /dev/ttyp9 == /dev/pts/9
42 /dev/ttypa == /dev/pts/10
43 /dev/ttype == /dev/pts/14
46 same_slave (const char *slave_name1
, const char *slave_name2
)
51 return (strcmp (slave_name1
, slave_name2
) == 0
52 || (stat (slave_name1
, &statbuf1
) >= 0
53 && stat (slave_name2
, &statbuf2
) >= 0
54 && SAME_INODE (statbuf1
, statbuf2
)));
58 test_errors (int fd
, const char *slave
)
68 if (buflen_max
> sizeof buffer
)
69 buflen_max
= sizeof buffer
;
70 for (buflen
= 0; buflen
<= buflen_max
; buflen
++)
72 memset (buffer
, 'X', sizeof buffer
);
74 result
= ptsname_r (fd
, buffer
, buflen
);
78 ASSERT (buffer
[0] == '/');
83 ASSERT (result
== errno
);
84 ASSERT (errno
== ERANGE
);
85 ASSERT (buffer
[0] == 'X');
90 result
= ptsname_r (fd
, null_ptr (), 0);
92 ASSERT (result
== errno
);
93 ASSERT (errno
== EINVAL
);
100 /* Declare failure if test takes too long, by using default abort
101 caused by SIGALRM. */
103 signal (SIGALRM
, SIG_DFL
);
112 result
= ptsname_r (-1, buffer
, sizeof buffer
);
113 ASSERT (result
!= 0);
114 ASSERT (result
== errno
);
115 ASSERT (errno
== EBADF
|| errno
== ENOTTY
);
123 /* Open the controlling tty of the current process. */
124 fd
= open ("/dev/tty", O_RDONLY
);
127 fprintf (stderr
, "Skipping test: cannot open controlling tty\n");
131 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
132 /* The result is usually NULL, because /dev/tty is a slave, not a
136 ASSERT (memcmp (buffer
, "/dev/", 5) == 0);
143 /* Solaris has BSD-style /dev/pty[p-r][0-9a-f] files, but the function
144 ptsname() does not work on them. */
150 /* Open a pty master. */
151 fd
= open ("/dev/ptmx", O_RDWR
| O_NOCTTY
);
154 fprintf (stderr
, "Skipping test: cannot open pseudo-terminal\n");
158 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
159 ASSERT (result
== 0);
160 ASSERT (memcmp (buffer
, "/dev/pts/", 9) == 0);
162 test_errors (fd
, buffer
);
168 /* AIX has BSD-style /dev/ptyp[0-9a-f] files, but the modern way to open
169 a pty is to go through /dev/ptc. */
175 /* Open a pty master. */
176 fd
= open ("/dev/ptc", O_RDWR
| O_NOCTTY
);
179 fprintf (stderr
, "Skipping test: cannot open pseudo-terminal\n");
183 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
184 ASSERT (result
== 0);
185 ASSERT (memcmp (buffer
, "/dev/pts/", 9) == 0);
187 test_errors (fd
, buffer
);
189 /* This call hangs on AIX. */
195 /* Try various master names of Mac OS X: /dev/pty[p-w][0-9a-f] */
200 for (char1
= 'p'; char1
<= 'w'; char1
++)
201 for (char2
= '0'; char2
<= 'f'; (char2
== '9' ? char2
= 'a' : char2
++))
203 char master_name
[32];
206 sprintf (master_name
, "/dev/pty%c%c", char1
, char2
);
207 fd
= open (master_name
, O_RDONLY
);
214 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
215 ASSERT (result
== 0);
216 sprintf (slave_name
, "/dev/tty%c%c", char1
, char2
);
217 ASSERT (same_slave (buffer
, slave_name
));
219 test_errors (fd
, buffer
);
221 /* This call hangs on AIX. */
227 /* Try various master names of *BSD: /dev/pty[p-sP-S][0-9a-v] */
233 for (upper
= 0; upper
<= 1; upper
++)
234 for (char1
= (upper
? 'P' : 'p'); char1
<= (upper
? 'S' : 's'); char1
++)
235 for (char2
= '0'; char2
<= 'v'; (char2
== '9' ? char2
= 'a' : char2
++))
237 char master_name
[32];
240 sprintf (master_name
, "/dev/pty%c%c", char1
, char2
);
241 fd
= open (master_name
, O_RDONLY
);
248 result
= ptsname_r (fd
, buffer
, sizeof buffer
);
249 ASSERT (result
== 0);
250 sprintf (slave_name
, "/dev/tty%c%c", char1
, char2
);
251 ASSERT (same_slave (buffer
, slave_name
));
253 test_errors (fd
, buffer
);