source: trunk/minix/commands/ftp/ftp.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 7.2 KB
RevLine 
[9]1/* ftp.c by Michael Temari 06/21/92
2 *
3 * ftp An ftp client program for use with TNET.
4 *
5 * Usage: ftp [[host] [port]]
6 *
7 * Version: 0.10 06/21/92 (pre-release not yet completed)
8 * 0.20 07/01/92
9 * 0.30 01/15/96 (Minix 1.7.1 initial release)
10 * 0.40 08/27/96
11 *
12 * Author: Michael Temari, <temari@ix.netcom.com>
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <ctype.h>
18#include <string.h>
19
20#include "ftp.h"
21#include "local.h"
22#include "file.h"
23#include "other.h"
24#include "net.h"
25
26FILE *fpcommin;
27FILE *fpcommout;
28
29int linkopen;
30int loggedin;
31int type;
32int format;
33int mode;
34int structure;
35int passive;
36int atty;
37
38int cmdargc;
39char *cmdargv[NUMARGS];
40
41char reply[1024];
42
43_PROTOTYPE(static int makeargs, (char *buff));
44_PROTOTYPE(int DOhelp, (void));
45_PROTOTYPE(int main, (int argc, char *argv[]));
46
47static int makeargs(buff)
48char *buff;
49{
50char *p;
51int i;
52
53 for(i = 0; i < NUMARGS; i++)
54 cmdargv[i] = (char *)0;
55
56 p = buff + strlen(buff) - 1;
57 while(p != buff)
58 if(*p == '\r' || *p == '\n' || isspace(*p))
59 *p-- = '\0';
60 else
61 break;
62
63 p = buff;
64 cmdargc = 0;
65 while(cmdargc < NUMARGS) {
66 while(*p && isspace(*p))
67 p++;
68 if(*p == '\0')
69 break;
70 cmdargv[cmdargc++] = p;
71 while(*p && !isspace(*p)) {
72 if(cmdargc == 1)
73 *p = tolower(*p);
74 p++;
75 }
76 if(*p == '\0')
77 break;
78 *p = '\0';
79 p++;
80 }
81}
82
83int readline(prompt, buff, len)
84char *prompt;
85char *buff;
86int len;
87{
88 printf(prompt); fflush(stdout);
89
90 if(fgets(buff, len, stdin) == (char *)NULL) {
91 printf("\nEnd of file on input!\n");
92 exit(1);
93 }
94
95 *strchr(buff, '\n') = 0;
96
97 if(!atty) {
98 printf("%s\n", buff);
99 fflush(stdout);
100 }
101
102 return(0);
103}
104
105int DOgetreply()
106{
107char *p;
108char buff[6];
109int s;
110int firsttime;
111
112 do {
113 firsttime = 1;
114 do {
115 if(fgets(reply, sizeof(reply), fpcommin) == (char *)0)
116 return(-1);
117 p = reply + strlen(reply) - 1;
118 while(p != reply)
119 if(*p == '\r' || *p == '\n' || isspace(*p))
120 *p-- = '\0';
121 else
122 break;
123 printf("%s\n", reply); fflush(stdout);
124 if(firsttime) {
125 firsttime = 0;
126 strncpy(buff, reply, 4);
127 buff[3] = ' ';
128 }
129 } while(strncmp(reply, buff, 3) || reply[3] == '-');
130 s = atoi(buff);
131 } while(s < 200 && s != 125 & s != 150);
132
133 return(s);
134}
135
136int DOcmdcheck()
137{
138 if(!linkopen) {
139 printf("You must \"OPEN\" a connection first.\n");
140 return(1);
141 }
142
143 if(!loggedin) {
144 printf("You must login first.\n");
145 return(1);
146 }
147
148 return(0);
149}
150
151int DOcommand(ftpcommand, ftparg)
152char *ftpcommand;
153char *ftparg;
154{
155 if(*ftparg)
156 fprintf(fpcommout, "%s %s\r\n", ftpcommand, ftparg);
157 else
158 fprintf(fpcommout, "%s\r\n", ftpcommand);
159
160 fflush(fpcommout);
161
162 return(DOgetreply());
163}
164
165int DOhelp()
166{
167char junk[10];
168
169 printf("Command: Description\n");
170 printf("! Escape to a shell\n");
171 printf("append Append a file to remote host\n");
172 printf("ascii Set file transfer mode to ascii\n");
173 printf("binary Set file transfer mode to binary\n");
174 printf("bye Close connection and exit\n");
175 printf("cd Change directory on remote host\n");
176 printf("close Close connection\n");
177 printf("del Remove file on remote host\n");
178 printf("dir Display long form remote host directory listing\n");
179 printf("exit Close connection and exit\n");
180 printf("get Retrieve a file from remote host\n");
181 printf("help Display this text\n");
182 printf("lcd Change directory on local host\n");
183 printf("ldir Display long form local host directory listing\n");
184 printf("lls Display local host directory listing\n");
185 printf("lmkdir Create directory on local host\n");
186 printf("lpwd Display current directory on local host\n");
187 printf("lrmdir Remove directory on local host\n");
188 printf("ls Display remote host directory listing\n");
189 printf("mget Retrieve multiple files from remote host\n");
190 printf("mkdir Create directory on remote host\n");
191 printf("mod Get file modification time\n");
192
193 readline("Press ENTER to continue... ", junk, sizeof(junk));
194
195 printf("mput Send multiple files to remote host\n");
196 printf("noop Send the ftp NOOP command\n");
197 printf("open Open connection to remote host\n");
198 printf("pass Enter remote user password\n");
199 printf("passive Toggle passive mode\n");
200 printf("put Send a file to remote host\n");
201 printf("putu Send a file to remote host(unique)\n");
202 printf("pwd Display current directory on remote host\n");
203 printf("quit Close connection and exit\n");
204 printf("quote Send raw ftp command to remote host\n");
205 printf("reget Restart a partial file retrieve from remote host\n");
206 printf("remotehelp Display ftp commands implemented on remote host\n");
207 printf("reput Restart a partial file send to remote host\n");
208 printf("rm Remove file on remote host\n");
209 printf("rmdir Remove directory on remote host\n");
210 printf("site Send a site specific command\n");
211 printf("size Get file size information\n");
212 printf("status Get connection/file status information\n");
213 printf("system Get remote system type information\n");
214 printf("user Enter remote user information\n");
215
216 return(0);
217}
218
219struct commands {
220 char *name;
221 _PROTOTYPE(int (*func), (void));
222};
223
224static struct commands commands[] = {
225 "!", DOlshell,
226 "append", DOappe,
227 "ascii", DOascii,
228 "binary", DObinary,
229 "bin", DObinary,
230 "bye", DOquit,
231 "cd", DOcd,
232 "close", DOclose,
233 "del", DOdelete,
234 "dir", DOlist,
235 "exit", DOquit,
236 "get", DOretr,
237 "help", DOhelp,
238 "lcd", DOlcd,
239 "ldir", DOllist,
240 "lls", DOlnlst,
241 "lmkdir", DOlmkdir,
242 "lpwd", DOlpwd,
243 "lrmdir", DOlrmdir,
244 "ls", DOnlst,
245 "mget", DOMretr,
246 "mkdir", DOmkdir,
247 "mod", DOmdtm,
248 "mput", DOMstor,
249 "noop", DOnoop,
250 "open", DOopen,
251 "pass", DOpass,
252 "passive", DOpassive,
253 "put", DOstor,
254 "putu", DOstou,
255 "pwd", DOpwd,
256 "quit", DOquit,
257 "quote", DOquote,
258 "reget", DOrretr,
259 "remotehelp", DOremotehelp,
260 "reput", DOrstor,
261 "rm", DOdelete,
262 "rmdir", DOrmdir,
263 "site", DOsite,
264 "size", DOsize,
265 "status", DOstat,
266 "system", DOsyst,
267 "user", DOuser,
268 "", (int (*)())0
269};
270
271int main(argc, argv)
272int argc;
273char *argv[];
274{
275int s;
276struct commands *cmd;
277static char buffer[128];
278
279 NETinit();
280
281 FTPinit();
282
283 s = 0;
284
285 if(argc > 1) {
286 sprintf(buffer, "open %s ", argv[1]);
287 makeargs(buffer);
288 s = DOopen();
289 if(atty && s > 0) {
290 sprintf(buffer, "user");
291 makeargs(buffer);
292 s = DOuser();
293 }
294 }
295
296 while(s >= 0) {
297 readline("ftp>", buffer, sizeof(buffer));
298 makeargs(buffer);
299 if(cmdargc == 0) continue;
300 for(cmd = commands; *cmd->name != '\0'; cmd++)
301 if(!strcmp(cmdargv[0], cmd->name))
302 break;
303 if(*cmd->name != '\0')
304 s = (*cmd->func)();
305 else {
306 s = 0;
307 printf("Command \"%s\" not recognized.\n", cmdargv[0]);
308 }
309 }
310
311 return(0);
312}
Note: See TracBrowser for help on using the repository browser.