29cf72cfb4601c75203b2c51c98e0bddf7aeefc0
[gnulib.git] / tests / test-ptsname.c
1 /* Test of ptsname(3).
2    Copyright (C) 2010-2013 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include "signature.h"
22 SIGNATURE_CHECK (ptsname, char *, (int));
23
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>
31
32 #include "same-inode.h"
33
34 #include "macros.h"
35
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
43  */
44 static int
45 same_slave (const char *slave_name1, const char *slave_name2)
46 {
47   struct stat statbuf1;
48   struct stat statbuf2;
49
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)));
54 }
55
56 int
57 main (void)
58 {
59 #if HAVE_DECL_ALARM
60   /* Declare failure if test takes too long, by using default abort
61      caused by SIGALRM.  */
62   signal (SIGALRM, SIG_DFL);
63   alarm (5);
64 #endif
65
66   {
67     char *result;
68
69     errno = 0;
70     result = ptsname (-1);
71     ASSERT (result == NULL);
72     ASSERT (errno == EBADF
73             || errno == ENOTTY /* seen on glibc */
74            );
75   }
76
77   {
78     int fd;
79     char *result;
80
81     /* Open the controlling tty of the current process.  */
82     fd = open ("/dev/tty", O_RDONLY);
83     if (fd < 0)
84       {
85         fprintf (stderr, "Skipping test: cannot open controlling tty\n");
86         return 77;
87       }
88
89     result = ptsname (fd);
90     /* The result is usually NULL, because /dev/tty is a slave, not a
91        master.  */
92     if (result != NULL)
93       {
94         ASSERT (memcmp (result, "/dev/", 5) == 0);
95       }
96
97     close (fd);
98   }
99
100 #if defined __sun
101   /* Solaris has BSD-style /dev/pty[p-r][0-9a-f] files, but the function
102      ptsname() does not work on them.  */
103   {
104     int fd;
105     char *result;
106
107     /* Open the controlling tty of the current process.  */
108     fd = open ("/dev/ptmx", O_RDWR | O_NOCTTY);
109     if (fd < 0)
110       {
111         fprintf (stderr, "Skipping test: cannot open pseudo-terminal\n");
112         return 77;
113       }
114
115     result = ptsname (fd);
116     ASSERT (result != NULL);
117     ASSERT (memcmp (result, "/dev/pts/", 9) == 0);
118
119     close (fd);
120   }
121
122 #else
123
124   /* Try various master names of Mac OS X: /dev/pty[p-w][0-9a-f]  */
125   {
126     int char1;
127     int char2;
128
129     for (char1 = 'p'; char1 <= 'w'; char1++)
130       for (char2 = '0'; char2 <= 'f'; (char2 == '9' ? char2 = 'a' : char2++))
131         {
132           char master_name[32];
133           int fd;
134
135           sprintf (master_name, "/dev/pty%c%c", char1, char2);
136           fd = open (master_name, O_RDONLY);
137           if (fd >= 0)
138             {
139               char *result;
140               char slave_name[32];
141
142               result = ptsname (fd);
143               ASSERT (result != NULL);
144               sprintf (slave_name, "/dev/tty%c%c", char1, char2);
145               ASSERT (same_slave (result, slave_name));
146
147               close (fd);
148             }
149         }
150   }
151
152   /* Try various master names of *BSD: /dev/pty[p-sP-S][0-9a-v]  */
153   {
154     int upper;
155     int char1;
156     int char2;
157
158     for (upper = 0; upper <= 1; upper++)
159       for (char1 = (upper ? 'P' : 'p'); char1 <= (upper ? 'S' : 's'); char1++)
160         for (char2 = '0'; char2 <= 'v'; (char2 == '9' ? char2 = 'a' : char2++))
161           {
162             char master_name[32];
163             int fd;
164
165             sprintf (master_name, "/dev/pty%c%c", char1, char2);
166             fd = open (master_name, O_RDONLY);
167             if (fd >= 0)
168               {
169                 char *result;
170                 char slave_name[32];
171
172                 result = ptsname (fd);
173                 ASSERT (result != NULL);
174                 sprintf (slave_name, "/dev/tty%c%c", char1, char2);
175                 ASSERT (same_slave (result, slave_name));
176
177                 close (fd);
178               }
179           }
180   }
181
182 #endif
183
184   return 0;
185 }