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