1 /* Test of ptsname_r(3).
2 Copyright (C) 2010-2012 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"
36 /* Compare two slave names.
37 On some systems, there are hard links in the /dev/ directory.
38 For example, on OSF/1 5.1,
39 /dev/ttyp0 == /dev/pts/0
40 /dev/ttyp9 == /dev/pts/9
41 /dev/ttypa == /dev/pts/10
42 /dev/ttype == /dev/pts/14
45 same_slave (const char *slave_name1, const char *slave_name2)
50 return (strcmp (slave_name1, slave_name2) == 0
51 || (stat (slave_name1, &statbuf1) >= 0
52 && stat (slave_name2, &statbuf2) >= 0
53 && SAME_INODE (statbuf1, statbuf2)));
63 test_errors (int fd, const char *slave)
73 if (buflen_max > sizeof buffer)
74 buflen_max = sizeof buffer;
75 for (buflen = 0; buflen <= buflen_max; buflen++)
77 memset (buffer, 'X', sizeof buffer);
79 result = ptsname_r (fd, buffer, buflen);
83 ASSERT (buffer[0] == '/');
88 ASSERT (result == errno);
89 ASSERT (errno == ERANGE);
90 ASSERT (buffer[0] == 'X');
95 result = ptsname_r (fd, null_ptr (), 0);
97 ASSERT (result == errno);
98 ASSERT (errno == EINVAL);
105 /* Declare failure if test takes too long, by using default abort
106 caused by SIGALRM. */
107 signal (SIGALRM, SIG_DFL);
116 result = ptsname_r (-1, buffer, sizeof buffer);
117 ASSERT (result != 0);
118 ASSERT (result == errno);
119 ASSERT (errno == EBADF || errno == ENOTTY);
127 /* Open the controlling tty of the current process. */
128 fd = open ("/dev/tty", O_RDONLY);
131 fprintf (stderr, "Skipping test: cannot open controlling tty\n");
135 result = ptsname_r (fd, buffer, sizeof buffer);
136 /* The result is usually NULL, because /dev/tty is a slave, not a
140 ASSERT (memcmp (buffer, "/dev/", 5) == 0);
147 /* Solaris has BSD-style /dev/pty[p-r][0-9a-f] files, but the function
148 ptsname() does not work on them. */
154 /* Open the controlling tty of the current process. */
155 fd = open ("/dev/ptmx", O_RDWR | O_NOCTTY);
158 fprintf (stderr, "Skipping test: cannot open pseudo-terminal\n");
162 result = ptsname_r (fd, buffer, sizeof buffer);
163 ASSERT (result == 0);
164 ASSERT (memcmp (buffer, "/dev/pts/", 9) == 0);
166 test_errors (fd, buffer);
173 /* Try various master names of MacOS X: /dev/pty[p-w][0-9a-f] */
178 for (char1 = 'p'; char1 <= 'w'; char1++)
179 for (char2 = '0'; char2 <= 'f'; (char2 == '9' ? char2 = 'a' : char2++))
181 char master_name[32];
184 sprintf (master_name, "/dev/pty%c%c", char1, char2);
185 fd = open (master_name, O_RDONLY);
192 result = ptsname_r (fd, buffer, sizeof buffer);
193 ASSERT (result == 0);
194 sprintf (slave_name, "/dev/tty%c%c", char1, char2);
195 ASSERT (same_slave (buffer, slave_name));
197 test_errors (fd, buffer);
204 /* Try various master names of *BSD: /dev/pty[p-sP-S][0-9a-v] */
210 for (upper = 0; upper <= 1; upper++)
211 for (char1 = (upper ? 'P' : 'p'); char1 <= (upper ? 'S' : 's'); char1++)
212 for (char2 = '0'; char2 <= 'v'; (char2 == '9' ? char2 = 'a' : char2++))
214 char master_name[32];
217 sprintf (master_name, "/dev/pty%c%c", char1, char2);
218 fd = open (master_name, O_RDONLY);
225 result = ptsname_r (fd, buffer, sizeof buffer);
226 ASSERT (result == 0);
227 sprintf (slave_name, "/dev/tty%c%c", char1, char2);
228 ASSERT (same_slave (buffer, slave_name));
230 test_errors (fd, buffer);