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