diff --git a/src/built_in.c b/src/built_in.c index 9a466e91501d5877107fb67e308275c4847bcb38..3ca21259e2a55ee660b9255ae85e5d74dbc5bcf5 100644 --- a/src/built_in.c +++ b/src/built_in.c @@ -8,6 +8,8 @@ #include "built_in.h" +int PID=1; + int do_cd(int argc, char** argv) { if (!validate_cd_argv(argc, argv)) return -1; @@ -35,9 +37,14 @@ int do_pwd(int argc, char** argv) { int do_fg(int argc, char** argv) { if (!validate_fg_argv(argc, argv)) return -1; - - // TODO: Fill this. - + + int stat; + if(PID!=1) { + printf("%d running %s\n",PID,running_command); + waitpid(PID,&stat,0); + PID=1; + } + return 0; } diff --git a/src/commands.c b/src/commands.c index 13e9c330aedcb9d3c1c0979a5b22fd7844516ada..aff83a24b5e468012db0f3f9eb5129f510923a75 100644 --- a/src/commands.c +++ b/src/commands.c @@ -2,6 +2,9 @@ #include <stdlib.h> #include <string.h> #include <assert.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> #include "commands.h" #include "built_in.h" @@ -28,6 +31,23 @@ static int is_built_in_command(const char* command_name) /* * Description: Currently this function only handles single built_in commands. You should modify this structure to launch process and offer pipeline functionality. */ + +void run_command(struct single_command *com) +{ + execv(com->argv[0], com->argv); + char PATH[50]; + char* tok, ptrtmp=NULL; + sprintf(PATH,"%s",getenv("PATH")); + tok=strtok_r(PATH,":",&ptrtmp); + while(tok){ + char PATH_tmp[50]; + char* tok_tmp=NULL; + sprintf(PATH_tmp,"%s/%s",tok,com->argv[0]); + execv(PATH_tmp,com->argv); + tok=strtok_r(NULL,":",&ptrtmp); + } +} + int evaluate_command(int n_commands, struct single_command (*commands)[512]) { if (n_commands > 0) { @@ -49,14 +69,31 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512]) return 0; } else if (strcmp(com->argv[0], "exit") == 0) { return 1; + } else if(n_commands==1) { + int pid=fork(), stat=0; + if(pid>0) + { + wait(&stat); + return stat ? -1 : 0; + } + else if(!pid) + { + run_command(com); + fprintf(stderr, "%s: command not found\n", com->argv[0]); + exit(1); + } + else if(pid<0) + { + fprintf(stderr, "fork error\n",com->argv[0]); + exit(1); + } } else { - fprintf(stderr, "%s: command not found\n", com->argv[0]); return -1; } } return 0; -} + } void free_commands(int n_commands, struct single_command (*commands)[512]) { diff --git a/src/main.c b/src/main.c index 77c78049273e013e278ad9021fa088531b636a97..1c3dee14e1d93737816060d266d2465e6ece746e 100644 --- a/src/main.c +++ b/src/main.c @@ -10,6 +10,7 @@ int main() { char buf[8096]; + putenv("PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"); while (1) { fgets(buf, 8096, stdin); diff --git a/src/signal_handlers.c b/src/signal_handlers.c index 4b6fe2e073f327917964b5327b6649f74bcbda1e..677ed715fc06f94fed01e3d36f3be28af3eb7ae7 100644 --- a/src/signal_handlers.c +++ b/src/signal_handlers.c @@ -1,3 +1,6 @@ +#include <stdlib.h> +#include <unistd.h> + #include "signal_handlers.h" void catch_sigint(int signalNo) diff --git a/src/utils.c b/src/utils.c index bbe6ed098ec9a4b7e0548d84f5c3f49260d5a8a7..0a3a5d97a9ffe209ee5bfb339a0a0287edefbb2b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -4,9 +4,7 @@ #include <stdlib.h> #include <string.h> -void mysh_parse_command(const char* command, - int* n_commands, - struct single_command (*commands)[]) +void mysh_parse_command(const char* command, int* n_commands, struct single_command (*commands)[]) { char buf[4096]; strcpy(buf, command); @@ -28,8 +26,7 @@ void mysh_parse_command(const char* command, *n_commands = ti; } -void parse_single_command(const char* command, - int *argc, char*** argv) +void parse_single_command(const char* command, int *argc, char*** argv) { const int kMaxArgc = 512; *argv = (char**)malloc(kMaxArgc * sizeof(char*));