command.c

来自「一个用于银行视频联播网中客户端广告机的网络通信部分」· C语言 代码 · 共 321 行

C
321
字号

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/errno.h>
#include <fcntl.h>

#include "command.h"
#include "enums.h"
#include "scon.h"
#include "keyio.h"

int ReadCommand = 0;
int bMode=ASCII;
char szBuffer[1025]={0}; 
char szUser[20]; 
char szPass[256]; 
extern int Connected; 
extern int hControlSocket;

extern int hListenSocket;

extern int hDataSocket;

char *strdupl(const char *str);
void strrev(char *str);
char *my_strrchr(const char *str,char ch);
/*select download mode ,include "-g":singla file download,"-rg":restart point download*/
int CheckOption(char *Option)
{
  if(!strncmp(Option,"-g",2)||!strcmp(Option,"-g"))
  	{
  	printf("file should be downloaded!\n");
  	return GET;
  	}
  if(!strncmp(Option,"-rg",3)||strcmp(Option,"-rg"))
  	{
	 return RGET;
  	}
  return (-1);
}
/*open control connect ,and login ftp server with username and passwd*/
void DoOpen( char *command,char *Username,char *PassWd)
{
char *szHost=NULL; 

if( Connected ) {
printf("Already connected. Close connection first.\n");
fflush(stdout);
return;
}
if(!strcmp(command,"open") || !strcmp(command,"o")) {

printf("Host:"); fgets(szBuffer,1024,stdin);
(void)strtok(szBuffer,"\n");
szHost = (char *)strdupl(szBuffer);;
}
else if( !strncmp(command,"open ",5))
szHost = strdupl(&command[5]);
else if( !strncmp(command,"o ",2) )
szHost = strdupl(&command[2]);
else
szHost = strdupl(command);


printf("Connecting to %s\n",szHost);
hControlSocket = ConnectToServer(szHost,"21");


if( hControlSocket > 0) {
printf("Connected to %s\n",szHost);


Connected = 1; 
GetReply(); 
/*login ftp server with username and passwd */
DoLogin((char *)NULL,Username,PassWd); 
/*set transfer mode to binary*/
DoBinary(); 
}
free(szHost); 
}


void DoLogin( char *command,char *username,char *Passwd)
{
char *User=NULL;
int len;
len=strlen(username);
//if(len>20)
//	break;
if( command && *command)
User=&command[5];

if( Connected ) {
while(User && (*User == ' ' || *User == '\t') && *User)
User++;

if(!User || !(*User) ) {
printf("Login:"); 
//fgets(szUser,20,stdin);
strcpy(szUser,username);
User = szUser;
(void)strtok(szUser,"\n"); 
}


sprintf(szBuffer,"USER %s\r\n",User);
SendControlMsg(szBuffer,strlen(szBuffer));
GetReply();
GetPassword( szPass,Passwd );

sprintf(szBuffer,"PASS %s\r\n",szPass);
SendControlMsg(szBuffer,strlen(szBuffer));
GetReply();
}
else
printf("Not Connected.\n");
}

/*close connection*/
void DoClose( void )
{
if( !Connected ) {
printf("Not Connected.\n");

}
else {
SendControlMsg("quit\r\n",6);
GetReply();
CloseControlConnection();
hControlSocket = -1;


Connected = 0;
}

}
/*cd into a directory*/
void DoCD( char *command)
{
char *dir=&command[2];

if( !Connected ) {
printf("Not Connected.\n");
return;
}
while( *dir && (*dir == ' ' || *dir == '\t') ) 
dir++;
if( ! (*dir) ) {
printf("Remote directory:");
fgets(szBuffer,1024,stdin);
(void)strtok(szBuffer,"\n");
dir = (char *)strdupl(szBuffer);
while( *dir && (*dir) == ' ') 
dir++;
if( !(*dir) ) {
printf("Usage: cd remote-directory\n");
return;
}
}
sprintf(szBuffer, "CWD %s\r\n",dir);
SendControlMsg(szBuffer,strlen(szBuffer));
(void)GetReply();
}

/*lcd localfile directory*/
void LCDFileDir(char *filename)
{
  char Cmd[128]="lcd ";
  strcat(Cmd,filename);
  DoLCD(Cmd);
 memset(Cmd,0,sizeof(Cmd));
}
/*lcd localfile directory*/
void LCDFileDir1(char *filename1)
{
   char Cmd[128]="lcd ";
   char *pl=NULL;
	strcat(Cmd,filename1);
  printf("%s\n",Cmd);
	pl=my_strrchr(Cmd,'/');
DoLCD(pl);
}
void DoLCD( char *command)
{
char *dir = &command[3];
printf("DoLCD command!\n");
while(*dir && (*dir == ' ' || *dir == '\t') ) 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 {
	printf("%s\n",dir);
}
}
}
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;
}


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;

}

void DoGet( char *rfilename)
{
if( !Connected ) {
printf("Not Connected.\n");
return;
}

char *p=NULL,*pl=NULL;
char Cmd[128]="cd ";
strcat(Cmd,rfilename);
p=my_strrchr(Cmd, '/');
DoCD(p);
pl=(strrchr(Cmd,'/')+1);
GetFile(pl);
return;
}

void DoRget(char *command,char *rfilename,char *lfilename)
{
  printf(" check connect status!\n");
 if(!Connected){
 	printf("Not Connected.\n");
       return;
   }
 char *p=NULL,*pl=NULL;
 char *q=NULL,*ql=NULL;
 char Cmd[128]="cd ";
 strcat(Cmd,rfilename);
 p=my_strrchr(Cmd,'/');
 DoCD(p);
 pl=(strrchr(Cmd,'/')+1);
 Rgetfile(pl,lfilename);
return ;
}



char *strdupl( const char *str)

{

char *szTemp;
szTemp = (char *)malloc(strlen(str));

if( !szTemp )

return (char *)NULL;
strcpy(szTemp,str);

return szTemp;

}
void strrev(char *str)
{
 int i;
 char ctemp;
 int len;
 len=(strlen(str)-1);
 for(i=0;i<len-i;i++)
 	{
 	ctemp=str[i];
	str[i]=str[len-i];
	str[len-1]=ctemp;
 	}
     str[len+1]=0;
	 return ;
}
/*return the front string from ch chractor */
char *my_strrchr(const char * str, char ch)
{
  int i,len;
  while(*str==(char*)NULL)
  	return (char*)NULL;
  len=strlen(str)-1;
  while(str[len--]!=ch)
  	;
  char *p=malloc(len+1);
  for(i=0;i<len+1&&&str!=NULL;i++)
  	p[i]=*str++;
  p[len+1]='\0';
  return p;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?