42_minishell/cmd/cmd.c
2023-04-17 10:41:54 +00:00

74 lines
2.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
/* Updated: 2023/04/17 10:41:23 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "cmd.h"
#include "cmd_private.h"
#include "../signal/signal.h"
#include <signal.h>
void ft_cmddel(void *ptr)
{
t_cmd *content;
content = (t_cmd *) ptr;
if (content->args != NULL)
ft_freer_tab_ultimate(1, content->args);
if (content->own_cmd == false && content->executable != NULL)
free(content->executable);
if (content->fd_in[0] > 2)
close(content->fd_in[0]);
if (content->fd_out[0] > 2)
close(content->fd_out[0]);
if (content->fd_in[1] > 2)
close(content->fd_in[1]);
if (content->fd_out[1] > 2)
close(content->fd_out[1]);
free(content);
}
void ft_cmdcloser(void *ptr)
{
t_cmd *cmd;
cmd = ptr;
ft_closer(cmd->fd_in);
ft_closer(cmd->fd_out);
}
void ft_cmdwaiter(void *ptr)
{
t_cmd *cmd;
int exit_status;
cmd = ptr;
if (cmd->executable != NULL && cmd->own_cmd == 0
&& cmd->pid != -1 && cmd->fd_in[0] != -2 && cmd->fd_out[0] != -2)
{
waitpid(cmd->pid, &exit_status, 0);
if (WIFSIGNALED(exit_status))
{
if (exit_status == 131)
{
ft_printf("Quit (core dumped)");
*ft_get_exit_code() = 131;
}
else
*ft_get_exit_code() = 130;
ft_printf("\n");
}
else
*ft_get_exit_code() = WEXITSTATUS(exit_status);
}
signal(SIGINT, ft_ctrlc);
signal(SIGQUIT, ft_quit);
}