pty: Activate the signature wrapper of forkpty.
[gnulib.git] / tests / test-openpty.c
1 /* Test of pty.h and openpty function.
2    Copyright (C) 2009-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 /* Written by Simon Josefsson <simon@josefsson.org>, 2009
18    and Bruno Haible <bruno@clisp.org>, 2010.  */
19
20 #include <config.h>
21
22 #include <pty.h>
23
24 #include "signature.h"
25 SIGNATURE_CHECK (openpty, int, (int *, int *, char *, struct termios const *,
26                                 struct winsize const *));
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
32
33 int
34 main ()
35 {
36   {
37     int master;
38     int slave;
39
40     /* Open a pseudo-terminal, as a master-slave pair.  */
41     {
42       int res = openpty (&master, &slave, NULL, NULL, NULL);
43       if (res != 0)
44         {
45           fprintf (stderr, "openpty returned %d\n", res);
46           return 1;
47         }
48     }
49
50     /* Set the terminal characteristics.
51        On Linux or Mac OS X, they can be set on either the master or the slave;
52        the effect is the same.  But on Solaris, they have to be set on the
53        master; tcgetattr on the slave fails.  */
54     {
55       int tcfd = slave; /* You can try  tcfd = master;  here.  */
56       struct termios attributes;
57
58       if (tcgetattr (tcfd, &attributes) < 0)
59         {
60           fprintf (stderr, "tcgetattr failed\n");
61           return 1;
62         }
63       /* Enable canonical processing, including erase.  */
64       attributes.c_lflag |= ECHO | ICANON | ECHOE;
65       attributes.c_cc[VERASE] = '\177';
66       if (tcsetattr (tcfd, TCSANOW, &attributes) < 0)
67         {
68           fprintf (stderr, "tcsetattr failed\n");
69           return 1;
70         }
71     }
72
73     /* Write into the master side.  */
74     {
75       static const char input[] = "Hello worst\177\177ld!\n";
76
77       if (write (master, input, strlen (input)) < (int) strlen (input))
78         {
79           fprintf (stderr, "write failed\n");
80           return 1;
81         }
82     }
83
84     /* Read from the slave side.  */
85     {
86       char buf[100];
87       int res = read (slave, buf, sizeof (buf));
88       static const char expected[] = "Hello world!\n";
89
90       if (res < 0)
91         {
92           fprintf (stderr, "read failed\n");
93           return 1;
94         }
95       if (!(res == strlen (expected)
96             && memcmp (buf, expected, strlen (expected)) == 0))
97         {
98           fprintf (stderr, "read result unexpected\n");
99           return 1;
100         }
101     }
102
103     /* Close the master side before the slave side gets closed.
104        This is necessary on Mac OS X 10.4.11.  */
105     close (master);
106   }
107
108   return 0;
109 }