📄 explaincmd.c
字号:
if (st.st_mode & S_IROTH)
mode[7] = 'r';
if (st.st_mode & S_IWOTH)
mode[8] = 'w';
if (st.st_mode & S_IXOTH)
mode[9] = 'x';
mode[10] = '\0';
off += snprintf(buf + off, len - off, "%s ", mode);
off += snprintf(buf + off, len - off, "%d ", 1);
if ((pwd_stu = getpwuid(st.st_uid)) == NULL)
{
closedir(dir);
return -1;
}
off += snprintf(buf + off,len - off, "%s ",pwd_stu->pw_name);
if ((grp=getgrgid(st.st_gid)) == NULL)
{
closedir(dir);
return -1;
}
off += snprintf(buf + off, len - off, "%s ", grp->gr_name);
off += snprintf(buf + off, len - off, "%*d ", 10, st.st_size);
ptm = localtime(&st.st_mtime);
if (ptm && (timelen = strftime(timebuf, sizeof(timebuf), "%b %d %H:%S", ptm)) > 0)
{
timebuf[timelen] = '\0';
off += snprintf(buf + off, len - off, "%s ", timebuf);
}
else
{
closedir(dir);
return -1;
}
off += snprintf(buf + off, len - off, "%s\r\n", filename);
if(off>=7800)
{
Send(sockfd,buf,strlen(buf),0);
off=0;
memset(buf,0,strlen(buf));
}
}
Send(sockfd,buf,strlen(buf),0);
return off;
}
void DoPasv(int sockfd)
{
struct sockaddr_in addr;
struct in_addr addr_i;
char ip_tmp[32]="";
int port_tmp;
char tmp[150]="";
int len=0;
int i=0;
data_pasv_sfd=CreateSocket(AF_INET, SOCK_STREAM, 0);
len=sizeof(addr);
getsockname(data_pasv_sfd,(struct sockaddr *)&addr,&len);
addr.sin_port=0;
Bind(data_pasv_sfd,&addr,sizeof(addr));
Listen(data_pasv_sfd,10);
get_my_address(&addr_i);
strcpy(ip_tmp,inet_ntoa(addr_i));
for(i=0;i<strlen(ip_tmp);i++)
{
if(ip_tmp[i]=='.')
ip_tmp[i]=',';
}
len=sizeof(addr);
getsockname(data_pasv_sfd,(struct sockaddr *)&addr,&len);
port_tmp = ntohs(addr.sin_port);
sprintf(tmp,"227 Entering Passive Mode (%s,%d,%d)\r\n",ip_tmp,port_tmp/256,port_tmp%256);
Write(sockfd,tmp,strlen(tmp));
tran_type=2;
}
int get_my_address (struct in_addr *addr)
{
struct ifreq req;
int sock;
sock = socket(AF_INET, SOCK_DGRAM, 0);
strncpy (req.ifr_name, "eth0", IFNAMSIZ);
if ( ioctl(sock, SIOCGIFADDR, &req) < 0 )
{
return 0;
}
memcpy (addr, &((struct sockaddr_in *) &req.ifr_addr)->sin_addr, sizeof (struct in_addr));
return 1;
}
int ChangeDir(int sockfd,char *dir)
{
int flag=0;
char cur_path[100]="";
if(dir[0]=='/')
{
//if(isany!=1)
flag=chdir(dir);
/*else
{
getcwd(cur_path,sizeof(cur_path));
strcat(cur_path,dir);
flag=chdir(cur_path);
}*/
}
else
{
getcwd(cur_path,sizeof(cur_path));
strcat(cur_path,"/");
strcat(cur_path,dir);
flag=chdir(cur_path);
}
return flag;
}
int CDUP(int sockfd)
{
char cur_path[100]="";
int flag=0;
getcwd(cur_path,sizeof(cur_path));
if(isany==1)
{
if(strcmp(cur_path,"/var/ftp")==0)
{
flag=chdir(cur_path);
return flag;
}
}
int i=0;
i=strlen(cur_path)-1;
for(;cur_path[i]!='/';i--);
cur_path[i]='\0';
if(strlen(cur_path)==0)
{
cur_path[i]='/';
cur_path[i+1]='\0';
}
flag=chdir(cur_path);
return flag;
}
void ChangeFileType(int sockfd,char *lastcmd)
{
if(strcmp(lastcmd,"I")==0)
{
Write(sockfd,"200 Switching to Binary mode.\r\n",strlen("200 Switching to Binary mode.\r\n"));
file_type=1;
}
else if(strcmp(lastcmd,"A")==0)
{
Write(sockfd,"200 Switching to ASCII mode.\r\n",strlen("200 Switching to ASCII mode.\r\n"));
file_type=2;
}
return;
}
void SendFilePasv(int sockfd,char *lastcmd)
{
int file_fd;
char file_path[100]="";
char cur_path[100]="";
char tmp[100]="";
struct stat buf;
char filebuf[BUFSIZ];
int readsize=0;
FILE *fp;
int pasvfd;
pasvfd=Accept(data_pasv_sfd,(struct sockaddr *)NULL,(socklen_t *)NULL);
if(lastcmd[0]=='/')
{
strcpy(file_path,lastcmd);
}
else
{
getcwd(cur_path,sizeof(cur_path));
strcpy(file_path,cur_path);
strcat(file_path,"/");
strcat(file_path,lastcmd);
}
if(file_type==1)
{
file_fd=open(file_path,O_RDONLY);
if(file_fd==-1)
{
Write(sockfd,"550 Failed to open file.\r\n",strlen("550 Failed to open file.\r\n"));
return;
}
Getfilesize(file_path,&buf);
sprintf(tmp,"150 Opening BINARY mode data connection for %s (%d bytes).\r\n",file_path,buf.st_size);
Write(sockfd,tmp,strlen(tmp));
lseek(file_fd,fileseek,SEEK_CUR);
while((readsize=read(file_fd,filebuf,sizeof(filebuf)))>0)
{
Send(pasvfd,filebuf,readsize,0);
memset(filebuf,0,sizeof(filebuf));
}
}
else if(file_type==2)
{
fp=fopen(file_path,"r");
if(fp==NULL)
{
Write(sockfd,"550 Failed to open file.\r\n",strlen("550 Failed to open file.\r\n"));
return;
}
Getfilesize(file_path,&buf);
sprintf(tmp,"150 Opening ASCII mode data connection for %s (%d bytes).\r\n",file_path,buf.st_size);
Write(sockfd,tmp,strlen(tmp));
fseek(fp,(long int)fileseek,SEEK_CUR);
while((readsize=fread(filebuf,sizeof(char),sizeof(filebuf),fp))>0)
{
Send(pasvfd,filebuf,readsize,0);
memset(filebuf,0,sizeof(filebuf));
}
}
Write(sockfd,"226 File send OK.\r\n",strlen("226 File send OK.\r\n"));
close(file_fd);
Close(pasvfd);
Close(data_pasv_sfd);
close(fp);
}
void SendFilePort(int sockfd,char *lastcmd)
{
int file_fd;
char file_path[100]="";
char cur_path[100]="";
char tmp[100]="";
struct stat buf_t;
char filebuf[BUFSIZ];
int readsize=0;
struct sockaddr_in addr;
FILE *fp;
data_port_sfd=CreateSocket(AF_INET, SOCK_STREAM, 0);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
addr.sin_port=htons(20);
Bind(data_port_sfd,&addr,sizeof(addr));
ConnectPort(port,ip_str);
if(lastcmd[0]=='/')
{
strcpy(file_path,lastcmd);
}
else
{
getcwd(cur_path,sizeof(cur_path));
strcpy(file_path,cur_path);
strcat(file_path,"/");
strcat(file_path,lastcmd);
}
if(file_type==1)
{
file_fd=open(file_path,O_RDONLY);
if(file_fd==-1)
{
Write(sockfd,"550 Failed to open file.\r\n",strlen("550 Failed to open file.\r\n"));
return;
}
Getfilesize(file_path,&buf_t);
sprintf(tmp,"150 Opening BINARY mode data connection for %s (%d bytes).\r\n",file_path,buf_t.st_size);
Write(sockfd,tmp,strlen(tmp));
lseek(file_fd,fileseek,SEEK_CUR);
while((readsize=read(file_fd,filebuf,sizeof(filebuf)))>0)
{
Send(data_port_sfd,filebuf,readsize,0);
memset(filebuf,0,sizeof(filebuf));
}
}
else if(file_type==2)
{
fp=fopen(file_path,"r");
if(fp==NULL)
{
Write(sockfd,"550 Failed to open file.\r\n",strlen("550 Failed to open file.\r\n"));
return;
}
Getfilesize(file_path,&buf_t);
sprintf(tmp,"150 Opening ASCII mode data connection for %s (%d bytes).\r\n",file_path,buf_t.st_size);
Write(sockfd,tmp,strlen(tmp));
fseek(fp,(long int)fileseek,SEEK_CUR);
while((readsize=fread(filebuf,sizeof(char),sizeof(filebuf),fp))>0)
{
Send(data_port_sfd,filebuf,readsize,0);
memset(filebuf,0,sizeof(filebuf));
}
}
Write(sockfd,"226 File send OK.\r\n",strlen("226 File send OK.\r\n"));
close(file_fd);
Close(data_port_sfd);
close(fp);
}
void RecvFilePasv(int sockfd,char *lastcmd)
{
int file_fd;
//char file_path[100]="";
//char cur_path[100]="";
char tmp[100]="";
char filebuf[BUFSIZ];
int writesize=0;
//FILE *fp;
int pasvfd;
pasvfd=Accept(data_pasv_sfd,(struct sockaddr *)NULL,(socklen_t *)NULL);
//puts(lastcmd);
if(file_type==1)
{
if(fileseek==0)
file_fd=open(lastcmd,O_WRONLY | O_CREAT| O_TRUNC,0755);
else
file_fd=open(lastcmd,O_WRONLY | O_APPEND,0755);
if(file_fd==-1)
{
Write(sockfd,"553 Could not create file.\r\n",strlen("553 Could not create file.\r\n"));
return;
}
Write(sockfd,"150 Ok to send data.\r\n",strlen("150 Ok to send data.\r\n"));
lseek(file_fd,fileseek,SEEK_CUR);
while((writesize=Recv(pasvfd,filebuf,sizeof(filebuf),0))>0)
{
write(file_fd,filebuf,writesize);
memset(filebuf,0,sizeof(filebuf));
}
Write(sockfd,"226 File receive OK.\r\n",strlen("226 File receive OK.\r\n"));
}
else if(file_type==2)
{
if(fileseek==0)
file_fd=open(lastcmd,O_WRONLY | O_CREAT| O_TRUNC,0755);
else
file_fd=open(lastcmd,O_WRONLY | O_APPEND,0755);
if(file_fd==-1)
{
Write(sockfd,"553 Could not create file.\r\n",strlen("553 Could not create file.\r\n"));
return;
}
Write(sockfd,"150 Ok to send data.\r\n",strlen("150 Ok to send data.\r\n"));
lseek(file_fd,fileseek,SEEK_CUR);
while((writesize=Recv(pasvfd,filebuf,sizeof(filebuf),0))>0)
{
write(file_fd,filebuf,writesize);
memset(filebuf,0,sizeof(filebuf));
}
Write(sockfd,"226 File receive OK.\r\n",strlen("226 File receive OK.\r\n"));
}
close(file_fd);
Close(pasvfd);
Close(data_pasv_sfd);
//close(fp);
}
void RecvFilePort(int sockfd,char *lastcmd)
{
int file_fd;
char tmp[100]="";
char filebuf[BUFSIZ];
int writesize=0;
struct sockaddr_in addr;
data_port_sfd=CreateSocket(AF_INET, SOCK_STREAM, 0);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
addr.sin_port=htons(20);
Bind(data_port_sfd,&addr,sizeof(addr));
ConnectPort(port,ip_str);
if(file_type==1)
{
if(fileseek==0)
file_fd=open(lastcmd,O_WRONLY | O_CREAT| O_TRUNC,0755);
else
file_fd=open(lastcmd,O_WRONLY | O_APPEND,0755);
if(file_fd==-1)
{
Write(sockfd,"553 Could not create file.\r\n",strlen("553 Could not create file.\r\n"));
return;
}
Write(sockfd,"150 Ok to send data.\r\n",strlen("150 Ok to send data.\r\n"));
lseek(file_fd,fileseek,SEEK_CUR);
while((writesize=Recv(data_port_sfd,filebuf,sizeof(filebuf),0))>0)
{
write(file_fd,filebuf,writesize);
memset(filebuf,0,sizeof(filebuf));
}
Write(sockfd,"226 File receive OK.\r\n",strlen("226 File receive OK.\r\n"));
}
else if(file_type==2)
{
if(fileseek==0)
file_fd=open(lastcmd,O_WRONLY | O_CREAT | O_TRUNC,0755);
else
file_fd=open(lastcmd,O_WRONLY | O_APPEND,0755);
if(file_fd==-1)
{
Write(sockfd,"553 Could not create file.\r\n",strlen("553 Could not create file.\r\n"));
return;
}
Write(sockfd,"150 Ok to send data.\r\n",strlen("150 Ok to send data.\r\n"));
lseek(file_fd,fileseek,SEEK_CUR);
while((writesize=Recv(data_port_sfd,filebuf,sizeof(filebuf),0))>0)
{
write(file_fd,filebuf,writesize);
memset(filebuf,0,sizeof(filebuf));
}
Write(sockfd,"226 File receive OK.\r\n",strlen("226 File receive OK.\r\n"));
}
close(file_fd);
Close(data_port_sfd);
}
int Getfilesize(char * filename,struct stat *buf)
{
return stat(filename,buf);
}
void Mkdir(int sockfd,char *lastcmd)
{
char file_path[100]="";
char cur_path[100]="";
char tmp[100]="";
int flag=0;
if(lastcmd[0]=='/')
{
strcpy(file_path,lastcmd);
}
else
{
getcwd(cur_path,sizeof(cur_path));
strcpy(file_path,cur_path);
strcat(file_path,"/");
strcat(file_path,lastcmd);
}
flag=mkdir(file_path,0755);
if(flag==-1)
Write(sockfd,"550 Create directory operation failed.\r\n",strlen("550 Create directory operation failed.\r\n"));
else
{
sprintf(tmp,"257 \"%s\" created\r\n",file_path);
Write(sockfd,tmp,strlen(tmp));
}
}
void DeleteFile(int sockfd,char *lastcmd)
{
char file_path[100]="";
char cur_path[100]="";
int flag=0;
if(lastcmd[0]=='/')
{
strcpy(file_path,lastcmd);
}
else
{
getcwd(cur_path,sizeof(cur_path));
strcpy(file_path,cur_path);
strcat(file_path,"/");
strcat(file_path,lastcmd);
}
flag=unlink(file_path);
if(flag==-1)
Write(sockfd,"550 Permission denied.\r\n",strlen("550 Permission denied.\r\n"));
else
Write(sockfd,"250 Delete operation successful.\r\n",strlen("250 Delete operation successful.\r\n"));
}
void Rmdir(int sockfd,char *lastcmd)
{
char file_path[100]="";
char cur_path[100]="";
int flag=0;
if(lastcmd[0]=='/')
{
strcpy(file_path,lastcmd);
}
else
{
getcwd(cur_path,sizeof(cur_path));
strcpy(file_path,cur_path);
strcat(file_path,"/");
strcat(file_path,lastcmd);
}
flag=rmdir(file_path);
if(flag==-1)
{
Write(sockfd,"550 Remove directory operation failed.\r\n",strlen("550 Remove directory operation failed.\r\n"));
}
else
{
Write(sockfd,"250 Remove directory operation successful.\r\n",strlen("250 Remove directory operation successful.\r\n"));
}
}
void Storfilename(char *filename,char *lastcmd)
{
char file_path[100]="";
char cur_path[100]="";
if(lastcmd[0]=='/')
{
strcpy(file_path,lastcmd);
}
else
{
getcwd(cur_path,sizeof(cur_path));
strcpy(file_path,cur_path);
strcat(file_path,"/");
strcat(file_path,lastcmd);
}
strcpy(filename,file_path);
}
void Renamefile(int sockfd,char *oldname,char *newname)
{
int flag=0;
flag=rename(oldname,newname);
if(flag==-1)
Write(sockfd,"550 Rename failed.\r\n",strlen("550 Rename failed.\r\n"));
else
Write(sockfd,"250 Rename successful.\r\n",strlen("250 Rename successful.\r\n"));
return;
}
void Dorest(int sockfd,char *lastcmd)
{
fileseek=atol(lastcmd);
char tmp[100]="";
sprintf(tmp,"350 Restart position accepted (%ld).\r\n",fileseek);
Write(sockfd,tmp,strlen(tmp));
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -