forget difference between big and small commands - obsolete with vm.
[minix.git] / commands / ftp101 / ftp.c
blob20c1661b9badad66f80ae7ecdc35a94228f69998
1 /* ftp.c Copyright 1992-2000 by Michael Temari All Rights Reserved
3 * ftp An ftp client program for use with TNET.
5 * Usage: ftp [host]
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 * 0.50 03/08/00
12 * 1.00 12/12/03 (added ver command)
13 * 1.01 02/07/05
15 * Author: Michael Temari, <Michael@TemWare.Com>
18 #include <sys/types.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include "ftp.h"
26 #include "local.h"
27 #include "file.h"
28 #include "other.h"
29 #include "net.h"
31 char *FtpVersion = "1.01 02/07/05";
33 int linkopen;
34 int loggedin;
35 int type;
36 int format;
37 int mode;
38 int structure;
39 int passive;
40 int atty;
42 int cmdargc;
43 char *cmdargv[NUMARGS];
45 int printreply = 1;
46 char reply[1024];
48 _PROTOTYPE(static void makeargs, (char *buff));
49 _PROTOTYPE(int DOver, (void));
50 _PROTOTYPE(int DOhelp, (void));
51 _PROTOTYPE(static int getline, (char *line, int len));
52 _PROTOTYPE(int main, (int argc, char *argv[]));
55 static void makeargs(buff)
56 char *buff;
58 int i;
59 char *p;
61 for(i = 0; i < NUMARGS; i++)
62 cmdargv[i] = (char *)0;
64 p = buff + strlen(buff) - 1;
65 while(p >= buff)
66 if(*p == '\r' || *p == '\n' || isspace(*p))
67 *p-- = '\0';
68 else
69 break;
71 p = buff;
72 cmdargc = 0;
73 while(cmdargc < NUMARGS) {
74 while(*p && isspace(*p))
75 p++;
76 if(*p == '\0')
77 break;
78 cmdargv[cmdargc++] = p;
79 while(*p && !isspace(*p)) {
80 if(cmdargc == 1)
81 *p = tolower(*p);
82 p++;
84 if(*p == '\0')
85 break;
86 *p = '\0';
87 p++;
91 int readline(prompt, buff, len)
92 char *prompt;
93 char *buff;
94 int len;
96 char *p;
98 printf(prompt); fflush(stdout);
100 if(fgets(buff, len, stdin) == (char *)NULL) {
101 printf("\nEnd of file on input!\n");
102 return(-1);
105 p = buff + strlen(buff) - 1;
106 while(p >= buff)
107 if(*p == '\r' || *p == '\n' || isspace(*p))
108 *p-- = '\0';
109 else
110 break;
112 if(!atty) {
113 printf("%s\n", buff);
114 fflush(stdout);
117 return(0);
120 static int getline(line, len)
121 char *line;
122 int len;
124 int s;
125 int gotcr;
127 /* leave room for at end for null */
128 len--;
130 /* got to be able to put in at least 1 character */
131 if(len < 1)
132 return(-1);
134 gotcr = 0;
135 while(len-- > 0) {
136 s = read(ftpcomm_fd, line, 1);
137 if(s != 1)
138 return(-1);
139 if(*line == '\n')
140 break;
141 gotcr = (*line == '\r');
142 line++;
144 if(gotcr)
145 --line;
147 *line = '\0';
149 return(0);
152 int DOgetreply()
154 int firsttime;
155 int s;
156 char code[4];
158 do {
159 firsttime = 1;
160 do {
161 if((s = getline(reply, sizeof(reply))) < 0)
162 return(s);
163 if(printreply) {
164 printf("%s\n", reply);
165 fflush(stdout);
167 if(firsttime) {
168 firsttime = 0;
169 strncpy(code, reply, 3);
170 code[3] = '\0';
172 } while(strncmp(reply, code, 3) || reply[3] == '-');
173 s = atoi(code);
174 } while(s < 200 && s != 125 && s != 150);
176 return(s);
179 int DOcmdcheck()
181 if(!linkopen) {
182 printf("You must \"OPEN\" a connection first.\n");
183 return(1);
186 if(!loggedin) {
187 printf("You must login first.\n");
188 return(1);
191 return(0);
194 int DOcommand(ftpcommand, ftparg)
195 char *ftpcommand;
196 char *ftparg;
198 int s;
199 #if 1
200 static char ss[64];
201 if(*ftparg)
202 sprintf(ss, "%s %s\r\n", ftpcommand, ftparg);
203 else
204 sprintf(ss, "%s\r\n", ftpcommand);
206 s = write(ftpcomm_fd, ss, strlen(ss));
207 if(s != strlen(ss))
208 return(-1);
210 #else
211 s = write(ftpcomm_fd, ftpcommand, strlen(ftpcommand));
212 if(s != strlen(ftpcommand))
213 return(-1);
215 if(*ftparg) {
216 s = write(ftpcomm_fd, " ", 1);
217 if(s != 1)
218 return(-1);
220 s = write(ftpcomm_fd, ftparg, strlen(ftparg));
221 if(s != strlen(ftparg))
222 return(-1);
225 s = write(ftpcomm_fd, "\r\n", 2);
226 if(s != 2)
227 return(-1);
228 #endif
230 return(DOgetreply());
233 int DOver()
235 printf("FTP Version %s\n", FtpVersion);
236 return(0);
239 int DOhelp()
241 char junk[10];
243 printf("Command: Description\n");
244 printf("! Escape to a shell\n");
245 printf("append Append a file to remote host\n");
246 printf("ascii Set file transfer type to ascii\n");
247 printf("binary Set file transfer type to binary\n");
248 printf("block Set file transfer mode to block\n");
249 printf("bye Close connection and exit\n");
250 printf("cd Change directory on remote host\n");
251 printf("close Close connection\n");
252 printf("clone Clone a file\n");
253 printf("del Remove file on remote host\n");
254 printf("dir Display long form remote host directory listing\n");
255 printf("exit Close connection and exit\n");
256 printf("get Retrieve a file from remote host\n");
257 printf("help Display this text\n");
259 if(readline("Press ENTER to continue... ", junk, sizeof(junk)))
260 return(-1);
262 printf("lcd Change directory on local host\n");
263 printf("ldir Display long form local host directory listing\n");
264 printf("lls Display local host directory listing\n");
265 printf("lmkdir Create directory on local host\n");
266 printf("lpwd Display current directory on local host\n");
267 printf("lrmdir Remove directory on local host\n");
268 printf("ls Display remote host directory listing\n");
269 printf("mget Retrieve multiple files from remote host\n");
270 printf("mkdir Create directory on remote host\n");
271 printf("mod Get file modification time\n");
272 printf("mput Send multiple files to remote host\n");
273 printf("noop Send the ftp NOOP command\n");
275 if(readline("Press ENTER to continue... ", junk, sizeof(junk)))
276 return(-1);
278 printf("open Open connection to remote host\n");
279 printf("pass Enter remote user password\n");
280 printf("passive Toggle passive mode\n");
281 printf("put Send a file to remote host\n");
282 printf("putu Send a file to remote host(unique)\n");
283 printf("pwd Display current directory on remote host\n");
284 printf("quit Close connection and exit\n");
285 printf("quote Send raw ftp command to remote host\n");
286 printf("reget Restart a partial file retrieve from remote host\n");
287 printf("remotehelp Display ftp commands implemented on remote host\n");
288 printf("reput Restart a partial file send to remote host\n");
289 printf("rm Remove file on remote host\n");
290 printf("rmdir Remove directory on remote host\n");
292 if(readline("Press ENTER to continue... ", junk, sizeof(junk)))
293 return(-1);
295 printf("site Send a site specific command\n");
296 printf("size Get file size information\n");
297 printf("status Get connection/file status information\n");
298 printf("stream Set file transfer mode to stream\n");
299 printf("system Get remote system type information\n");
300 printf("user Enter remote user information\n");
301 printf("ver Display client version information\n");
303 return(0);
306 struct commands {
307 char *name;
308 _PROTOTYPE(int (*func), (void));
311 static struct commands commands[] = {
312 "!", DOlshell,
313 "append", DOappe,
314 "ascii", DOascii,
315 "binary", DObinary,
316 "block", DOblock,
317 "bye", DOquit,
318 "cd", DOcd,
319 "close", DOclose,
320 "clone", DOclone,
321 "del", DOdelete,
322 "dir", DOlist,
323 "exit", DOquit,
324 "get", DOretr,
325 "help", DOhelp,
326 "lcd", DOlcd,
327 "ldir", DOllist,
328 "lls", DOlnlst,
329 "lmkdir", DOlmkdir,
330 "lpwd", DOlpwd,
331 "lrmdir", DOlrmdir,
332 "ls", DOnlst,
333 "mget", DOMretr,
334 "mkdir", DOmkdir,
335 "mod", DOmdtm,
336 "mput", DOMstor,
337 "noop", DOnoop,
338 "open", DOopen,
339 "pass", DOpass,
340 "passive", DOpassive,
341 "put", DOstor,
342 "putu", DOstou,
343 "pwd", DOpwd,
344 "quit", DOquit,
345 "quote", DOquote,
346 "reget", DOrretr,
347 "remotehelp", DOremotehelp,
348 "reput", DOrstor,
349 "rm", DOdelete,
350 "rmdir", DOrmdir,
351 "site", DOsite,
352 "size", DOsize,
353 "status", DOstat,
354 "stream", DOstream,
355 "system", DOsyst,
356 "user", DOuser,
357 "ver", DOver,
358 "", (int (*)())0
361 int main(argc, argv)
362 int argc;
363 char *argv[];
365 int s;
366 struct commands *cmd;
367 static char buffer[128];
369 if(NETinit())
370 return(-1);
372 FTPinit();
374 s = 0;
376 if(argc > 1) {
377 sprintf(buffer, "open %s ", argv[1]);
378 makeargs(buffer);
379 s = DOopen();
380 if(atty && s > 0) {
381 sprintf(buffer, "user");
382 makeargs(buffer);
383 s = DOuser();
387 while(s >= 0) {
388 s = readline("ftp>", buffer, sizeof(buffer));
389 if(s < 0) break;
390 makeargs(buffer);
391 if(cmdargc == 0) continue;
392 for(cmd = commands; *cmd->name != '\0'; cmd++)
393 if(!strcmp(cmdargv[0], cmd->name))
394 break;
395 if(*cmd->name != '\0')
396 s = (*cmd->func)();
397 else {
398 s = 0;
399 printf("Command \"%s\" not recognized.\n", cmdargv[0]);
403 return(s);