sys_socket: Add support for OpenVMS.
[gnulib.git] / lib / openpty.c
blob9d6cff32db1ae5ac5631f9858a3d2b40e1612741
1 /* Open a pseudo-terminal.
2 Copyright (C) 2010-2017 Free Software Foundation, Inc.
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.
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.
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/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <pty.h>
22 #if HAVE_OPENPTY
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)
30 /* Cast away const, for implementations with weaker prototypes. */
31 return openpty (amaster, aslave, name, (struct termios *) termp,
32 (struct winsize *) winp);
35 #elif (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__ /* mingw */
37 # include <errno.h>
39 int
40 openpty (int *amaster _GL_UNUSED, int *aslave _GL_UNUSED,
41 char *name _GL_UNUSED,
42 struct termios const *termp _GL_UNUSED,
43 struct winsize const *winp _GL_UNUSED)
45 /* Mingw lacks pseudo-terminals altogether. */
46 errno = ENOSYS;
47 return -1;
50 #else /* AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 10 */
52 # include <fcntl.h>
53 # include <stdlib.h>
54 # include <string.h>
55 # include <sys/ioctl.h>
56 # include <termios.h>
57 # include <unistd.h>
58 # if defined __sun || defined __hpux /* Solaris, HP-UX */
59 # include <stropts.h>
60 # endif
62 int
63 openpty (int *amaster, int *aslave, char *name,
64 struct termios const *termp, struct winsize const *winp)
66 int master;
67 char *slave_name;
68 int slave;
70 # if HAVE__GETPTY /* IRIX */
72 slave_name = _getpty (&master, O_RDWR, 0622, 0);
73 if (slave_name == NULL)
74 return -1;
76 # else /* AIX 5.1, HP-UX 11, Solaris 10, mingw */
78 /* This call uses the 'posix_openpt' module. */
79 master = posix_openpt (O_RDWR | O_NOCTTY);
80 if (master < 0)
81 return -1;
83 # endif
85 /* This call does not require a dependency to the 'grantpt' module,
86 because AIX, HP-UX, IRIX, Solaris all have the grantpt() function. */
87 if (grantpt (master))
88 goto fail;
90 /* This call does not require a dependency to the 'unlockpt' module,
91 because AIX, HP-UX, IRIX, Solaris all have the unlockpt() function. */
92 if (unlockpt (master))
93 goto fail;
95 # if !HAVE__GETPTY /* !IRIX */
96 slave_name = ptsname (master);
97 if (slave_name == NULL)
98 goto fail;
99 # endif
101 slave = open (slave_name, O_RDWR | O_NOCTTY);
102 if (slave == -1)
103 goto fail;
105 # if defined __sun || defined __hpux /* Solaris, HP-UX */
106 if (ioctl (slave, I_PUSH, "ptem") < 0
107 || ioctl (slave, I_PUSH, "ldterm") < 0
108 # if defined __sun
109 || ioctl (slave, I_PUSH, "ttcompat") < 0
110 # endif
113 close (slave);
114 goto fail;
116 # endif
118 /* XXX Should we ignore errors here? */
119 if (termp)
120 tcsetattr (slave, TCSAFLUSH, termp);
121 if (winp)
122 ioctl (slave, TIOCSWINSZ, winp);
124 *amaster = master;
125 *aslave = slave;
126 if (name != NULL)
127 strcpy (name, slave_name);
129 return 0;
131 fail:
132 close (master);
133 return -1;
136 #endif