146
|
1 /*****************************************************************************/
|
|
2 /* pty.c - general purpose routines */
|
|
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
|
|
4 /* */
|
|
5 /* This program is free software; you can redistribute it and/or modify */
|
|
6 /* it under the terms of the GNU General Public License as published by */
|
|
7 /* the Free Software Foundation; either version 2 of the License, or */
|
|
8 /* (at your option) any later version. */
|
|
9 /* */
|
|
10 /* This program 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 */
|
|
13 /* GNU General Public License for more details. */
|
|
14 /* */
|
|
15 /* You should have received a copy of the GNU General Public License */
|
|
16 /* along with this program; if not, write to the Free Software */
|
|
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
18 /*****************************************************************************/
|
|
19
|
|
20 static const char cvsid[] = "$Id$";
|
|
21
|
|
22 #include "gftp.h"
|
|
23
|
179
|
24 #ifdef __sgi
|
146
|
25
|
179
|
26 char *
|
|
27 get_pty_impl (void)
|
|
28 {
|
|
29 return ("sgi");
|
|
30 }
|
|
31
|
146
|
32
|
|
33 int
|
|
34 open_ptys (gftp_request * request, int *fdm, int *fds)
|
|
35 {
|
|
36 char *pts_name;
|
|
37
|
|
38 if ((pts_name = _getpty (fdm, O_RDWR, 0600, 0)) == NULL)
|
|
39 return (GFTP_ERETRYABLE);
|
|
40
|
|
41 if ((*fds = open (pts_name, O_RDWR)) < 0)
|
|
42 {
|
|
43 close (*fdm);
|
|
44 return (GFTP_ERETRYABLE);
|
|
45 }
|
|
46
|
|
47 return (0);
|
|
48 }
|
|
49
|
178
|
50 #elif HAVE_GRANTPT
|
146
|
51
|
179
|
52 char *
|
|
53 get_pty_impl (void)
|
|
54 {
|
|
55 return ("unix98");
|
|
56 }
|
|
57
|
|
58
|
146
|
59 int
|
|
60 open_ptys (gftp_request * request, int *fdm, int *fds)
|
|
61 {
|
|
62 char *pts_name;
|
|
63
|
|
64 if ((*fdm = open ("/dev/ptmx", O_RDWR)) < 0)
|
|
65 return (GFTP_ERETRYABLE);
|
|
66
|
|
67 if (grantpt (*fdm) < 0)
|
|
68 {
|
|
69 close (*fdm);
|
|
70 return (GFTP_ERETRYABLE);
|
|
71 }
|
|
72
|
|
73 if (unlockpt (*fdm) < 0)
|
|
74 {
|
|
75 close (*fdm);
|
|
76 return (GFTP_ERETRYABLE);
|
|
77 }
|
|
78
|
|
79 if ((pts_name = ptsname (*fdm)) == NULL)
|
|
80 {
|
|
81 close (*fdm);
|
|
82 return (GFTP_ERETRYABLE);
|
|
83 }
|
|
84
|
|
85 if ((*fds = open (pts_name, O_RDWR)) < 0)
|
|
86 {
|
|
87 close (*fdm);
|
|
88 return (GFTP_ERETRYABLE);
|
|
89 }
|
|
90
|
179
|
91 #ifdef SYSV
|
146
|
92 /* I intentionally ignore these errors */
|
|
93 ioctl (*fds, I_PUSH, "ptem");
|
|
94 ioctl (*fds, I_PUSH, "ldterm");
|
|
95 ioctl (*fds, I_PUSH, "ttcompat");
|
179
|
96 #endif
|
146
|
97
|
|
98 return (0);
|
|
99 }
|
|
100
|
178
|
101 #elif HAVE_OPENPTY
|
146
|
102
|
179
|
103 char *
|
|
104 get_pty_impl (void)
|
|
105 {
|
|
106 return ("openpty");
|
|
107 }
|
|
108
|
|
109
|
146
|
110 int
|
|
111 open_ptys (gftp_request * request, int *fdm, int *fds)
|
|
112 {
|
|
113 char *pts_name;
|
|
114
|
|
115 if (openpty (fdm, fds, &pts_name, NULL, NULL ) < 0)
|
|
116 return (GFTP_ERETRYABLE);
|
|
117
|
|
118 ioctl (*fds, TIOCSCTTY, NULL);
|
|
119
|
|
120 return (0);
|
|
121 }
|
|
122
|
|
123 #else /* !HAVE_OPENPTY */
|
|
124
|
|
125 /* Fall back to *BSD... */
|
|
126
|
179
|
127 char *
|
|
128 get_pty_impl (void)
|
|
129 {
|
|
130 return ("bsd");
|
|
131 }
|
|
132
|
|
133
|
146
|
134 int
|
|
135 open_ptys (gftp_request * request, int *fdm, int *fds)
|
|
136 {
|
|
137 char pts_name[20], *pos1, *pos2;
|
|
138
|
|
139 strncpy (pts_name, "/dev/ptyXY", sizeof (pts_name));
|
|
140 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++)
|
|
141 {
|
|
142 pts_name[8] = *pos1;
|
|
143 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++)
|
|
144 {
|
|
145 pts_name[9] = *pos2;
|
|
146 if ((*fdm = open (pts_name, O_RDWR)) < 0)
|
|
147 continue;
|
|
148
|
|
149 pts_name[5] = 't';
|
|
150 chmod (pts_name, S_IRUSR | S_IWUSR);
|
|
151 chown (pts_name, getuid (), -1);
|
|
152
|
|
153 if ((*fds = open (pts_name, O_RDWR)) < 0)
|
|
154 {
|
|
155 pts_name[5] = 'p';
|
|
156 continue;
|
|
157 }
|
|
158
|
|
159 #if defined(TIOCSCTTY) && !defined(CIBAUD)
|
|
160 ioctl (*fds, TIOCSCTTY, NULL);
|
|
161 #endif
|
|
162
|
|
163 return (0);
|
|
164 }
|
|
165 }
|
|
166
|
|
167 return (GFTP_ERETRYABLE);
|
|
168 }
|
|
169
|
|
170 #endif /* __sgi */
|
|
171
|
|
172
|
|
173 int
|
|
174 tty_raw (int fd)
|
|
175 {
|
|
176 struct termios buf;
|
|
177
|
|
178 if (tcgetattr (fd, &buf) < 0)
|
|
179 return (-1);
|
|
180
|
|
181 buf.c_iflag |= IGNPAR;
|
|
182 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR);
|
|
183 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL);
|
|
184 #ifdef IEXTEN
|
|
185 buf.c_lflag &= ~(IEXTEN);
|
|
186 #endif
|
|
187
|
|
188 buf.c_oflag &= ~(OPOST);
|
|
189 buf.c_cc[VMIN] = 1;
|
|
190 buf.c_cc[VTIME] = 0;
|
|
191
|
|
192 if (tcsetattr (fd, TCSADRAIN, &buf) < 0)
|
|
193 return (-1);
|
|
194 return (0);
|
|
195 }
|
|
196
|
|
197
|