📄 cmd.c
字号:
get_reply(client_sockfd);}//-------------------------------------------------------------------------------------------------void cmd_rmdir(){ if(isconnect==0) { printf("please connect to a ftp server first.\n"); return ; } if(isconnect==1 && islogin==0) { printf("please login first.\n"); return ; } int num=cmd_format(cmd_buf); int i=0; char* para=cmd_buf; if(num==1) { printf("(directory name): "); fgets(cmd_buf,CMD_BUFFER_SIZE, stdin); num=cmd_format(cmd_buf); if(num==0) { printf("usage: rmdir directory-name)"); return; } para=cmd_buf; } else //将para指向第一个参数起始处 { for(i=0;cmd_buf[i]!=' ';i++) ; para=cmd_buf+i+1; } for(i=0;para[i]!=' ' && para[i]!='\0';i++) ; para[i]='\0'; //printf("para:%s\n",para); send_cmd("RMD ",para, client_sockfd); get_reply(client_sockfd);}//-------------------------------------------------------------------------------------------------void cmd_delete(){ if(isconnect==0) { printf("please connect to a ftp server first.\n"); return ; } if(isconnect==1 && islogin==0) { printf("please login first.\n"); return ; } int num=cmd_format(cmd_buf); int i=0; char* para=cmd_buf; if(num==1) { printf("(remote-file): "); fgets(cmd_buf,CMD_BUFFER_SIZE, stdin); num=cmd_format(cmd_buf); if(num==0) { printf("usage: rmdir remote-file)"); return; } para=cmd_buf; } else //将para指向第一个参数起始处 { for(i=0;cmd_buf[i]!=' ';i++) ; para=cmd_buf+i+1; } for(i=0;para[i]!=' ' && para[i]!='\0';i++) ; para[i]='\0'; //printf("para:%s\n",para); send_cmd("DELE ",para, client_sockfd); get_reply(client_sockfd);}//-------------------------------------------------------------------------------------------------void cmd_mirror() // mirror [remote [local]]{ if(isconnect==0) { printf("please connect to a ftp server first.\n"); return ; } if(isconnect==1 && islogin==0) { printf("please login first.\n"); return ; } //1。根据参数在本地创建文件夹,然后发送CWD /hoss到对方 //PASV //LIST 获取对方本文件夹下的目录,然后逐个检查,如果是文件夹就递归调用本程序,否则就发送GET //退出 int i=0, begin, end; int num=cmd_format(cmd_buf); if(num>=2) //传送当前文件夹下的所有文件到客户端的当前文件夹下 { char lfold_name[LEN_OF_FILENAME], rfold_name[LEN_OF_FILENAME]; int lfold, rfold; for(i=0;cmd_buf[i]!=' ';i++) ; begin=i+1; for(i=i+1;cmd_buf[i]!=' ' && cmd_buf[i]!='\0';i++) ; cmd_buf[i]='\0'; strcpy(rfold_name, cmd_buf+begin); strcpy(lfold_name,rfold_name); if(num>=3) { begin=i+1; for(i=i;cmd_buf[i]!=' ' && cmd_buf[i]!='\0';i++) ; cmd_buf[i]='\0'; strcpy(lfold_name,cmd_buf+begin); } mode_t mode; int lfile=mkdir(lfold_name, mode); //在本地创建文件夹 if(lfile<0) { printf("create local fold error!\n"); return ; } if(chdir(lfold_name)<0) //进入文件夹 { printf("Enter %s error!\n",lfold_name); return ; } send_cmd("CWD ",rfold_name, client_sockfd); get_reply(client_sockfd); } //建立数据连接 发送LIST 获取当前文件夹的内容 int client_data_sockfd=create_datasock(); if(client_data_sockfd<0) { printf("ls executed failed: create data socket failed.\n"); return; } send_cmd("LIST ", NULL, client_sockfd); if(get_reply(client_sockfd)!=150) //说明连接不成功 { close(client_data_sockfd); return; } char name[8]="ls_file"; int ls_file_fd=0; ls_file_fd=open(name, O_CREAT|O_RDWR); if(ls_file_fd<0) { printf("create ls_file failed!\n"); return; } get_data(client_data_sockfd, ls_file_fd); get_reply(client_sockfd); //transfer complete close(ls_file_fd); //重新打开文件,读出每一行,分析。。。 char list_buf[512]; FILE *fd=fopen(name, "rb"); while(1) { fgets(list_buf, 512, fd); printf("%s\n",list_buf); if(list_buf[0]=='\0') break; else { //获取文件夹或者文件名字 int k=0; int j=0; while(1) { while(list_buf[k]!=' ') k++; if(k>=1 && list_buf[k-1]!=' ') j++; if(j==8) break; else k++; } while(list_buf[k]==' ') k++; char subfold_name[LEN_OF_FILENAME]; strcpy(subfold_name, list_buf+k); subfold_name[strlen(subfold_name)-2]='\n'; //for(i=0;subfold_name[i]!='\n'&& subfold_name[i]!='\0' ;i++) ; //subfold_name[i]='\0'; printf("subfold_name:%s",subfold_name); if(list_buf[0]=='d') //下载文件夹 { if(strncmp(subfold_name,"..",2)==0 || strncmp(subfold_name, ".", 1)==0 ) continue; strcpy(cmd_buf, "mirror "); strcat(cmd_buf, subfold_name); cmd_mirror(); } else if(list_buf[0]=='-') //下载文件 { strcpy(cmd_buf, "get "); strcat(cmd_buf, subfold_name); cmd_get(); } else { //出错,可能是一行的字符数大于LEN_OF_FILENAME break; } list_buf[0]='\0'; } //end of else } //end of while fclose(fd); unlink("ls_file"); if(num>=2) { send_cmd("CWD ..", NULL, client_sockfd); get_reply(client_sockfd); chdir(".."); }}//-------------------------------------------------------------------------------------------------void cmd_uopen(){ if(isconnect==1) //说明已经有一个连接了 { printf("You have connect to %s, use close first.\n", server_name); return; } else { int num=cmd_format(cmd_buf); if(num==1) { printf("Usage: open host-name [port]\n"); return; } if(num>=2) { int i=0; char *name_begin; char *port_begin; for(i=0;cmd_buf[i]!=' ';i++) ; name_begin=cmd_buf+i+1; if(num==2) { strcpy(server_name,name_begin); cmd_open(server_name, "21"); } else if (num==3) { for(i=i+1;cmd_buf[i]!=' '; i++) ; cmd_buf[i]='\0'; strcpy(server_name, name_begin); port_begin=cmd_buf+i+1; strcpy(server_port, port_begin); cmd_open(server_name, server_port); } else { printf("Usage: open host-name [port]\n"); return; } } //end if(num>=2) } // end else return;}//-------------------------------------------------------------------------------------------------void cmd_close(){ if(isconnect==0) { printf("please connect to a ftp server first.\n"); return ; } if(isconnect==1 && islogin==0) { printf("please login first.\n"); return ; } send_cmd("QUIT ", NULL, client_sockfd); get_reply(client_sockfd); isconnect=0; islogin=0; close(client_sockfd);}//-------------------------------------------------------------------------------------------------void cmd_mget(){ if(isconnect==0) { printf("please connect to a ftp server first.\n"); return ; } if(isconnect==1 && islogin==0) { printf("please login first.\n"); return ; } int num=cmd_format(cmd_buf); if(num<=1) { printf("usage: mget remotefile1 remotefile2 ...\n"); return ; } char *begin; char *end; //char file_list[CMD_BUFFER_SIZE]; //strcpy(file_list, cmd_buf); //char *point=file_list; char *point = cmd_buf; begin=strchr(point,' '); begin=begin+1; point=begin; while(1) { end = strchr(point, ' '); if(!end) { cmd_buf[0]='\0'; strcat(cmd_buf, "get "); strcat(cmd_buf, begin); strcat(cmd_buf, "\n"); cmd_get(); return ; } end[0]='\0'; cmd_buf[0]='\0'; strcat(cmd_buf, "get "); strcat(cmd_buf, begin); strcat(cmd_buf, "\n"); cmd_get(); begin = end+1; point=begin; }}//-------------------------------------------------------------------------------------------------void cmd_mput(){ if(isconnect==0) { printf("please connect to a ftp server first.\n"); return ; } if(isconnect==1 && islogin==0) { printf("please login first.\n"); return ; } int num=cmd_format(cmd_buf); if(num<=1) { printf("usage: mput localfile1 localfile2 ...\n"); return ; } char *begin; char *end; //char file_list[CMD_BUFFER_SIZE]; //strcpy(file_list, cmd_buf); //char *point=file_list; char *point = cmd_buf; begin=strchr(point,' '); begin=begin+1; point=begin; while(1) { end = strchr(point, ' '); if(!end) { cmd_buf[0]='\0'; strcat(cmd_buf, "put "); strcat(cmd_buf, begin); strcat(cmd_buf, "\n"); cmd_put(); return ; } end[0]='\0'; cmd_buf[0]='\0'; strcat(cmd_buf, "put "); strcat(cmd_buf, begin); strcat(cmd_buf, "\n"); cmd_put(); begin = end+1; point=begin; }}//-------------------------------------------------------------------------------------------------void cmd_lpwd(){ char temp[512]; int size=sizeof(temp); if(getcwd(temp,size)!=NULL) printf("local directory now: %s\n",temp); else printf("Can't get local dir\n");}//-------------------------------------------------------------------------------------------------void cmd_lmkdir(){ int num=cmd_format(cmd_buf); if(num!=2) { printf("usage: lmkdir dir_name.\n"); return ; } char *begin=strchr(cmd_buf,' '); mode_t mode; if(mkdir(begin+1, mode)<0) { perror("mode"); printf("lmkdir error!\n"); return ; }}//-------------------------------------------------------------------------------------------------void cmd_lrmdir(){ int num=cmd_format(cmd_buf); if(num!=2) { printf("usage: lrmdir dir_name.\n"); return ; } char *begin=strchr(cmd_buf,' '); struct stat st; if(lstat(begin+1, &st)<0 || remove(begin+1)<0) { perror("dele"); return ; }}//-------------------------------------------------------------------------------------------------void cmd_ldir(){ char buf[BUFSIZ]; DIR *dir; struct dirent *ent; int off=0; if((dir=opendir("."))<0) { perror("opendir()"); return ; } buf[0]='\0'; while((ent = readdir(dir)) != NULL) { char *filename = ent->d_name; struct stat st; char mode[] = "----------"; struct passwd *pwd; struct group *grp; struct tm *ptm; char timebuf[BUFSIZ]; int timelen; if(strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0) continue; if(stat(filename, &st)<0) { perror("stat()"); closedir(dir); return ; } if(S_ISDIR(st.st_mode)) mode[0] = 'd'; if(st.st_mode & S_IRUSR) mode[1] = 'r'; if(st.st_mode & S_IWUSR) mode[2] = 'w'; if(st.st_mode & S_IXUSR) mode[3] = 'x'; if(st.st_mode & S_IRGRP) mode[4] = 'r'; if(st.st_mode & S_IWGRP) mode[5] = 'w'; if(st.st_mode & S_IXGRP) mode[6] = 'x'; 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, sizeof(buf)-off, "%s ", mode); off += snprintf(buf+off, sizeof(buf)-off, "%d ", 1); //用户 if((pwd = getpwuid(st.st_uid)) == NULL) { perror("getpwuid"); closedir(dir); return ; } off += snprintf(buf+off, sizeof(buf)-off, "%s ", pwd->pw_name); //组 if((grp = getgrgid(st.st_gid)) == NULL) { perror("getgrgid"); closedir(dir); return ; } off += snprintf(buf+off, sizeof(buf)-off, "%s ", grp->gr_name); //大小 off += snprintf(buf+off, sizeof(buf)-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, sizeof(buf)-off, "%s ", timebuf); } else { perror("strftime"); closedir(dir); return ; } off += snprintf(buf+off, sizeof(buf)-off, "%s\r\n", filename); } //end of while if(off>=0) printf("%s",buf); closedir(dir); return ;}//-------------------------------------------------------------------------------------------------void cmd_pwd(){ if(isconnect==0) { printf("please connect to a ftp server first.\n"); return ; } if(isconnect==1 && islogin==0) { printf("please login first.\n"); return ; } send_cmd("PWD ", NULL, client_sockfd); get_reply(client_sockfd);}//-------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -