1 /* ftp.c Copyright 1992-2000 by Michael Temari All Rights Reserved
3 * ftp An ftp client program for use with TNET.
7 * Version: 0.10 06/21/92 (pre-release not yet completed)
9 * 0.30 01/15/96 (Minix 1.7.1 initial release)
12 * 1.00 12/12/03 (added ver command)
15 * Author: Michael Temari, <Michael@TemWare.Com>
18 #include <sys/types.h>
31 char *FtpVersion
= "1.01 02/07/05";
43 char *cmdargv
[NUMARGS
];
49 /* Already declared in stdio.h */
50 #define getline ftp_getline
53 static void makeargs(char *buff
);
56 static int getline(char *line
, int len
);
57 int main(int argc
, char *argv
[]);
60 static void makeargs(buff
)
66 for(i
= 0; i
< NUMARGS
; i
++)
67 cmdargv
[i
] = (char *)0;
69 p
= buff
+ strlen(buff
) - 1;
71 if(*p
== '\r' || *p
== '\n' || isspace(*p
))
78 while(cmdargc
< NUMARGS
) {
79 while(*p
&& isspace(*p
))
83 cmdargv
[cmdargc
++] = p
;
84 while(*p
&& !isspace(*p
)) {
96 int readline(prompt
, buff
, len
)
103 printf(prompt
); fflush(stdout
);
105 if(fgets(buff
, len
, stdin
) == (char *)NULL
) {
106 printf("\nEnd of file on input!\n");
110 p
= buff
+ strlen(buff
) - 1;
112 if(*p
== '\r' || *p
== '\n' || isspace(*p
))
118 printf("%s\n", buff
);
125 static int getline(line
, len
)
132 /* leave room for at end for null */
135 /* got to be able to put in at least 1 character */
141 s
= read(ftpcomm_fd
, line
, 1);
146 gotcr
= (*line
== '\r');
166 if((s
= getline(reply
, sizeof(reply
))) < 0)
169 printf("%s\n", reply
);
174 strncpy(code
, reply
, 3);
177 } while(strncmp(reply
, code
, 3) || reply
[3] == '-');
179 } while(s
< 200 && s
!= 125 && s
!= 150);
187 printf("You must \"OPEN\" a connection first.\n");
192 printf("You must login first.\n");
199 int DOcommand(ftpcommand
, ftparg
)
207 sprintf(ss
, "%s %s\r\n", ftpcommand
, ftparg
);
209 sprintf(ss
, "%s\r\n", ftpcommand
);
211 s
= write(ftpcomm_fd
, ss
, strlen(ss
));
216 s
= write(ftpcomm_fd
, ftpcommand
, strlen(ftpcommand
));
217 if(s
!= strlen(ftpcommand
))
221 s
= write(ftpcomm_fd
, " ", 1);
225 s
= write(ftpcomm_fd
, ftparg
, strlen(ftparg
));
226 if(s
!= strlen(ftparg
))
230 s
= write(ftpcomm_fd
, "\r\n", 2);
235 return(DOgetreply());
240 printf("FTP Version %s\n", FtpVersion
);
248 printf("Command: Description\n");
249 printf("! Escape to a shell\n");
250 printf("append Append a file to remote host\n");
251 printf("ascii Set file transfer type to ascii\n");
252 printf("binary Set file transfer type to binary\n");
253 printf("block Set file transfer mode to block\n");
254 printf("bye Close connection and exit\n");
255 printf("cd Change directory on remote host\n");
256 printf("close Close connection\n");
257 printf("clone Clone a file\n");
258 printf("del Remove file on remote host\n");
259 printf("dir Display long form remote host directory listing\n");
260 printf("exit Close connection and exit\n");
261 printf("get Retrieve a file from remote host\n");
262 printf("help Display this text\n");
264 if(readline("Press ENTER to continue... ", junk
, sizeof(junk
)))
267 printf("lcd Change directory on local host\n");
268 printf("ldir Display long form local host directory listing\n");
269 printf("lls Display local host directory listing\n");
270 printf("lmkdir Create directory on local host\n");
271 printf("lpwd Display current directory on local host\n");
272 printf("lrmdir Remove directory on local host\n");
273 printf("ls Display remote host directory listing\n");
274 printf("mget Retrieve multiple files from remote host\n");
275 printf("mkdir Create directory on remote host\n");
276 printf("mod Get file modification time\n");
277 printf("mput Send multiple files to remote host\n");
278 printf("noop Send the ftp NOOP command\n");
280 if(readline("Press ENTER to continue... ", junk
, sizeof(junk
)))
283 printf("open Open connection to remote host\n");
284 printf("pass Enter remote user password\n");
285 printf("passive Toggle passive mode\n");
286 printf("put Send a file to remote host\n");
287 printf("putu Send a file to remote host(unique)\n");
288 printf("pwd Display current directory on remote host\n");
289 printf("quit Close connection and exit\n");
290 printf("quote Send raw ftp command to remote host\n");
291 printf("reget Restart a partial file retrieve from remote host\n");
292 printf("remotehelp Display ftp commands implemented on remote host\n");
293 printf("reput Restart a partial file send to remote host\n");
294 printf("rm Remove file on remote host\n");
295 printf("rmdir Remove directory on remote host\n");
297 if(readline("Press ENTER to continue... ", junk
, sizeof(junk
)))
300 printf("site Send a site specific command\n");
301 printf("size Get file size information\n");
302 printf("status Get connection/file status information\n");
303 printf("stream Set file transfer mode to stream\n");
304 printf("system Get remote system type information\n");
305 printf("user Enter remote user information\n");
306 printf("ver Display client version information\n");
316 static struct commands commands
[] = {
345 "passive", DOpassive
,
352 "remotehelp", DOremotehelp
,
371 struct commands
*cmd
;
372 static char buffer
[128];
382 sprintf(buffer
, "open %s ", argv
[1]);
386 sprintf(buffer
, "user");
393 s
= readline("ftp>", buffer
, sizeof(buffer
));
396 if(cmdargc
== 0) continue;
397 for(cmd
= commands
; *cmd
->name
!= '\0'; cmd
++)
398 if(!strcmp(cmdargv
[0], cmd
->name
))
400 if(*cmd
->name
!= '\0')
404 printf("Command \"%s\" not recognized.\n", cmdargv
[0]);