error: Avoid "function declaration isn't a prototype" warning.
[gnulib.git] / tests / test-ptsname.c
blobe24d34f6b3426c7da712154bbc0ff5767e66a6ed
1 /* Test of ptsname(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/>. */
17 #include <config.h>
19 #include <stdlib.h>
21 #include "signature.h"
22 SIGNATURE_CHECK (ptsname, char *, (int));
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
32 #include "same-inode.h"
34 #include "macros.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
44 static int
45 same_slave (const char *slave_name1, const char *slave_name2)
47 struct stat statbuf1;
48 struct stat statbuf2;
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)));
56 int
57 main (void)
59 #if HAVE_DECL_ALARM
60 /* Declare failure if test takes too long, by using default abort
61 caused by SIGALRM. */
62 int alarm_value = 5;
63 signal (SIGALRM, SIG_DFL);
64 alarm (alarm_value);
65 #endif
68 char *result;
70 errno = 0;
71 result = ptsname (-1);
72 ASSERT (result == NULL);
73 ASSERT (errno == EBADF
74 || errno == ENOTTY /* seen on glibc */
79 int fd;
80 char *result;
82 /* Open the controlling tty of the current process. */
83 fd = open ("/dev/tty", O_RDONLY);
84 if (fd < 0)
86 fprintf (stderr, "Skipping test: cannot open controlling tty\n");
87 return 77;
90 result = ptsname (fd);
91 /* The result is usually NULL, because /dev/tty is a slave, not a
92 master. */
93 if (result != NULL)
95 ASSERT (memcmp (result, "/dev/", 5) == 0);
98 close (fd);
101 #if defined __sun
102 /* Solaris has BSD-style /dev/pty[p-r][0-9a-f] files, but the function
103 ptsname() does not work on them. */
105 int fd;
106 char *result;
108 /* Open the controlling tty of the current process. */
109 fd = open ("/dev/ptmx", O_RDWR | O_NOCTTY);
110 if (fd < 0)
112 fprintf (stderr, "Skipping test: cannot open pseudo-terminal\n");
113 return 77;
116 result = ptsname (fd);
117 ASSERT (result != NULL);
118 ASSERT (memcmp (result, "/dev/pts/", 9) == 0);
120 close (fd);
123 #else
125 /* Try various master names of Mac OS X: /dev/pty[p-w][0-9a-f] */
127 int char1;
128 int char2;
130 for (char1 = 'p'; char1 <= 'w'; char1++)
131 for (char2 = '0'; char2 <= 'f'; (char2 == '9' ? char2 = 'a' : char2++))
133 char master_name[32];
134 int fd;
136 sprintf (master_name, "/dev/pty%c%c", char1, char2);
137 fd = open (master_name, O_RDONLY);
138 if (fd >= 0)
140 char *result;
141 char slave_name[32];
143 result = ptsname (fd);
144 ASSERT (result != NULL);
145 sprintf (slave_name, "/dev/tty%c%c", char1, char2);
146 ASSERT (same_slave (result, slave_name));
148 close (fd);
153 /* Try various master names of *BSD: /dev/pty[p-sP-S][0-9a-v] */
155 int upper;
156 int char1;
157 int char2;
159 for (upper = 0; upper <= 1; upper++)
160 for (char1 = (upper ? 'P' : 'p'); char1 <= (upper ? 'S' : 's'); char1++)
161 for (char2 = '0'; char2 <= 'v'; (char2 == '9' ? char2 = 'a' : char2++))
163 char master_name[32];
164 int fd;
166 sprintf (master_name, "/dev/pty%c%c", char1, char2);
167 fd = open (master_name, O_RDONLY);
168 if (fd >= 0)
170 char *result;
171 char slave_name[32];
173 result = ptsname (fd);
174 ASSERT (result != NULL);
175 sprintf (slave_name, "/dev/tty%c%c", char1, char2);
176 ASSERT (same_slave (result, slave_name));
178 close (fd);
183 #endif
185 return 0;