ftp.c

Go to the documentation of this file.
00001 /* ftp.c by Michael Temari 06/21/92
00002  *
00003  * ftp          An ftp client program for use with TNET.
00004  *
00005  * Usage:       ftp [[host] [port]]
00006  *
00007  * Version:     0.10    06/21/92 (pre-release not yet completed)
00008  *              0.20    07/01/92
00009  *              0.30    01/15/96 (Minix 1.7.1 initial release)
00010  *              0.40    08/27/96
00011  *
00012  * Author:      Michael Temari, <temari@ix.netcom.com>
00013  */
00014 
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <ctype.h>
00018 #include <string.h>
00019 
00020 #include "ftp.h"
00021 #include "local.h"
00022 #include "file.h"
00023 #include "other.h"
00024 #include "net.h"
00025 
00026 FILE *fpcommin;
00027 FILE *fpcommout;
00028 
00029 int linkopen;
00030 int loggedin;
00031 int type;
00032 int format;
00033 int mode;
00034 int structure;
00035 int passive;
00036 int atty;
00037 
00038 int cmdargc;
00039 char *cmdargv[NUMARGS];
00040 
00041 char reply[1024];
00042 
00043 _PROTOTYPE(static int makeargs, (char *buff));
00044 _PROTOTYPE(int DOhelp, (void));
00045 _PROTOTYPE(int main, (int argc, char *argv[]));
00046 
00047 static int makeargs(buff)
00048 char *buff;
00049 {
00050 char *p;
00051 int i;
00052 
00053    for(i = 0; i < NUMARGS; i++)
00054         cmdargv[i] = (char *)0;
00055 
00056    p = buff + strlen(buff) - 1;
00057    while(p != buff)
00058         if(*p == '\r' || *p == '\n' || isspace(*p))
00059                 *p-- = '\0';
00060         else
00061                 break;
00062 
00063    p = buff;
00064    cmdargc = 0;
00065    while(cmdargc < NUMARGS) {
00066         while(*p && isspace(*p))
00067                 p++;
00068         if(*p == '\0')
00069                 break;
00070         cmdargv[cmdargc++] = p;
00071         while(*p && !isspace(*p)) {
00072                 if(cmdargc == 1)
00073                         *p = tolower(*p);
00074                 p++;
00075         }
00076         if(*p == '\0')
00077                 break;
00078         *p = '\0';
00079         p++;
00080    }
00081 }
00082 
00083 int readline(prompt, buff, len)
00084 char *prompt;
00085 char *buff;
00086 int len;
00087 {
00088    printf(prompt); fflush(stdout);
00089 
00090    if(fgets(buff, len, stdin) == (char *)NULL) {
00091         printf("\nEnd of file on input!\n");
00092         exit(1);
00093    }
00094 
00095    *strchr(buff, '\n') = 0;
00096 
00097    if(!atty) {
00098         printf("%s\n", buff);
00099         fflush(stdout);
00100    }
00101 
00102    return(0);
00103 }
00104 
00105 int DOgetreply()
00106 {
00107 char *p;
00108 char buff[6];
00109 int s;
00110 int firsttime;
00111 
00112    do {
00113         firsttime = 1;
00114         do {
00115                 if(fgets(reply, sizeof(reply), fpcommin) == (char *)0)
00116                         return(-1);
00117                 p = reply + strlen(reply) - 1;
00118                 while(p != reply)
00119                         if(*p == '\r' || *p == '\n' || isspace(*p))
00120                                 *p-- = '\0';
00121                         else
00122                                 break;
00123                 printf("%s\n", reply); fflush(stdout);
00124                 if(firsttime) {
00125                         firsttime = 0;
00126                         strncpy(buff, reply, 4);
00127                         buff[3] = ' ';
00128                 }
00129            } while(strncmp(reply, buff, 3) || reply[3] == '-');
00130            s = atoi(buff);
00131    } while(s < 200 && s != 125 & s != 150);
00132 
00133    return(s);
00134 }
00135 
00136 int DOcmdcheck()
00137 {
00138    if(!linkopen) {
00139         printf("You must \"OPEN\" a connection first.\n");
00140         return(1);
00141    }
00142 
00143    if(!loggedin) {
00144         printf("You must login first.\n");
00145         return(1);
00146    }
00147 
00148    return(0);
00149 }
00150 
00151 int DOcommand(ftpcommand, ftparg)
00152 char *ftpcommand;
00153 char *ftparg;
00154 {
00155    if(*ftparg)
00156         fprintf(fpcommout, "%s %s\r\n", ftpcommand, ftparg);
00157    else
00158         fprintf(fpcommout, "%s\r\n", ftpcommand);
00159 
00160    fflush(fpcommout);
00161 
00162    return(DOgetreply());
00163 }
00164 
00165 int DOhelp()
00166 {
00167 char junk[10];
00168 
00169    printf("Command:      Description\n");
00170    printf("!             Escape to a shell\n");
00171    printf("append        Append a file to remote host\n");
00172    printf("ascii         Set file transfer mode to ascii\n");
00173    printf("binary        Set file transfer mode to binary\n");
00174    printf("bye           Close connection and exit\n");
00175    printf("cd            Change directory on remote host\n");
00176    printf("close         Close connection\n");
00177    printf("del           Remove file on remote host\n");
00178    printf("dir           Display long form remote host directory listing\n");
00179    printf("exit          Close connection and exit\n");
00180    printf("get           Retrieve a file from remote host\n");
00181    printf("help          Display this text\n");
00182    printf("lcd           Change directory on local host\n");
00183    printf("ldir          Display long form local host directory listing\n");
00184    printf("lls           Display local host directory listing\n");
00185    printf("lmkdir        Create directory on local host\n");
00186    printf("lpwd          Display current directory on local host\n");
00187    printf("lrmdir        Remove directory on local host\n");
00188    printf("ls            Display remote host directory listing\n");
00189    printf("mget          Retrieve multiple files from remote host\n");
00190    printf("mkdir         Create directory on remote host\n");
00191    printf("mod           Get file modification time\n");
00192 
00193    readline("Press ENTER to continue... ", junk, sizeof(junk));
00194 
00195    printf("mput          Send multiple files to remote host\n");
00196    printf("noop          Send the ftp NOOP command\n");
00197    printf("open          Open connection to remote host\n");
00198    printf("pass          Enter remote user password\n");
00199    printf("passive       Toggle passive mode\n");
00200    printf("put           Send a file to remote host\n");
00201    printf("putu          Send a file to remote host(unique)\n");
00202    printf("pwd           Display current directory on remote host\n");
00203    printf("quit          Close connection and exit\n");
00204    printf("quote         Send raw ftp command to remote host\n");
00205    printf("reget         Restart a partial file retrieve from remote host\n");
00206    printf("remotehelp    Display ftp commands implemented on remote host\n");
00207    printf("reput         Restart a partial file send to remote host\n");
00208    printf("rm            Remove file on remote host\n");
00209    printf("rmdir         Remove directory on remote host\n");
00210    printf("site          Send a site specific command\n");
00211    printf("size          Get file size information\n");
00212    printf("status        Get connection/file status information\n");
00213    printf("system        Get remote system type information\n");
00214    printf("user          Enter remote user information\n");
00215 
00216    return(0);
00217 }
00218 
00219 struct commands {
00220         char *name;
00221         _PROTOTYPE(int (*func), (void));
00222 };
00223 
00224 static struct commands commands[] = {
00225         "!",            DOlshell,
00226         "append",       DOappe,
00227         "ascii",        DOascii,
00228         "binary",       DObinary,
00229         "bin",          DObinary,
00230         "bye",          DOquit,
00231         "cd",           DOcd,
00232         "close",        DOclose,
00233         "del",          DOdelete,
00234         "dir",          DOlist,
00235         "exit",         DOquit,
00236         "get",          DOretr,
00237         "help",         DOhelp,
00238         "lcd",          DOlcd,
00239         "ldir",         DOllist,
00240         "lls",          DOlnlst,
00241         "lmkdir",       DOlmkdir,
00242         "lpwd",         DOlpwd,
00243         "lrmdir",       DOlrmdir,
00244         "ls",           DOnlst,
00245         "mget",         DOMretr,
00246         "mkdir",        DOmkdir,
00247         "mod",          DOmdtm,
00248         "mput",         DOMstor,
00249         "noop",         DOnoop,
00250         "open",         DOopen,
00251         "pass",         DOpass,
00252         "passive",      DOpassive,
00253         "put",          DOstor,
00254         "putu",         DOstou,
00255         "pwd",          DOpwd,
00256         "quit",         DOquit,
00257         "quote",        DOquote,
00258         "reget",        DOrretr,
00259         "remotehelp",   DOremotehelp,
00260         "reput",        DOrstor,
00261         "rm",           DOdelete,
00262         "rmdir",        DOrmdir,
00263         "site",         DOsite,
00264         "size",         DOsize,
00265         "status",       DOstat,
00266         "system",       DOsyst,
00267         "user",         DOuser,
00268         "",     (int (*)())0
00269 };
00270 
00271 int main(argc, argv)
00272 int argc;
00273 char *argv[];
00274 {
00275 int s;
00276 struct commands *cmd;
00277 static char buffer[128];
00278 
00279    NETinit();
00280 
00281    FTPinit();
00282 
00283    s = 0;
00284 
00285    if(argc > 1) {
00286         sprintf(buffer, "open %s ", argv[1]);
00287         makeargs(buffer);
00288         s = DOopen();
00289         if(atty && s > 0) {
00290                 sprintf(buffer, "user");
00291                 makeargs(buffer);
00292                 s = DOuser();
00293         }
00294    }
00295 
00296    while(s >= 0) {
00297         readline("ftp>", buffer, sizeof(buffer));
00298         makeargs(buffer);
00299         if(cmdargc == 0) continue;
00300         for(cmd = commands; *cmd->name != '\0'; cmd++)
00301                 if(!strcmp(cmdargv[0], cmd->name))
00302                         break;
00303         if(*cmd->name != '\0')
00304                 s = (*cmd->func)();
00305         else {
00306                 s = 0;
00307                 printf("Command \"%s\" not recognized.\n", cmdargv[0]);
00308         }
00309    }
00310 
00311    return(0);
00312 }

Generated on Fri Apr 14 22:56:53 2006 for minix by  doxygen 1.4.6