diff --git a/src/built_in.c b/src/built_in.c index 9a466e91501d5877107fb67e308275c4847bcb38..2d42e4c9e8c92f48b96f013bafc6dc006770ff07 100644 --- a/src/built_in.c +++ b/src/built_in.c @@ -8,6 +8,8 @@ #include "built_in.h" +extern int bg; +extern char bg_command[100]; int do_cd(int argc, char** argv) { if (!validate_cd_argv(argc, argv)) return -1; @@ -37,7 +39,10 @@ int do_fg(int argc, char** argv) { return -1; // TODO: Fill this. - +if(bg==0) +printf("no job"); +else +printf("%d running %s",bg,bg_command); return 0; } diff --git a/src/commands.c b/src/commands.c index 13e9c330aedcb9d3c1c0979a5b22fd7844516ada..84167b55093244cfff1f953ff2931fb2815759f7 100644 --- a/src/commands.c +++ b/src/commands.c @@ -2,9 +2,18 @@ #include <stdlib.h> #include <string.h> #include <assert.h> - +#include<unistd.h> #include "commands.h" #include "built_in.h" +#include<sys/wait.h> +#include<sys/socket.h> +#include <pthread.h> +#include <fcntl.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <sys/stat.h> +int bg=0; +char bg_command[100]; static struct built_in_command built_in_commands[] = { { "cd", do_cd, validate_cd_argv }, @@ -24,13 +33,73 @@ static int is_built_in_command(const char* command_name) return -1; // Not found } +void * client(void *command) +{ + +char **com = (char **)command; + +int client_sock; +pid_t pid; +FILE *fp_in; +char *pathname="./filetest"; + + + +struct sockaddr_un clientaddr; + +int fd; + + + + +client_sock = socket(AF_UNIX,SOCK_STREAM,0);//make socket + + if(client_sock == -1) + { + printf("socket Error!"); + exit(1); + + } + memset(&clientaddr,0, sizeof(clientaddr)); + clientaddr.sun_family = AF_UNIX; + strcpy(clientaddr.sun_path, pathname); + + if(connect(client_sock, (struct sockaddr *)&clientaddr, sizeof(clientaddr))<0) + { + printf("Connect Error!: "); + exit(0); + } + + + +if((pid = fork())==0) +{ + dup2(STDOUT_FILENO,fd); + dup2(client_sock, STDOUT_FILENO); + execv(com[0],com); + exit(0); +} +else +{ +wait(NULL); +dup2(fd, STDOUT_FILENO); +close(fd); + +} +close(client_sock); + +pthread_exit(0); + +} /* * Description: Currently this function only handles single built_in commands. You should modify this structure to launch process and offer pipeline functionality. */ int evaluate_command(int n_commands, struct single_command (*commands)[512]) { - if (n_commands > 0) { + + if (n_commands > 0) +{//command struct single_command* com = (*commands); assert(com->argc != 0); @@ -49,23 +118,156 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512]) return 0; } else if (strcmp(com->argv[0], "exit") == 0) { return 1; - } else { - fprintf(stderr, "%s: command not found\n", com->argv[0]); - return -1; - } - } + } + else{ + if(n_commands==2) + { + pthread_t newthread; + int server_sock, client_sock; + int state, client_len; + pid_t pid, ppid; + int status; + FILE *fp; + char **commandline; + int fd= dup(STDIN_FILENO); + commandline = com->argv ; + + + + pthread_create(&newthread, NULL, client, commandline); + + struct sockaddr_un clientaddr, serveraddr; + + + char *Path = "./filetest"; + + state = 0; + + if (access(Path, F_OK) == 0) + { + unlink(Path); + } + + client_len = sizeof(clientaddr); + server_sock = socket(AF_UNIX, SOCK_STREAM,0); + + if(server_sock==-1) + { + perror("socket erro:"); + exit(0); + } + + + memset(&serveraddr,0x00, sizeof(serveraddr)); + serveraddr.sun_family = AF_UNIX; + strcpy(serveraddr.sun_path, Path); + + state = bind(server_sock, (struct sockaddr *)&serveraddr,sizeof(serveraddr)); + + if(state ==-1) + { + perror("bind Error!:"); + exit(0); + } + + state = listen(server_sock,2); + if(state == -1) + { + perror("listen Error!:"); + exit(0); + } + + client_sock = accept(server_sock, (struct sockaddr *)&clientaddr,&client_len); + pthread_join(newthread,NULL); + pid = fork(); + if(pid == 0) + { + if(client_sock ==-1) + { + perror("accept Error:"); + exit(0); + } + + + dup2(STDIN_FILENO, fd); + dup2(client_sock, STDIN_FILENO); + + execv((com+1)->argv[0], (com+1)->argv); + fprintf(stderr,"%s : command not found\n", (com+1)->argv[0]); + + + + + exit(0); + + + } + + else + { + wait(NULL); + dup2(fd, STDIN_FILENO); + close(fd); + close(client_sock); + } + } + else{ + + pid_t pid; + char path[5][30]={{"/usr/loca/bin/"},{"/usr/bin/"},{"/bin/"},{"/usr/sbin/"},{"/sbin/"}}; + char tmp[200]; + pid=fork(); + if(pid==0)//child process + { + if(strcmp(com->argv[(com->argc)-1],"&")==0)//backGround + { + + com->argv[com->argc-1]=NULL; + (com->argc)--; + printf("%d\n",getpid()); + bg=getpid(); + strcpy(bg_command,com->argv[0]); + } + + else if(execv(com->argv[0],com->argv)==-1)//path + { + for(int i=0;i<5;i++) + { + strcpy(tmp,path[i]); + strcat(tmp,com->argv[0]); + execv(tmp,com->argv); + } + } + else + { + execv(com->argv[0],com->argv);// + fprintf(stderr, "%s: command not found\n", com->argv[0]); + exit(-1); + } + + + } + else + { + wait(NULL); + } + } + } + }//command return 0; } void free_commands(int n_commands, struct single_command (*commands)[512]) { - for (int i = 0; i < n_commands; ++i) { + for (int i = 0; i < n_commands; ++i) +{ struct single_command *com = (*commands) + i; int argc = com->argc; char** argv = com->argv; - for (int j = 0; j < argc; ++j) { + for (int j = 0; j < argc; ++j) + { free(argv[j]); } @@ -74,3 +276,4 @@ void free_commands(int n_commands, struct single_command (*commands)[512]) memset((*commands), 0, sizeof(struct single_command) * n_commands); } + diff --git a/src/main.c b/src/main.c index 77c78049273e013e278ad9021fa088531b636a97..f87b38a9547b77e556ff1c76d37763eadc53a37d 100644 --- a/src/main.c +++ b/src/main.c @@ -5,11 +5,14 @@ #include "commands.h" #include "built_in.h" #include "utils.h" - +#include "signal_handlers.h" +#include<signal.h> int main() { char buf[8096]; + signal(SIGINT,(void*)catch_sigint); + signal(SIGTSTP,(void*)catch_sigtstp); while (1) { fgets(buf, 8096, stdin); diff --git a/src/signal_handlers.c b/src/signal_handlers.c index 4b6fe2e073f327917964b5327b6649f74bcbda1e..524f6cdb62521e58997bd5a1e3bbe8df20ccc4a6 100644 --- a/src/signal_handlers.c +++ b/src/signal_handlers.c @@ -1,11 +1,20 @@ #include "signal_handlers.h" +#include<stdio.h> +#include<signal.h> void catch_sigint(int signalNo) { - // TODO: File this! + // TODO: File this + +signal(SIGINT,catch_sigint); + } void catch_sigtstp(int signalNo) { // TODO: File this! + +signal(SIGTSTP,catch_sigtstp); } + +