feat: Added signals

Added functionality of the signals ctrl+d, ctrl+c and ctr+\
This commit is contained in:
marcnava-42cursus
2026-02-13 18:08:45 +01:00
parent 3e31447073
commit c166d0f77a
6 changed files with 212 additions and 72 deletions

View File

@@ -12,6 +12,34 @@
#include "executor.h"
static void set_signal_exit_status(
int status,
uint8_t *exit_status
)
{
if (WTERMSIG(status) == SIGINT)
write(STDOUT_FILENO, "\n", 1);
else if (WTERMSIG(status) == SIGQUIT)
{
if (WCOREDUMP(status))
write(STDERR_FILENO, "Quit (core dumped)\n", 19);
else
write(STDERR_FILENO, "Quit\n", 5);
}
*exit_status = 128 + WTERMSIG(status);
}
static void set_last_child_status(
int status,
uint8_t *exit_status
)
{
if (WIFEXITED(status))
*exit_status = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
set_signal_exit_status(status, exit_status);
}
void executor_child_process(
t_list *current_command,
t_pipeline *pipeline,
@@ -22,6 +50,7 @@ void executor_child_process(
t_command *command;
command = current_command->content;
minishell_set_child_signals();
executor_setup_child_input(pipeline);
executor_setup_child_output(current_command, pipeline);
if (!executor_apply_redirections(command, NULL, NULL))
@@ -59,54 +88,8 @@ uint8_t executor_wait_for_children(
while (last_child_pid > 0 && pid > 0)
{
if (pid == last_child_pid)
{
if (WIFEXITED(status))
exit_status = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
exit_status = 128 + WTERMSIG(status);
}
set_last_child_status(status, &exit_status);
pid = wait(&status);
}
return (exit_status);
}
static void cmdfree_argv(
char **argv
)
{
size_t i;
if (argv == NULL)
return ;
i = 0;
while (argv[i] != NULL)
{
free(argv[i]);
i++;
}
free(argv);
}
static void cmdfree_redirection(
t_redirection *redirection
)
{
if (redirection == NULL)
return ;
free(redirection->target);
free(redirection);
}
void executor_cmdfree(
t_command *command
)
{
if (command == NULL)
return ;
cmdfree_argv(command->argv);
free(command->path);
ft_lstclear(&command->redirections,
(void (*)(void *))cmdfree_redirection);
ft_lstclear(&command->heredocs, (void (*)(void *))cmdfree_redirection);
free(command);
}