finalise NEWS.stable
[gnulib.git] / tests / test-ptsname.c
1 /* Test of ptsname(3).
2    Copyright (C) 2010-2011 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 <fcntl.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30
31 #include "same-inode.h"
32
33 #include "macros.h"
34
35 /* Compare two slave names.
36    On some systems, there are hard links in the /dev/ directory.
37    For example, on OSF/1 5.1,
38      /dev/ttyp0 == /dev/pts/0
39      /dev/ttyp9 == /dev/pts/9
40      /dev/ttypa == /dev/pts/10
41      /dev/ttype == /dev/pts/14
42  */
43 static int
44 same_slave (const char *slave_name1, const char *slave_name2)
45 {
46   struct stat statbuf1;
47   struct stat statbuf2;
48
49   return (strcmp (slave_name1, slave_name2) == 0
50           || (stat (slave_name1, &statbuf1) >= 0
51               && stat (slave_name2, &statbuf2) >= 0
52               && SAME_INODE (statbuf1, statbuf2)));
53 }
54
55 int
56 main (void)
57 {
58 #if HAVE_DECL_ALARM
59   /* Declare failure if test takes too long, by using default abort
60      caused by SIGALRM.  */
61   signal (SIGALRM, SIG_DFL);
62   alarm (5);
63 #endif
64
65   {
66     int fd;
67     char *result;
68
69     /* Open the controlling tty of the current process.  */
70     fd = open ("/dev/tty", O_RDONLY);
71     if (fd < 0)
72       {
73         fprintf (stderr, "Skipping test: cannot open controlling tty\n");
74         return 77;
75       }
76
77     result = ptsname (fd);
78     /* The result is usually NULL, because /dev/tty is a slave, not a
79        master.  */
80     if (result != NULL)
81       {
82         ASSERT (memcmp (result, "/dev/", 5) == 0);
83       }
84
85     close (fd);
86   }
87
88 #if defined __sun
89   /* Solaris has BSD-style /dev/pty[p-r][0-9a-f] files, but the function
90      ptsname() does not work on them.  */
91   {
92     int fd;
93     char *result;
94
95     /* Open the controlling tty of the current process.  */
96     fd = open ("/dev/ptmx", O_RDWR | O_NOCTTY);
97     if (fd < 0)
98       {
99         fprintf (stderr, "Skipping test: cannot open pseudo-terminal\n");
100         return 77;
101       }
102
103     result = ptsname (fd);
104     ASSERT (result != NULL);
105     ASSERT (memcmp (result, "/dev/pts/", 9) == 0);
106
107     close (fd);
108   }
109
110 #else
111
112   /* Try various master names of MacOS X: /dev/pty[p-w][0-9a-f]  */
113   {
114     int char1;
115     int char2;
116
117     for (char1 = 'p'; char1 <= 'w'; char1++)
118       for (char2 = '0'; char2 <= 'f'; (char2 == '9' ? char2 = 'a' : char2++))
119         {
120           char master_name[32];
121           int fd;
122
123           sprintf (master_name, "/dev/pty%c%c", char1, char2);
124           fd = open (master_name, O_RDONLY);
125           if (fd >= 0)
126             {
127               char *result;
128               char slave_name[32];
129
130               result = ptsname (fd);
131               ASSERT (result != NULL);
132               sprintf (slave_name, "/dev/tty%c%c", char1, char2);
133               ASSERT (same_slave (result, slave_name));
134
135               close (fd);
136             }
137         }
138   }
139
140   /* Try various master names of *BSD: /dev/pty[p-sP-S][0-9a-v]  */
141   {
142     int upper;
143     int char1;
144     int char2;
145
146     for (upper = 0; upper <= 1; upper++)
147       for (char1 = (upper ? 'P' : 'p'); char1 <= (upper ? 'S' : 's'); char1++)
148         for (char2 = '0'; char2 <= 'v'; (char2 == '9' ? char2 = 'a' : char2++))
149           {
150             char master_name[32];
151             int fd;
152
153             sprintf (master_name, "/dev/pty%c%c", char1, char2);
154             fd = open (master_name, O_RDONLY);
155             if (fd >= 0)
156               {
157                 char *result;
158                 char slave_name[32];
159
160                 result = ptsname (fd);
161                 ASSERT (result != NULL);
162                 sprintf (slave_name, "/dev/tty%c%c", char1, char2);
163                 ASSERT (same_slave (result, slave_name));
164
165                 close (fd);
166               }
167           }
168   }
169
170 #endif
171
172   return 0;
173 }