diff --git a/src/builtins/builtin_cd.c b/src/builtins/builtin_cd.c index d2b70e9..3585c55 100644 --- a/src/builtins/builtin_cd.c +++ b/src/builtins/builtin_cd.c @@ -59,7 +59,7 @@ static int change_dir(t_shell *shell, char *path) { write(2, "minishell: cd: ", 15); perror(shell->cmds->cmd_args[1]); - return (1); + return (free(cwd), 1); } if (set_env(envp, cwd, "OLDPWD=")) return (1); diff --git a/src/executor/process.c b/src/executor/process.c index 8f3d698..605573a 100644 --- a/src/executor/process.c +++ b/src/executor/process.c @@ -25,19 +25,30 @@ void childs_exit(t_shell *shell, int i, int *fd, int code) exit(code); } +static bool get_caught_pid(pid_t *pid, int *status, int i, pid_t last_pid) +{ + pid_t caugth_pid; + + caugth_pid = waitpid(pid[i], status, 0); + if (caugth_pid == last_pid) + return (true); + return (false); +} + void wait_pid(t_shell *shell, pid_t *pid, int nb_cmd) { int status; int i; - pid_t caugth_pid; pid_t last_pid; + bool is_child_sigint; + status = 0; last_pid = pid[nb_cmd - 1]; i = -1; + is_child_sigint = false; while (++i < nb_cmd) { - caugth_pid = waitpid(pid[i], &status, 0); - if (caugth_pid == last_pid) + if (get_caught_pid(pid, &status, i, last_pid)) { if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT) write(2, "Quit (core dumped)\n", 19); @@ -46,7 +57,11 @@ void wait_pid(t_shell *shell, pid_t *pid, int nb_cmd) else shell->exit_status = WEXITSTATUS(status); } + if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT) + is_child_sigint = true; } + if (is_child_sigint) + write(2, "\n", 1); } static void run_child(t_shell *shell, int i, int *fd, t_cmd *current)