📄 commands.c
字号:
/* dftp: FTP client example * Copyright (c) 1996 by Donald C. Asonye * Email: donald@uh.edu * * commands.c. * * This and other files are hereby released to the public for * educational purposes. You may redistribute this and other files * or routines as long as you are not compensated for it, and as * long as this notice is included with it. You may not use the * released source codes in anyway commercial without permission. * * It'd be nice if you share your modifications with me and everyone * else. I encourage you to make the code better as I am very busy. * * Share your knowledge :) */#include <stdio.h>#include <stdlib.h>#include <string.h>#include "commands.h"#include "extfuncs.h"char szBuffer[1025]={0}; /* buffer used to read/write */char szUser[20]; /* stores username */char szPass[256]; /* stores user password */extern int Connected; /* flag for connect status *//* * CheckCommand * this is the main command parser. it scans user input * and returns the correct command flag. it's fairly * straight forward. just a bunch of strcmp & strncmp */int CheckCommand(char *command){ if( !strcmp(command,"pwd") ) return PWD; if( !strncmp(command,"rhelp",5) ) return RHELP; if( !strncmp(command,"help",4) ) return HELP; if( !strncmp(command,"ls ",3) || !strcmp(command,"ls") || !strcmp(command,"dir")) return LS; if( !strcmp(command,"cd") || !strncmp(command,"cd ",3) ) return CD; if( !strcmp(command,"close")) return CLOSE; if( !strcmp(command,"quit") || !strcmp(command,"bye") || !strcmp(command,"exit") ) return QUIT; if( !strcmp(command,"open") || !strcmp(command,"o") || !strncmp(command,"open ",5) || !strncmp(command,"o ",2) ) return OPEN; if( !strcmp(command,"lls") || !strcmp(command,"ldir") ) return LLS; if( !strcmp(command,"lcd") || !strncmp(command, "lcd ",4) ) return LCD; if( !strcmp(command,"user") || !strncmp(command,"user ",5)) return USER; if( !strcmp(command,"bin") || !strcmp(command,"binary") ) return BINARY; if( !strcmp(command,"as") || !strcmp(command,"ascii") ) return ASCII; if( !strncmp(command,"!",1) ) return SHELL; if( !strncmp(command,"get ", 4) ) return GET; if( !strncmp(command,"put ", 4) ) return PUT; return -1; /* unsupported command */}/* * DoOpen * this function is called when the o,open command is issued. * it connects to the requested host, and logs the user in * */void DoOpen( char *command){ char *szHost=NULL; /* remote host */ /* * do not do anything if we are already connected. */ if( Connected ) { printf("Already connected. Close connection first.\n"); fflush(stdout); return; } /* * extract the remote host from command line (if given ). * make a copy of the host name with strdup. */ if(!strcmp(command,"open") || !strcmp(command,"o")) { printf("Host:"); fgets(szBuffer,1024,stdin); (void)strtok(szBuffer,"\n"); szHost = (char *)strdup(szBuffer);; } else if( !strncmp(command,"open ",5)) szHost = strdup(&command[5]); else if( !strncmp(command,"o ",2) ) szHost = strdup(&command[2]); else szHost = strdup(command); printf("Connecting to %s\n",szHost); hControlSocket = ConnectToServer(szHost,"21");#if (defined(WIN32) || defined(_WIN32) ) Sleep(1);#else sleep(1);#endif if( hControlSocket > 0) { printf("Connected to %s\n",szHost);#if (defined(WIN32) || defined(_WIN32) ) sprintf(command,"dftp: Connected to %s ", szHost); SetConsoleTitle(command); /* set console window title */#endif Connected = 1; /* we ar now connected */ GetReply(); /* get reply (welcome message) from server */ DoLogin((char *)NULL); /* prompt for username and password */ DoBinary(); /* default binary mode */ } free(szHost); /* free the strdupped string */}/* * DoLogin * this function logs the user into the remote host. * prompts for username and password */void DoLogin( char *command){ char *User=NULL; if( command && *command) User=&command[5]; if( Connected ) { /* * ignore leading whitespace */ while(User && (*User == ' ' || *User == '\t') && *User) User++; /* * if user name was not provided via command line, read it in. */ if(!User || !(*User) ) { printf("Login:"); fgets(szUser,20,stdin); User = szUser; (void)strtok(szUser,"\n"); /* remove '\n' */ } /* * send user name & password to server & get reply message */ sprintf(szBuffer,"USER %s\r\n",User); SendControlMsg(szBuffer,strlen(szBuffer)); GetReply(); GetPassword( szPass ); sprintf(szBuffer,"PASS %s\r\n",szPass); SendControlMsg(szBuffer,strlen(szBuffer)); GetReply(); } else printf("Not Connected.\n");}/* * DoClose * closes connection to the ftp server */void DoClose( void ){ if( !Connected ) { printf("Not Connected.\n"); } else { SendControlMsg("quit\r\n",6); GetReply(); CloseControlConnection(); hControlSocket = -1;#if (defined(WIN32) || defined(_WIN32) ) SetConsoleTitle("dftp: Connection closed");#endif Connected = 0; }}/* * DoList * perform directory listing i.e: ls */void DoList( char *command){ if( !Connected ) { printf("Not Connected.\n"); return; } /* * obtain a listening socket */ if( GetListenSocket() < 0) { printf("Cannot obtain a listen socket.\n"); return; } /* * parse command */ if( !strcmp(command,"ls") ) { sprintf(szBuffer,"NLST\r\n"); } else if( !strcmp(command,"dir") ) sprintf(szBuffer,"LIST\r\n"); else if( !strncmp(command, "ls ",3)) { while( *command == ' ') command++; sprintf(szBuffer,"LIST %s\r\n",&command[3]); } /* * send command to server and get response */ SendControlMsg(szBuffer,strlen(szBuffer)); memset(szBuffer,0,1024); GetReply(); /* * accept server's connection */ if(AcceptConnection() < 0) { printf("Cannot accept connection.\n"); return; } CloseListenSocket(); /* close listening socket */ /* * display directory listing. */ while( ReadDataMsg(szBuffer,1024) > 0) { fflush(stdout); printf(szBuffer); memset(szBuffer,0,1024); } /* * read response */ (void)GetReply();}/* * DoCD * chang to another directory on the remote system */void DoCD( char *command){ char *dir=&command[2]; if( !Connected ) { printf("Not Connected.\n"); return; } /* * ignore leading whitespace */ while( *dir && (*dir == ' ' || *dir == '\t') ) dir++; /* * if dir is not specified, read it in */ if( ! (*dir) ) { printf("Remote directory:"); fgets(szBuffer,1024,stdin); (void)strtok(szBuffer,"\n"); dir = (char *)strdup(szBuffer); while( *dir && (*dir) == ' ') dir++; if( !(*dir) ) { printf("Usage: cd remote-directory\n"); return; } } /* * send command to server and read response */ sprintf(szBuffer, "CWD %s\r\n",dir); SendControlMsg(szBuffer,strlen(szBuffer)); (void)GetReply();}/* * DoLCD * change directory on the local system */void DoLCD( char *command){ char *dir = &command[3]; while(*dir && (*dir == ' ' || *dir == '\t') ) dir++; /* * if dir is not specified, then print the current dir */ if( ! *dir ) { dir = getcwd((char *)NULL,256); if( !dir) perror("getcwd"); else printf("Current directory is: %s\n",dir); } else { if( chdir(dir) < 0) perror("chdir"); else { dir = getcwd((char *)NULL,256); if( !dir) perror("getcwd"); else printf("Current directory is: %s\n",dir); } }}/* * DoLLS * perform local directory listing. winqvt implements this, but * this is not supported by many systems. it's not really needed * since we already have '!' command. you can just do !ls or !dir. */void DoLLS( char *command ){#if ( !defined(_WIN32) || !defined(WIN32) ) system("ls");#else system("dir");#endif}/* * function to pass commands to the system, ie: dir. * this function gets called when '!' is encountered */void DoShellCommand( char *command ){ command++; /* ignore '!' */#if ( !defined(_WIN32) || !defined(WIN32) ) system(command);#else if( !command || !*command) system("cmd"); /* have to fix this for win95 */ else /* maybe i should use 'start' */ system(command); /* programatically determine which */ #endif /* system we are on, then make the */ /* appropriate call */}/* * DoBinary * set file transfer mode to binary */void DoBinary(){ if( !Connected ) { printf("Not Connected.\n"); return; } sprintf(szBuffer, "TYPE I\r\n"); SendControlMsg(szBuffer,strlen(szBuffer)); GetReply(); printf("File transfer modes set to binary.\n"); bMode = BINARY;}/* * DoAscii * set file transfer mode to ascii text */void DoAscii(){ if( !Connected ) { printf("Not Connected.\n"); return; } sprintf(szBuffer, "TYPE A\r\n"); SendControlMsg(szBuffer,strlen(szBuffer)); GetReply(); printf("File transfer modes set to ascii.\n"); bMode = ASCII;}/* * DoGet * retrieve a file from the remote host. calls GetFile(..) */void DoGet( char *command){ if( !Connected ) { printf("Not Connected.\n"); return; } (void)strtok(command," "); GetFile(strtok((char *)NULL, " "));}/* * DoPut * send a file to the remote host. calls PutFile(..) */void DoPut( char *command ){ if( !Connected ) { printf("Not Connected.\n"); return; } (void)strtok(command," "); PutFile(strtok((char *)NULL, " "));}/* * DoRhelp * sends a help command to the server. */void DoRhelp( char *command ){ char *szCommand; if( !Connected ) { printf("Not Connected.\n"); return; } (void)strtok(command," "); szCommand=strtok((char *)NULL, " "); if( szCommand && *szCommand ) sprintf(szBuffer,"HELP %s\r\n",szCommand); else sprintf(szBuffer, "HELP\r\n"); SendControlMsg(szBuffer, strlen(szBuffer)); GetReply();}/* * retrieves the current directory on the remote host */void DoPWD(){ if( !Connected ) { printf("Not Connected.\n"); return; } sprintf(szBuffer, "PWD\r\n"); SendControlMsg(szBuffer,strlen(szBuffer)); GetReply();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -