fix: signal

This commit is contained in:
Camille Chauvet 2023-04-05 15:32:04 +00:00
parent e0fb7f1fd0
commit 5d482fc65a
16 changed files with 68 additions and 97 deletions

View File

@ -6,16 +6,16 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:42:09 by cchauvet #+# #+# */
/* Updated: 2023/03/27 13:42:58 by cchauvet ### ########.fr */
/* Updated: 2023/04/05 14:48:30 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
#include "./data_private.h"
#include "data.h"
t_data *ft_get_data(void)
int *ft_get_exit_code(void)
{
static t_data data;
static int exit_code;
return (&data);
return (&exit_code);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:43:39 by cchauvet #+# #+# */
/* Updated: 2023/03/27 13:43:40 by cchauvet ### ########.fr */
/* Updated: 2023/04/05 14:45:23 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,10 +18,9 @@ typedef struct s_data
{
t_list **env;
t_list **cmds;
int exit_code;
int child_pid;
int *exit_code;
} t_data;
t_data *ft_get_data(void);
int *ft_get_exit_code(void);
#endif

4
env/env_fill.c vendored
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
/* Updated: 2023/03/29 19:06:44 by cchauvet ### ########.fr */
/* Updated: 2023/04/05 15:09:46 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,7 +40,7 @@ static char *ft_getvalue(t_data *data, char *key)
char *value;
if (ft_strcmp(key, "?") == 0)
value = ft_itoa(data->exit_code);
value = ft_itoa(*data->exit_code);
else if (ft_strcmp(key, "$") == 0)
value = ft_strdup("PID");
else if (key[0] == '\0')

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
/* Updated: 2023/04/05 12:30:09 by alouis-j ### ########.fr */
/* Updated: 2023/04/05 15:13:33 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,14 +30,14 @@ static int ft_execute_own_cmd(t_data *data, t_cmd *cmd)
return_code = (echo(cmd->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);

View File

@ -6,12 +6,14 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:45:53 by cchauvet #+# #+# */
/* Updated: 2023/03/29 18:55:22 by cchauvet ### ########.fr */
/* Updated: 2023/04/05 15:15:56 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef EXECUTION_PRIVATE_H
# define EXECUTION_PRIVATE_H
# include <signal.h>
# include "../signal/signal.h"
# include "../data/data.h"
# include "../libftx/libftx.h"
# include "../cmd/cmd.h"

Binary file not shown.

42
main.c
View File

@ -6,14 +6,17 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/03/30 13:12:41 by cchauvet ### ########.fr */
/* Updated: 2023/04/05 15:31:19 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
#include "data/data.h"
#include "env/env.h"
#include "libftx/libft/libft.h"
#include "libftx/libftx.h"
#include "minishell.h"
#include "signal/signal.h"
#include <stdlib.h>
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);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
/* Updated: 2023/03/28 16:03:54 by cchauvet ### ########.fr */
/* Updated: 2023/04/05 14:46:56 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,6 +24,7 @@
# include "./utils/utils.h"
# include "./data/data.h"
# include "./signal/signal.h"
# include <stdio.h>
# include <readline/readline.h>
# include <readline/history.h>
# include <signal.h>

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */
/* Updated: 2023/04/05 14:12:02 by alouis-j ### ########.fr */
/* Updated: 2023/04/05 14:53:18 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
@ -98,7 +98,7 @@ static bool ft_check_outfile_append(t_data *data, t_cmd *cmd,
{
if (cmd->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);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:36:11 by cchauvet #+# #+# */
/* Updated: 2023/03/30 13:26:31 by cchauvet ### ########.fr */
/* Updated: 2023/04/05 15:12:41 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,14 +20,14 @@ int ft_file_is_readable(t_data *data, const char *path)
fd = open(path, O_RDONLY);
if (fd == -1)
{
data->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);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
/* Updated: 2023/04/05 14:07:10 by alouis-j ### ########.fr */
/* Updated: 2023/04/05 14:58:01 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
@ -82,7 +82,8 @@ int ft_heredoc(t_data *data, char *stop)
break ;
else if (return_code == 1)
{
close(*ft_get_heredoc());
if (*ft_get_heredoc() > 2)
close(*ft_get_heredoc());
*ft_get_heredoc() = -1;
return (-2);
}

View File

@ -6,61 +6,23 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:59:01 by cchauvet #+# #+# */
/* Updated: 2023/04/05 14:09:39 by alouis-j ### ########.fr */
/* Updated: 2023/04/05 15:05:11 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
#include "signal_private.h"
static void ft_new_line(void)
void ft_ctrlc(int num)
{
(void) num;
rl_replace_line("", 0);
rl_on_new_line();
ft_putchar_fd('\n', 1);
rl_redisplay();
}
void ft_ctrlc(int num)
{
t_data *data;
if (num == SIGQUIT)
return ;
data = ft_get_data();
data->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");
}

View File

@ -6,13 +6,14 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:59:42 by cchauvet #+# #+# */
/* Updated: 2023/03/28 16:04:46 by cchauvet ### ########.fr */
/* Updated: 2023/04/05 15:05:00 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SIGNAL_PRIVATE_H
# define SIGNAL_PRIVATE_H
# include <signal.h>
# include <stdio.h>
# include <readline/readline.h>
# include <unistd.h>
# include "../libftx/libftx.h"

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 13:00:05 by cchauvet #+# #+# */
/* Updated: 2023/03/30 15:58:39 by cchauvet ### ########.fr */
/* Updated: 2023/04/05 15:11:16 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
@ -99,7 +99,7 @@ int ft_syntax_verif(t_data *data, const char *str)
|| ft_pipe_is_alone(str)
|| ft_special_char_dub(str))
{
data->exit_code = 2;
*data->exit_code = 2;
return (1);
}
return (0);

1
tester

@ -1 +0,0 @@
Subproject commit 1c6111b2fd281937d38ebfa7e8d87b38baef0802

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:39:48 by cchauvet #+# #+# */
/* Updated: 2023/03/30 13:04:53 by cchauvet ### ########.fr */
/* Updated: 2023/04/05 15:09:29 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,13 +19,13 @@ char *ft_get_executable_with_path(t_data *data, const char *name)
if (access(name, F_OK) != 0)
{
data->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);