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