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/>. */
24 /* Provide a wrapper with the prototype of glibc-2.8 and newer. */
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 */
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. */
50 #else /* AIX 5.1, HP-UX 11, IRIX 6.5, Solaris 10 */
55 # include <sys/ioctl.h>
58 # if defined __sun || defined __hpux /* Solaris, HP-UX */
63 openpty (int *amaster
, int *aslave
, char *name
,
64 struct termios
const *termp
, struct winsize
const *winp
)
70 # if HAVE__GETPTY /* IRIX */
72 slave_name
= _getpty (&master
, O_RDWR
, 0622, 0);
73 if (slave_name
== NULL
)
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
);
85 /* This call does not require a dependency to the 'grantpt' module,
86 because AIX, HP-UX, IRIX, Solaris all have the grantpt() function. */
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
))
95 # if !HAVE__GETPTY /* !IRIX */
96 slave_name
= ptsname (master
);
97 if (slave_name
== NULL
)
101 slave
= open (slave_name
, O_RDWR
| O_NOCTTY
);
105 # if defined __sun || defined __hpux /* Solaris, HP-UX */
106 if (ioctl (slave
, I_PUSH
, "ptem") < 0
107 || ioctl (slave
, I_PUSH
, "ldterm") < 0
109 || ioctl (slave
, I_PUSH
, "ttcompat") < 0
118 /* XXX Should we ignore errors here? */
120 tcsetattr (slave
, TCSAFLUSH
, termp
);
122 ioctl (slave
, TIOCSWINSZ
, winp
);
127 strcpy (name
, slave_name
);