1 /* 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.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
27 #include <sys/types.h>
30 /* Return the result of ptsname_r in the buffer pointed to by PTS,
31 which should be of length BUF_LEN. If it is too long to fit in
32 this buffer, a sufficiently long buffer is allocated using malloc,
33 and returned in PTS. 0 is returned upon success, -1 otherwise. */
35 pts_name (int fd
, char **pts
, size_t buf_len
)
46 rv
= ptsname_r (fd
, buf
, buf_len
);
48 if (rv
!= 0 || memchr (buf
, '\0', buf_len
))
49 /* We either got an error, or we succeeded and the
50 returned name fit in the buffer. */
53 /* Try again with a longer buffer. */
54 buf_len
+= buf_len
; /* Double it */
57 /* No initial buffer; start out by mallocing one. */
58 buf_len
= 128; /* First time guess. */
61 /* We've already malloced another buffer at least once. */
62 new_buf
= realloc (buf
, buf_len
);
64 new_buf
= malloc (buf_len
);
75 *pts
= buf
; /* Return buffer to the user. */
77 free (buf
); /* Free what we malloced when returning an error. */
82 /* Create pseudo tty master slave pair and set terminal attributes
83 according to TERMP and WINP. Return handles for both ends in
84 AMASTER and ASLAVE, and return the name of the slave end in NAME. */
86 openpty (int *amaster
, int *aslave
, char *name
,
87 const struct termios
*termp
, const struct winsize
*winp
)
101 if (grantpt (master
))
104 if (unlockpt (master
))
107 if (pts_name (master
, &buf
, sizeof (_buf
)))
110 slave
= open (buf
, O_RDWR
| O_NOCTTY
);
119 /* XXX Should we ignore errors here? */
121 tcsetattr (slave
, TCSAFLUSH
, termp
);
123 ioctl (slave
, TIOCSWINSZ
, winp
);
138 libutil_hidden_def (openpty
)