maint.mk: silence new syntax check
[gnulib.git] / lib / openpty.c
1 /* Open a pseudo-terminal.
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 /* Specification.  */
20 #include <pty.h>
21
22 #if HAVE_OPENPTY
23
24 /* Provide a wrapper with the prototype of glibc-2.8 and newer.  */
25 # undef openpty
26 int
27 rpl_openpty (int *amaster, int *aslave, char *name,
28              struct termios const *termp, struct winsize const *winp)
29 {
30   /* Cast away const, for implementations with weaker prototypes.  */
31   return openpty (amaster, aslave, name, (struct termios *) termp,
32                   (struct winsize *) winp);
33 }
34
35 #else /* AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 10, mingw */
36
37 # include <fcntl.h>
38 # include <stdlib.h>
39 # include <string.h>
40 # include <sys/ioctl.h>
41 # include <termios.h>
42 # include <unistd.h>
43 # if defined __sun || defined __hpux /* Solaris, HP-UX */
44 #  include <stropts.h>
45 # endif
46
47 int
48 openpty (int *amaster, int *aslave, char *name,
49          struct termios const *termp, struct winsize const *winp)
50 {
51   int master;
52   char *slave_name;
53   int slave;
54
55 # if HAVE__GETPTY /* IRIX */
56
57   slave_name = _getpty (&master, O_RDWR, 0622, 0);
58   if (slave_name == NULL)
59     return -1;
60
61 # else /* AIX 5.1, HP-UX 11, Solaris 10, mingw */
62
63   /* This call uses the 'posix_openpt' module.  */
64   master = posix_openpt (O_RDWR | O_NOCTTY);
65   if (master < 0)
66     return -1;
67
68 # endif
69
70   /* This call does not require a dependency to the 'grantpt' module,
71      because AIX, HP-UX, IRIX, Solaris all have the grantpt() function.  */
72   if (grantpt (master))
73     goto fail;
74
75   /* This call does not require a dependency to the 'unlockpt' module,
76      because AIX, HP-UX, IRIX, Solaris all have the unlockpt() function.  */
77   if (unlockpt (master))
78     goto fail;
79
80 # if !HAVE__GETPTY /* !IRIX */
81   slave_name = ptsname (master);
82   if (slave_name == NULL)
83     goto fail;
84 # endif
85
86   slave = open (slave_name, O_RDWR | O_NOCTTY);
87   if (slave == -1)
88     goto fail;
89
90 # if defined __sun || defined __hpux /* Solaris, HP-UX */
91   if (ioctl (slave, I_PUSH, "ptem") < 0
92       || ioctl (slave, I_PUSH, "ldterm") < 0
93 #  if defined __sun
94       || ioctl (slave, I_PUSH, "ttcompat") < 0
95 #  endif
96      )
97     {
98       close (slave);
99       goto fail;
100     }
101 # endif
102
103   /* XXX Should we ignore errors here?  */
104   if (termp)
105     tcsetattr (slave, TCSAFLUSH, termp);
106   if (winp)
107     ioctl (slave, TIOCSWINSZ, winp);
108
109   *amaster = master;
110   *aslave = slave;
111   if (name != NULL)
112     strcpy (name, slave_name);
113
114   return 0;
115
116  fail:
117   close (master);
118   return -1;
119 }
120
121 #endif