From 0d7b2f84b240d73ae8f0baafb0a87570b24f02d7 Mon Sep 17 00:00:00 2001 From: RaonSol <windows0903@gmail.com> Date: Wed, 9 May 2018 21:42:56 +0900 Subject: [PATCH] done --- src/built_in.c | 13 ++++++++++--- src/commands.c | 41 +++++++++++++++++++++++++++++++++++++++-- src/main.c | 1 + src/signal_handlers.c | 3 +++ src/utils.c | 7 ++----- 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/built_in.c b/src/built_in.c index 9a466e9..3ca2125 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 13e9c33..aff83a2 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 77c7804..1c3dee1 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 4b6fe2e..677ed71 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 bbe6ed0..0a3a5d9 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*)); -- GitLab