#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#define LS 0
#define PWD 1
#define CD 2
#define GET 3
#define PUT 4
#define QUIT 5
#define U_LS 6
#define U_CD 7
#define U_PWD 8
#define DOFILE 9
#define RM 10
struct Msg
{
int num;
char data[1024];
char pebuf[1024];
};
char *get_set(char *msg)
{
char *a;
a = strtok(msg," ");
a = strtok(NULL," ");
return a;
}
int get_client_num(char *data)
{
if(strcmp("ls",data) == 0) return LS;
if(strcmp("pwd",data) == 0) return PWD;
if(strstr(data,"cd") != NULL) return CD;
if(strstr(data,"get") != NULL) return GET;
if(strstr(data,"put") != NULL) return PUT;
if(strcmp("quit",data) == 0) return QUIT;
if(strstr(data,"rm") != NULL) return RM;
return 666;
}
void msg_handle(struct Msg msg,int fd)
{
char file_buf[1024] = {
0};
FILE *z = NULL;
printf("client request: %s\n",msg.data);
int ret = get_client_num(msg.data);
char *set = NULL;
int a;
char *request = NULL;
switch(ret)
{
case LS:
case PWD:
msg.num = 0;
z = popen(msg.data,"r");
a = fread(msg.data,sizeof(msg.data),1,z);
if(a<0){
printf("error\n"); perror("fread");}
write(fd,&msg,sizeof(msg));
break;
case CD:
msg.num = 1;
set = get_set(msg.data);