blob: 5f58476e90983ebcb3aa6965195e47eea09b489f [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Copyright (C) 1998, 1999, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <errno.h>
21#include <fcntl.h>
22#include <limits.h>
23#include <pty.h>
24#include <stdlib.h>
25#include <string.h>
26#include <termios.h>
27#include <unistd.h>
28#include <sys/types.h>
29
30/* BCS: the following function is, IMO, overkill */
31#if 0
32/* Return the result of ptsname_r in the buffer pointed to by PTS,
33 which should be of length BUF_LEN. If it is too long to fit in
34 this buffer, a sufficiently long buffer is allocated using malloc,
35 and returned in PTS. 0 is returned upon success, -1 otherwise. */
36static int
37pts_name (int fd, char **pts, size_t buf_len)
38{
39 int rv;
40 char *buf = *pts;
41
42 for (;;)
43 {
44 char *new_buf;
45
46 if (buf_len)
47 {
48 rv = ptsname_r (fd, buf, buf_len);
49
50 if (rv != 0 || memchr (buf, '\0', buf_len))
51 /* We either got an error, or we succeeded and the
52 returned name fit in the buffer. */
53 break;
54
55 /* Try again with a longer buffer. */
56 buf_len += buf_len; /* Double it */
57 }
58 else
59 /* No initial buffer; start out by mallocing one. */
60 buf_len = 128; /* First time guess. */
61
62 if (buf != *pts)
63 /* We've already malloced another buffer at least once. */
64 new_buf = realloc (buf, buf_len);
65 else
66 new_buf = malloc (buf_len);
67 if (! new_buf)
68 {
69 rv = -1;
70 __set_errno (ENOMEM);
71 break;
72 }
73 buf = new_buf;
74 }
75
76 if (rv == 0)
77 *pts = buf; /* Return buffer to the user. */
78 else if (buf != *pts)
79 free (buf); /* Free what we malloced when returning an error. */
80
81 return rv;
82}
83#endif
84
85/* Create pseudo tty master slave pair and set terminal attributes
86 according to TERMP and WINP. Return handles for both ends in
87 AMASTER and ASLAVE, and return the name of the slave end in NAME. */
88libutil_hidden_proto(openpty)
89int
90openpty (int *amaster, int *aslave, char *name, struct termios *termp,
91 struct winsize *winp)
92{
93#if 0
94#ifdef PATH_MAX
95 char _buf[PATH_MAX];
96#else
97 char _buf[512];
98#endif
99 char *buf = _buf;
100#else
101#ifdef PATH_MAX
102 char buf[PATH_MAX];
103#else
104 char buf[512];
105#endif
106#endif
107 int master, slave;
108
109 master = posix_openpt (O_RDWR);
110 if (master == -1)
111 return -1;
112
113 if (grantpt (master))
114 goto fail;
115
116 if (unlockpt (master))
117 goto fail;
118
119#if 0
120 if (pts_name (master, &buf, sizeof (_buf)))
121#else
122 if (ptsname_r (master, buf, sizeof buf))
123#endif
124 goto fail;
125
126 slave = open (buf, O_RDWR | O_NOCTTY);
127 if (slave == -1)
128 {
129#if 0
130 if (buf != _buf)
131 free (buf);
132#endif
133 goto fail;
134 }
135
136 /* XXX Should we ignore errors here? */
137 if(termp)
138 tcsetattr (slave, TCSAFLUSH, termp);
139 if (winp)
140 ioctl (slave, TIOCSWINSZ, winp);
141
142 *amaster = master;
143 *aslave = slave;
144 if (name != NULL)
145 strcpy (name, buf);
146
147#if 0
148 if (buf != _buf)
149 free (buf);
150#endif
151 return 0;
152
153 fail:
154 close (master);
155 return -1;
156}
157libutil_hidden_def(openpty)