From 75036dc3da46a2d09d3ec33a3ffe3a5c5b121a3f Mon Sep 17 00:00:00 2001 From: printf <1239977258@qq.com> Date: Thu, 22 Jun 2023 03:41:27 +0000 Subject: [PATCH] update client/worker.c. --- client/worker.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/client/worker.c b/client/worker.c index 09c2269..af7244a 100644 --- a/client/worker.c +++ b/client/worker.c @@ -10,3 +10,118 @@ enum command{ PUTS, TOKEN, }; + +int handleCmd(transShell_t transShell, int netfd){ + char shell[] = "cd dir1/dir2/dir3"; + char *cmd = strtok(shell," "); + char *path = strtok(NULL," "); + char *extra = strtok(NULL," "); + char pwd[1024] = {0}; + int datalength; + while(1){ + if(extra != NULL){ + printf("%s:too many argument!\n",cmd); + break; + } + else if(strlen(cmd) > 5 || strlen(cmd) < 2){ + printf("Invalid command! command:%s\n",cmd); + break; + } + else{ + if(strcmp(cmd,"cd")==0){ + transShell.cmd = CD; + transShell.length = strlen(shell); + strcpy(transShell.path,path); + send(netfd,&transShell,sizeof(transShell.cmd)+sizeof(transShell.path)+sizeof(transShell.length)+transShell.length,0); + int check = 0; + recvn(netfd, &check, sizeof(int)); + if (check != 0) + { + printf("not such dir!\n"); + } + recvn(netfd, &datalength, sizeof(int)); + bzero(pwd, sizeof(pwd)); + recvn(netfd, pwd, datalength); + } + else if(strcmp(cmd,"ls")==0){ + transShell.cmd = LS; + transShell.length = strlen(shell); + strcpy(transShell.path,path); + send(netfd,&transShell,sizeof(transShell.cmd)+sizeof(transShell.path)+sizeof(transShell.length)+transShell.length,0); + int check = 0; + recvn(netfd, &check, sizeof(int)); + if (check != 0) + { + printf("not such dir!\n"); + } + + char fileName[100] = {0}; + while(1){ + recvn(netfd, &datalength, sizeof(datalength)); + if(datalength == 0){ + break; + } + bzero(fileName, sizeof(fileName)); + recvn(netfd, fileName, datalength); + printf("%-8s ", fileName); + } + printf("\n"); + } + else if(strcmp(cmd,"puts")==0){ + transShell.cmd = PUTS; + transShell.length = strlen(shell); + strcpy(transShell.path,path); + send(netfd,&transShell,sizeof(transShell.cmd)+sizeof(transShell.path)+sizeof(transShell.length)+transShell.length,0); + } + else if(strcmp(cmd,"gets")==0){ + transShell.cmd = GETS; + transShell.length = strlen(shell); + strcpy(transShell.path,path); + send(netfd,&transShell,sizeof(transShell.cmd)+sizeof(transShell.path)+sizeof(transShell.length)+transShell.length,0); + int check = 0; + recvn(netfd, &check, sizeof(int)); + } + else if(strcmp(cmd,"rm")==0){ + transShell.cmd = RM; + transShell.length = strlen(shell); + strcpy(transShell.path,path); + send(netfd,&transShell,sizeof(transShell.cmd)+sizeof(transShell.path)+sizeof(transShell.length)+transShell.length,0); + int check = 0; + recvn(netfd, &check, sizeof(int)); + if (check != 0){ + printf("not such dir!\n"); + } + } + else if(strcmp(cmd,"pwd")==0){ + transShell.cmd = PWD; + transShell.length = strlen(shell); + strcpy(transShell.path,path); + send(netfd,&transShell,sizeof(transShell.cmd)+sizeof(transShell.path)+sizeof(transShell.length)+transShell.length,0); + int check = 0; + recvn(netfd, &check, sizeof(int)); + if (check != 0){ + printf("not such dir!\n"); + } + + bzero(pwd, sizeof(pwd)); + recvn(netfd, &datalength, sizeof(int)); + recvn(netfd, pwd, datalength); + printf("%s\n", pwd); + } + else if(strcmp(cmd,"mkdir")==0){ + transShell.cmd = MKDIR; + transShell.length = strlen(shell); + strcpy(transShell.path,path); + send(netfd,&transShell,sizeof(transShell.cmd)+sizeof(transShell.path)+sizeof(transShell.length)+transShell.length,0); + int check = 0; + recvn(netfd, &check, sizeof(int)); + if (check != 0){ + printf("mkdir failed!\n"); + } + } + else{ + printf("Invalid command!\n"); + } + } + } +} -- Gitee