diff --git a/src/commands.c b/src/commands.c index 13e9c330aedcb9d3c1c0979a5b22fd7844516ada..aa2276649f71ed2f3e77598a517473758c70eb4f 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 4b6fe2e073f327917964b5327b6649f74bcbda1e..8bf3c836b8b4415bdac85a5053eadd0d64715a63 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); + } }