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
|
|
24
|
|
25 #ifdef __sgi
|
|
26
|
|
27 int
|
|
28 open_ptys (gftp_request * request, int *fdm, int *fds)
|
|
29 {
|
|
30 char *pts_name;
|
|
31
|
|
32 if ((pts_name = _getpty (fdm, O_RDWR, 0600, 0)) == NULL)
|
|
33 return (GFTP_ERETRYABLE);
|
|
34
|
|
35 if ((*fds = open (pts_name, O_RDWR)) < 0)
|
|
36 {
|
|
37 close (*fdm);
|
|
38 return (GFTP_ERETRYABLE);
|
|
39 }
|
|
40
|
|
41 return (0);
|
|
42 }
|
|
43
|
|
44 #else /* !__sgi */
|
|
45
|
|
46 #ifdef HAVE_GRANTPT
|
|
47
|
|
48 int
|
|
49 open_ptys (gftp_request * request, int *fdm, int *fds)
|
|
50 {
|
|
51 char *pts_name;
|
|
52
|
|
53 if ((*fdm = open ("/dev/ptmx", O_RDWR)) < 0)
|
|
54 return (GFTP_ERETRYABLE);
|
|
55
|
|
56 if (grantpt (*fdm) < 0)
|
|
57 {
|
|
58 close (*fdm);
|
|
59 return (GFTP_ERETRYABLE);
|
|
60 }
|
|
61
|
|
62 if (unlockpt (*fdm) < 0)
|
|
63 {
|
|
64 close (*fdm);
|
|
65 return (GFTP_ERETRYABLE);
|
|
66 }
|
|
67
|
|
68 if ((pts_name = ptsname (*fdm)) == NULL)
|
|
69 {
|
|
70 close (*fdm);
|
|
71 return (GFTP_ERETRYABLE);
|
|
72 }
|
|
73
|
|
74 if ((*fds = open (pts_name, O_RDWR)) < 0)
|
|
75 {
|
|
76 close (*fdm);
|
|
77 return (GFTP_ERETRYABLE);
|
|
78 }
|
|
79
|
|
80 /* I intentionally ignore these errors */
|
|
81 ioctl (*fds, I_PUSH, "ptem");
|
|
82 ioctl (*fds, I_PUSH, "ldterm");
|
|
83 ioctl (*fds, I_PUSH, "ttcompat");
|
|
84
|
|
85 return (0);
|
|
86 }
|
|
87
|
|
88 #else /* !HAVE_GRANTPT */
|
|
89
|
|
90 #ifdef HAVE_OPENPTY
|
|
91
|
|
92 int
|
|
93 open_ptys (gftp_request * request, int *fdm, int *fds)
|
|
94 {
|
|
95 char *pts_name;
|
|
96
|
|
97 if (openpty (fdm, fds, &pts_name, NULL, NULL ) < 0)
|
|
98 return (GFTP_ERETRYABLE);
|
|
99
|
|
100 ioctl (*fds, TIOCSCTTY, NULL);
|
|
101
|
|
102 return (0);
|
|
103 }
|
|
104
|
|
105 #else /* !HAVE_OPENPTY */
|
|
106
|
|
107 /* Fall back to *BSD... */
|
|
108
|
|
109 int
|
|
110 open_ptys (gftp_request * request, int *fdm, int *fds)
|
|
111 {
|
|
112 char pts_name[20], *pos1, *pos2;
|
|
113
|
|
114 strncpy (pts_name, "/dev/ptyXY", sizeof (pts_name));
|
|
115 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++)
|
|
116 {
|
|
117 pts_name[8] = *pos1;
|
|
118 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++)
|
|
119 {
|
|
120 pts_name[9] = *pos2;
|
|
121 if ((*fdm = open (pts_name, O_RDWR)) < 0)
|
|
122 continue;
|
|
123
|
|
124 pts_name[5] = 't';
|
|
125 chmod (pts_name, S_IRUSR | S_IWUSR);
|
|
126 chown (pts_name, getuid (), -1);
|
|
127
|
|
128 if ((*fds = open (pts_name, O_RDWR)) < 0)
|
|
129 {
|
|
130 pts_name[5] = 'p';
|
|
131 continue;
|
|
132 }
|
|
133
|
|
134 #if defined(TIOCSCTTY) && !defined(CIBAUD)
|
|
135 ioctl (*fds, TIOCSCTTY, NULL);
|
|
136 #endif
|
|
137
|
|
138 return (0);
|
|
139 }
|
|
140 }
|
|
141
|
|
142 return (GFTP_ERETRYABLE);
|
|
143 }
|
|
144
|
|
145 #endif /* HAVE_OPENPTY */
|
|
146
|
|
147 #endif /* HAVE_GRANTPT */
|
|
148
|
|
149 #endif /* __sgi */
|
|
150
|
|
151
|
|
152 int
|
|
153 tty_raw (int fd)
|
|
154 {
|
|
155 struct termios buf;
|
|
156
|
|
157 if (tcgetattr (fd, &buf) < 0)
|
|
158 return (-1);
|
|
159
|
|
160 buf.c_iflag |= IGNPAR;
|
|
161 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR);
|
|
162 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL);
|
|
163 #ifdef IEXTEN
|
|
164 buf.c_lflag &= ~(IEXTEN);
|
|
165 #endif
|
|
166
|
|
167 buf.c_oflag &= ~(OPOST);
|
|
168 buf.c_cc[VMIN] = 1;
|
|
169 buf.c_cc[VTIME] = 0;
|
|
170
|
|
171 if (tcsetattr (fd, TCSADRAIN, &buf) < 0)
|
|
172 return (-1);
|
|
173 return (0);
|
|
174 }
|
|
175
|
|
176
|