From 3dd7461b5bbbc73fbc6e07628aeb2708d62feebe Mon Sep 17 00:00:00 2001 From: LimBeomJune <juna0624@naver.com> Date: Wed, 9 May 2018 23:38:46 +0900 Subject: [PATCH] #1 --- src/commands.c | 22 ++++++++++++++++++++-- src/signal_handlers.c | 11 +++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/commands.c b/src/commands.c index 13e9c33..aa22766 100644 --- a/src/commands.c +++ b/src/commands.c @@ -2,9 +2,14 @@ #include <stdlib.h> #include <string.h> #include <assert.h> +#include <sys/wait.h> +#include <unistd.h> +#include <sys/types.h> +#include <signal.h> #include "commands.h" #include "built_in.h" +#include "signal_handlers.h" static struct built_in_command built_in_commands[] = { { "cd", do_cd, validate_cd_argv }, @@ -30,6 +35,9 @@ static int is_built_in_command(const char* command_name) */ int evaluate_command(int n_commands, struct single_command (*commands)[512]) { + signal(SIGINT, catch_sigint); + signal(SIGTSTP, catch_sigtstp); + if (n_commands > 0) { struct single_command* com = (*commands); @@ -50,8 +58,18 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512]) } else if (strcmp(com->argv[0], "exit") == 0) { return 1; } else { - fprintf(stderr, "%s: command not found\n", com->argv[0]); - return -1; + pid_t pid; + int status; + + if ((pid = fork()) < 0) { + return 0; + } else if (pid == 0) { + if (execv(com->argv[0], com->argv) < 0) { + return 1; + } + } else { + while (wait(&status) != pid); + } } } diff --git a/src/signal_handlers.c b/src/signal_handlers.c index 4b6fe2e..8bf3c83 100644 --- a/src/signal_handlers.c +++ b/src/signal_handlers.c @@ -1,11 +1,22 @@ +#include <stdio.h> +#include <signal.h> + #include "signal_handlers.h" void catch_sigint(int signalNo) { // TODO: File this! + if (signalNo == SIGINT) { + fflush(stdin); + fflush(stdout); + } } void catch_sigtstp(int signalNo) { // TODO: File this! + if (signalNo == SIGTSTP) { + fflush(stdin); + fflush(stdout); + } } -- GitLab