1 /* Copyright (C) 1998, 1999 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.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
28 #include <sys/types.h>
30 extern int getpt (void);
31 extern int grantpt (int fd);
32 extern int ptsname_r (int fd, char *buf, size_t buflen);
33 extern int unlockpt (int fd);
35 /* BCS: the following function is, IMO, overkill */
37 /* Return the result of ptsname_r in the buffer pointed to by PTS,
38 which should be of length BUF_LEN. If it is too long to fit in
39 this buffer, a sufficiently long buffer is allocated using malloc,
40 and returned in PTS. 0 is returned upon success, -1 otherwise. */
42 pts_name (int fd, char **pts, size_t buf_len)
53 rv = ptsname_r (fd, buf, buf_len);
55 if (rv != 0 || memchr (buf, '\0', buf_len))
56 /* We either got an error, or we succeeded and the
57 returned name fit in the buffer. */
60 /* Try again with a longer buffer. */
61 buf_len += buf_len; /* Double it */
64 /* No initial buffer; start out by mallocing one. */
65 buf_len = 128; /* First time guess. */
68 /* We've already malloced another buffer at least once. */
69 new_buf = realloc (buf, buf_len);
71 new_buf = malloc (buf_len);
82 *pts = buf; /* Return buffer to the user. */
84 free (buf); /* Free what we malloced when returning an error. */
90 /* Create pseudo tty master slave pair and set terminal attributes
91 according to TERMP and WINP. Return handles for both ends in
92 AMASTER and ASLAVE, and return the name of the slave end in NAME. */
94 __openpty (int *amaster, int *aslave, char *name, struct termios *termp,
117 if (grantpt (master))
120 if (unlockpt (master))
124 if (pts_name (master, &buf, sizeof (_buf)))
126 if (ptsname_r (master, buf, sizeof buf))
130 slave = open (buf, O_RDWR | O_NOCTTY);
140 /* XXX Should we ignore errors here? */
142 tcsetattr (slave, TCSAFLUSH, termp);
144 ioctl (slave, TIOCSWINSZ, winp);
162 strong_alias(__openpty,openpty)