From 5d482fc65ad276ce51c0a1705993522a95276702 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Wed, 5 Apr 2023 15:32:04 +0000 Subject: [PATCH] fix: signal --- data/data.c | 8 +++--- data/data.h | 7 +++--- env/env_fill.c | 4 +-- execution/execution.c | 10 ++++---- execution/execution_private.h | 4 ++- libftx/printf/libftprintf.a | Bin 30760 -> 0 bytes main.c | 42 ++++++++++++++++++------------- minishell.h | 3 ++- redirection/check.c | 4 +-- redirection/file.c | 14 +++++------ redirection/heredoc.c | 5 ++-- signal/signal.c | 46 +++------------------------------- signal/signal_private.h | 3 ++- syntax/syntax.c | 4 +-- tester | 1 - utils/ft_get_executable.c | 10 ++++---- 16 files changed, 68 insertions(+), 97 deletions(-) delete mode 100644 libftx/printf/libftprintf.a delete mode 160000 tester diff --git a/data/data.c b/data/data.c index f7075fe..79a9bd7 100644 --- a/data/data.c +++ b/data/data.c @@ -6,16 +6,16 @@ /* By: cchauvet exit_code); + value = ft_itoa(*data->exit_code); else if (ft_strcmp(key, "$") == 0) value = ft_strdup("PID"); else if (key[0] == '\0') diff --git a/execution/execution.c b/execution/execution.c index 81e0645..c8c5003 100644 --- a/execution/execution.c +++ b/execution/execution.c @@ -6,7 +6,7 @@ /* By: cchauvet fd_out[0], cmd->args + 1)); else { - return_code = ft_exit(cmd->args + 1, data->exit_code); + return_code = ft_exit(cmd->args + 1, *data->exit_code); if (return_code >= 0) { - data->exit_code = return_code; + *data->exit_code = return_code; return (-2); } } - data->exit_code = return_code; + *data->exit_code = return_code; return (return_code); } @@ -46,11 +46,11 @@ static bool ft_executor(t_data *data, t_cmd *cmd, char **env) if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL) return (0); cmd->pid = fork(); - ft_get_data()->child_pid = cmd->pid; if (cmd->pid == -1) return (1); if (cmd->pid == 0) { + signal(SIGQUIT, ft_quit); dup2(cmd->fd_in[0], 0); dup2(cmd->fd_out[0], 1); ft_lstiter(*data->cmds, ft_cmdcloser); diff --git a/execution/execution_private.h b/execution/execution_private.h index 5b6aab0..2633787 100644 --- a/execution/execution_private.h +++ b/execution/execution_private.h @@ -6,12 +6,14 @@ /* By: cchauvet +# include "../signal/signal.h" # include "../data/data.h" # include "../libftx/libftx.h" # include "../cmd/cmd.h" diff --git a/libftx/printf/libftprintf.a b/libftx/printf/libftprintf.a deleted file mode 100644 index 968fe3a2432cabafa799960d16ca6ec968bcc127..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 30760 zcmeHQZ)_aLbzf4F#Yj|0ISESFcD-&wn?x*%q$!bd6yqf2?6oY%N@}JI5P|`bRY;Q4FdQ>3g|;o7)e{G=pPV4>#|i)$sf`RDd4hgfDkQO&>%mshyW9X z5cU0L-rJp<+u_|(q%FyQz~0QhnKy6#?fcE!o!xu%VE1V6;L|(y`uWsooa(sw>-){E z&77lahv#|Ko>w;Oc{{)5dAn;oujaF!_eh23J=WrRdjikf_m`e`aL)6N{Eg>5f6?>4 zM)^7@>?Qswl6~d;K&G>AWOV3ECOh2O(>>Pj5o2hqZ|LMu22n|i;^R?tV7U7v&c;p+ zjY$04T4Er|6_u-#I@Dx`Za&?anm%GA`N@c6QeeqphkR8qV&Gc)AH-?bsX2=n~CSbENjSw5SZtEsbqD zXV;E5(#M`DFDs*A(^Td??L}w8^}M?KqIgP0Q{FDG()(4_Z~b3;ko)f-cWyDrWfy|0 z=~c)Fljjz_?8CF=6kmEYm`*SL1X95VlqHv53??a?&huZCxO(D>SYnkJWhRc+E0 zqk}xn

x%PR=I`3t7 z%u<23-v4vRPp9Wm)|WjS#{R*o#w`d#+HVrdd)`UwMujW15X70r|A&>pBelrga0!J-={nv z*gqUXDUJwV#<|Pl%Q#P1eCb!OVeGZjneHA6Gov~jl-OwhaCbwde>~$g^mS*ty@sB# zF|VQb?CI0}XNY&^Y^J|~uF*YXneN_Ge17swwqa!SETLfLHLpRH&_CEYFxq{(pKEY? zfluP-Dto4;hzI&N?niy5^GY1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h_rJVOdUQgc#P94K zJk>eSJv7V}k^2q6ZrEeNZgb55`yhOu;-K5d=sWv8ZW%Y-+S0MPj_nC9U=Z#-;OfMs z4HBFE7{$Ri%ny>ihioINxP|(GeJFFi@`BA$PgWD^NBJg6wIqCs5+!ctll{~t4{l@;(Ck$0RN;WVx_a-p!KhTLf%*?P9RQLHJs9Pu;%c_n%}h3+j+k6|t~Y zfA=}Ja)I88no*y>{0ZtAy%sf#(`0}}0vK1=PFUyl?tR1c5n`5ptr?tHLM88J%)(qU zpSP>F6K_5H#pz*+Oe%S}B3^AgM!h;Zl>Xxj`I=9DUr_MuE0b3}f{6 zy*ZAp7jxv9gE@ZCE&7EYryHtQS-;N>PKr4-+@LRUITG0Yx0@2|^#<}@VEGa-M}0qt z-MQj>zV^#M2A2LluRK3Oy;At0U-Q2PXb<<~xn)oOUau|MC9t~mAl(^X)t{L>kY1K7 zpW|<5sU+G9@b9xI^~9Clv5~%^?083QqWjJpflatC5n@yMb9aD0RpJy04!bVRr1wY~Us$e@on5wAl#``mO$8-6xpU@dZ_BHOpm=s^*VvAs7 zAZyzwmtXS-P!>bBeiz1+DlZtr)eSX@Jh0IuTfgg`;}2P=7)BkYe0G@-y~gr(VCTj7 z#Q8-c?^^{({|ozNil9up{fnHTO)_$5%VQ}P>lek0f1FG diff --git a/main.c b/main.c index f497d15..9324ec3 100644 --- a/main.c +++ b/main.c @@ -6,14 +6,17 @@ /* By: cchauvet static char *ft_get_user_input(void) { @@ -56,17 +59,22 @@ static void ft_cmds_waiter(t_data *data) waitpid(cmd->pid, &exit_status, 0); if (WIFSIGNALED(exit_status)) { - if (WTERMSIG(exit_status) == SIGKILL) - data->exit_code = 131; + if (exit_status == 131) + { + ft_printf("Quit (core dumped)\n"); + *data->exit_code = 131; + } else - data->exit_code = 130; + { + ft_putchar_fd('\n', 1); + *data->exit_code = 130; + } } else - data->exit_code = WEXITSTATUS(exit_status); + *data->exit_code = WEXITSTATUS(exit_status); } current = current->next; } - data->child_pid = 0; } static int ft_minishell(t_data *data, char *line) @@ -93,8 +101,7 @@ static int ft_minishell(t_data *data, char *line) int ft_init_data(t_data *data, char **env) { - data->exit_code = 0; - data->child_pid = 0; + data->exit_code = ft_get_exit_code(); data->cmds = malloc(sizeof(t_cmd *)); if (data->cmds == NULL) { @@ -114,29 +121,28 @@ int ft_init_data(t_data *data, char **env) int main(int ac, char **av, char **env) { - t_data *data; + t_data data; char *line; (void) ac; (void) av; signal(SIGINT, ft_ctrlc); - signal(SIGQUIT, ft_quit); - data = ft_get_data(); - if (ft_init_data(data, env)) + signal(SIGQUIT, SIG_IGN); + if (ft_init_data(&data, env)) return (1); line = ft_get_user_input(); while (line != NULL) { - if (ft_minishell(data, line) == 1) + if (ft_minishell(&data, line) == 1) break ; free(line); line = ft_get_user_input(); if (line == NULL) break ; } - ft_lstclear(data->cmds, ft_cmddel); - free(data->cmds); - ft_lstclear(data->env, env_del); - free(data->env); - return (data->exit_code); + ft_lstclear(data.cmds, ft_cmddel); + free(data.cmds); + ft_lstclear(data.env, env_del); + free(data.env); + return (*data.exit_code); } diff --git a/minishell.h b/minishell.h index 5b8af0f..5e6ee2c 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: cchauvet # include # include # include diff --git a/redirection/check.c b/redirection/check.c index e97db5b..a089045 100644 --- a/redirection/check.c +++ b/redirection/check.c @@ -6,7 +6,7 @@ /* By: cchauvet fd_out[0] == -2) return (0); - if (ft_file_is_writable(data, redirection)) + if (ft_file_is_appendable(data, redirection)) { fd = open(redirection, O_WRONLY | O_APPEND | O_CREAT, 0644); diff --git a/redirection/file.c b/redirection/file.c index 4a49779..d7604b4 100644 --- a/redirection/file.c +++ b/redirection/file.c @@ -6,7 +6,7 @@ /* By: cchauvet exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: No such file or directory\n", path); return (0); } readable = read(fd, "", 0); if (readable == -1) { - data->exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: Permission denied\n", path); return (0); } @@ -43,14 +43,14 @@ int ft_file_is_writable(t_data *data, const char *path) fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644); if (fd == -1) { - data->exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: Permission denied\n", path); return (0); } writeable = write(fd, "", 0); if (writeable == -1) { - data->exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: Permission denied\n", path); return (0); } @@ -66,14 +66,14 @@ int ft_file_is_appendable(t_data *data, const char *path) fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0644); if (fd == -1) { - data->exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: Permission denied\n", path); return (0); } writeable = write(fd, "", 0); if (writeable == -1) { - data->exit_code = 1; + *data->exit_code = 1; ft_eprintf("minishell: %s: Permission denied\n", path); return (0); } diff --git a/redirection/heredoc.c b/redirection/heredoc.c index cb0ca04..f1f707d 100644 --- a/redirection/heredoc.c +++ b/redirection/heredoc.c @@ -6,7 +6,7 @@ /* By: cchauvet 2) + close(*ft_get_heredoc()); *ft_get_heredoc() = -1; return (-2); } diff --git a/signal/signal.c b/signal/signal.c index 2e166ca..538fcb6 100644 --- a/signal/signal.c +++ b/signal/signal.c @@ -6,61 +6,23 @@ /* By: cchauvet exit_code = 130; - if (*ft_get_heredoc() != -1) - { - if (*ft_get_heredoc() > 2) - close(*ft_get_heredoc()); - *ft_get_heredoc() = -1; - } - else - { - if (data->child_pid > 1) - { - data->child_pid = 0; - ft_putchar_fd('\n', 1); - } - else - ft_new_line(); - } -} - void ft_quit(int num) { - t_data *data; - (void) num; - data = ft_get_data(); - data->exit_code = 131; - if (data->child_pid > 1) - { - ft_printf("Quit (core dumped)\n"); - data->child_pid = 0; - } - else - { - rl_replace_line("", 0); - rl_redisplay(); - } + ft_printf("Quit (core dumped)\n"); } diff --git a/signal/signal_private.h b/signal/signal_private.h index 1817ef0..730d47c 100644 --- a/signal/signal_private.h +++ b/signal/signal_private.h @@ -6,13 +6,14 @@ /* By: cchauvet +# include # include # include # include "../libftx/libftx.h" diff --git a/syntax/syntax.c b/syntax/syntax.c index 754d133..32d0d5e 100644 --- a/syntax/syntax.c +++ b/syntax/syntax.c @@ -6,7 +6,7 @@ /* By: cchauvet exit_code = 2; + *data->exit_code = 2; return (1); } return (0); diff --git a/tester b/tester deleted file mode 160000 index 1c6111b..0000000 --- a/tester +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 1c6111b2fd281937d38ebfa7e8d87b38baef0802 diff --git a/utils/ft_get_executable.c b/utils/ft_get_executable.c index 5a84117..4e9c8f1 100644 --- a/utils/ft_get_executable.c +++ b/utils/ft_get_executable.c @@ -6,7 +6,7 @@ /* By: cchauvet exit_code = 127; + *data->exit_code = 127; ft_eprintf("minishell: %s: No such file or directery\n", name); return (NULL); } if (access(name, X_OK) != 0) { - data->exit_code = 126; + *data->exit_code = 126; ft_eprintf("minishell: %s: permission denied\n", name); return (NULL); } @@ -46,7 +46,7 @@ static char **ft_get_paths(t_data *data, const char *name) paths = get_value_by_key("PATH", data->env); if (paths == NULL) { - data->exit_code = 127; + *data->exit_code = 127; ft_eprintf("minishell: %s: command not found\n", name); return (NULL); } @@ -97,7 +97,7 @@ static char *ft_get_executable_without_path(t_data *data, const char *name) ft_freer_tab_ultimate(1, paths); if (path == NULL) { - data->exit_code = 127; + *data->exit_code = 127; ft_eprintf("minishell: %s: command not found\n", name); } return (path);