Debian Codegraph @

lib/openpty.c

raw
1 /* Open a pseudo-terminal.
2 Copyright (C) 2010-2026 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://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 #elif defined _WIN32 && !defined __CYGWIN__ /* mingw */
36
37 # include <errno.h>
38
39 int
40 openpty (_GL_UNUSED int *amaster, _GL_UNUSED int *aslave,
41 _GL_UNUSED char *name,
42 _GL_UNUSED struct termios const *termp,
43 _GL_UNUSED struct winsize const *winp)
44 {
45 /* Mingw lacks pseudo-terminals altogether. */
46 errno = ENOSYS;
47 return -1;
48 }
49
50 #else /* AIX 5.1, HP-UX 11, Solaris 10 */
51
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
61
62 int
63 openpty (int *amaster, int *aslave, char *name,
64 struct termios const *termp, struct winsize const *winp)
65 {
66 /* This call uses the 'posix_openpt' module. */
67 int master = posix_openpt (O_RDWR | O_NOCTTY);
68 if (master < 0)
69 return -1;
70
71 /* This call does not require a dependency to the 'grantpt' module,
72 because AIX, HP-UX, Solaris all have the grantpt() function. */
73 if (grantpt (master))
74 goto fail;
75
76 /* This call does not require a dependency to the 'unlockpt' module,
77 because AIX, HP-UX, Solaris all have the unlockpt() function. */
78 if (unlockpt (master))
79 goto fail;
80
81 char *slave_name = ptsname (master);
82 if (slave_name == NULL)
83 goto fail;
84
85 int slave = open (slave_name, O_RDWR | O_NOCTTY);
86 if (slave == -1)
87 goto fail;
88
89 # if defined __sun || defined __hpux /* Solaris, HP-UX */
90 if (ioctl (slave, I_PUSH, "ptem") < 0
91 || ioctl (slave, I_PUSH, "ldterm") < 0
92 # if defined __sun
93 || ioctl (slave, I_PUSH, "ttcompat") < 0
94 # endif
95 )
96 {
97 close (slave);
98 goto fail;
99 }
100 # endif
101
102 /* XXX Should we ignore errors here? */
103 if (termp)
104 tcsetattr (slave, TCSAFLUSH, termp);
105 if (winp)
106 ioctl (slave, TIOCSWINSZ, winp);
107
108 *amaster = master;
109 *aslave = slave;
110 if (name != NULL)
111 strcpy (name, slave_name);
112
113 return 0;
114
115 fail:
116 close (master);
117 return -1;
118 }
119
120 #endif