diff --git a/src/commands.c b/src/commands.c index 13e9c330aedcb9d3c1c0979a5b22fd7844516ada..6ae328d78ca5bf0c6de2dac727311a57329f88f0 100644 --- a/src/commands.c +++ b/src/commands.c @@ -50,9 +50,46 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512]) } else if (strcmp(com->argv[0], "exit") == 0) { return 1; } else { + int back=0; + if(strcmp(com->argv[com->argc-1],"&")==0) + { + back=1; + com->argv[com->argc-1]=NULL; + (com->argc)--; + } + + pid_t pid; + pid=fork(); + char temp[100]; + char pr[5][30]={{"/usr/local/bin/"},{"/usr/bin/"},{"/bin/"},{"/usr/sbin "},{"/sbin/"}}; + + if(pid<0) + { + fprintf(stderr,"fork failed!"); + return -1; + } + else if(pid==0) + { + if(execv(com->argv[0],com->argv)==-1){ + for(int i=0;i<5;i++) + { + strcpy(temp,pr[i]); + strcat(temp,com->argv[0]); + execv(temp,com->argv); + } + } + fprintf(stderr, "%s: command not found\n", com->argv[0]); - return -1; + exit(0); } + else{ + if(back==1){} + else{ + wait(NULL); + return 0; + } +} +} } return 0; @@ -73,4 +110,5 @@ 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..a0d37b1946c5c57ba04ed9aad713ecd029db82d8 100644 --- a/src/main.c +++ b/src/main.c @@ -1,16 +1,20 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> - +#include <signal.h> #include "commands.h" #include "built_in.h" #include "utils.h" +#include "signal_handlers.h" int main() { char buf[8096]; + signal(SIGINT,(void*)catch_sigint); + signal(SIGTSTP,(void*)catch_sigtstp); while (1) { + buf[0]=0; fgets(buf, 8096, stdin); struct single_command commands[512]; diff --git a/src/signal_handlers.c b/src/signal_handlers.c index 4b6fe2e073f327917964b5327b6649f74bcbda1e..82220968e96148a05b264b258e4fb34b168ada50 100644 --- a/src/signal_handlers.c +++ b/src/signal_handlers.c @@ -3,9 +3,11 @@ void catch_sigint(int signalNo) { // TODO: File this! + signal(SIGINT,SIG_IGN); } void catch_sigtstp(int signalNo) { // TODO: File this! + signal(SIGTSTP,SIG_IGN); }