From 6a4cd934f61b459bd9ae3fd99c1176f9e05ac525 Mon Sep 17 00:00:00 2001 From: "Znet12345678@yahoo.com" Date: Wed, 8 Apr 2015 17:37:29 -0500 Subject: [PATCH] Added Shell like environment --- src/server.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/server.c b/src/server.c index 053d60f..04b9457 100755 --- a/src/server.c +++ b/src/server.c @@ -79,6 +79,45 @@ int server(char *args){ } */ } + else if(strncmp(dir,"sh",2) == 0){ + while(1){ + send(client_socket[i],"&",2,0); + char *sh_buff = malloc(1024); + recv(client_socket[i],sh_buff,sizeof(sh_buff),0); + if(strncmp(sh_buff,"echo",4) == 0){ + memmove(sh_buff,sh_buff + strlen("echo "),1+strlen(sh_buff + strlen("echo "))); + send(client_socket[i],sh_buff,strlen(sh_buff),0); + } + else if(strncmp(sh_buff,"exit",4) == 0){ + break; + } + else if(strncmp(sh_buff,"write",5) == 0){ + char *path = "/home/znet/"; + send(client_socket[i],"*",2,0); + char *recv_buff = malloc(1024); + recv(client_socket[i],recv_buff,sizeof(recv_buff),0); + strcpy(path,recv_buff); + FILE *fw; + if((fw = fopen(path,"a")) < 0){ + send(client_socket[i],"Error Opening File\n",strlen("Error Opening File\n"),0); + } + else{ + while(1){ + send(client_socket[i],">",strlen(">"),0); + char *w_buff = malloc(255); + if(strncmp(w_buff,"!EOF",4) == 0){ + break; + } + fprintf(fw,"%s",w_buff); + } + } + } + else{ + char *msg = "Unknown command\n"; + send(client_socket[i],msg,strlen(msg),0); + } + } + } else{ dir[strlen(dir) - 1] = '\0'; DIR *d;