From 54c9df31e4a5c0190db1f782fa4cacf36882aa3b Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Tue, 21 Dec 2021 17:33:23 +0100 Subject: [PATCH] error message on cmd segfault. --- srcs/exec/subshell_wait.c | 20 +++++++++++++++----- tests/test_segfault.c | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 tests/test_segfault.c diff --git a/srcs/exec/subshell_wait.c b/srcs/exec/subshell_wait.c index b5ae3b0..c74f5c8 100644 --- a/srcs/exec/subshell_wait.c +++ b/srcs/exec/subshell_wait.c @@ -6,12 +6,13 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */ -/* Updated: 2021/11/27 10:43:46 by lperrey ### ########.fr */ +/* Updated: 2021/12/21 17:29:39 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +static void handle_signal_status(int wstatus); static int handle_wait_error(void); void wait_subshell(pid_t last_cmd_pid) @@ -27,10 +28,7 @@ void wait_subshell(pid_t last_cmd_pid) if (WIFEXITED(wstatus)) set_last_exit_status(WEXITSTATUS(wstatus)); if (WIFSIGNALED(wstatus)) - { - write(STDIN_FILENO, "\n", 1); - set_last_exit_status(EXIT_SIGNAL + WTERMSIG(wstatus)); - } + handle_signal_status(wstatus); } ret = 0; while (ret != -1) @@ -39,6 +37,18 @@ void wait_subshell(pid_t last_cmd_pid) if (ret == -1) ret = handle_wait_error(); } + if (get_last_exit_status() == EXIT_SIGNAL + SIGSEGV) + ft_putstr_fd("Segmentation fault (core dumped)\n", STDERR_FILENO); +} + +static void handle_signal_status(int wstatus) +{ + int signum; + + signum = WTERMSIG(wstatus); + set_last_exit_status(EXIT_SIGNAL + signum); + if (signum == SIGINT) + write(STDIN_FILENO, "\n", 1); } static int handle_wait_error(void) diff --git a/tests/test_segfault.c b/tests/test_segfault.c new file mode 100644 index 0000000..3627b86 --- /dev/null +++ b/tests/test_segfault.c @@ -0,0 +1,17 @@ + +# include + #include + +int main(int argc, char *argv[], char *envp[]) +{ + int i; + char *str; + + str = NULL; + i = 0; + while (str[i]) + { + write(1, &str[i], 1); + } + return (0); +}