2023-02-15 14:52:27 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* cmd.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/02/15 14:18:21 by cchauvet #+# #+# */
|
2023-04-17 08:16:41 -04:00
|
|
|
/* Updated: 2023/04/17 11:57:07 by cchauvet ### ########.fr */
|
2023-02-15 14:52:27 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-04-07 11:12:48 -04:00
|
|
|
#include "cmd.h"
|
2023-03-10 06:32:39 -05:00
|
|
|
#include "cmd_private.h"
|
2023-04-17 06:41:54 -04:00
|
|
|
#include "../signal/signal.h"
|
|
|
|
#include <signal.h>
|
2023-02-15 14:52:27 -05:00
|
|
|
|
|
|
|
void ft_cmddel(void *ptr)
|
|
|
|
{
|
|
|
|
t_cmd *content;
|
|
|
|
|
|
|
|
content = (t_cmd *) ptr;
|
|
|
|
if (content->args != NULL)
|
|
|
|
ft_freer_tab_ultimate(1, content->args);
|
2023-03-10 06:32:39 -05:00
|
|
|
if (content->own_cmd == false && content->executable != NULL)
|
2023-02-15 14:52:27 -05:00
|
|
|
free(content->executable);
|
2023-03-10 06:32:39 -05:00
|
|
|
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]);
|
2023-02-15 14:52:27 -05:00
|
|
|
free(content);
|
|
|
|
}
|
2023-03-31 10:36:15 -04:00
|
|
|
|
|
|
|
void ft_cmdcloser(void *ptr)
|
|
|
|
{
|
|
|
|
t_cmd *cmd;
|
|
|
|
|
|
|
|
cmd = ptr;
|
|
|
|
ft_closer(cmd->fd_in);
|
|
|
|
ft_closer(cmd->fd_out);
|
|
|
|
}
|
2023-04-07 11:12:48 -04:00
|
|
|
|
|
|
|
void ft_cmdwaiter(void *ptr)
|
|
|
|
{
|
|
|
|
t_cmd *cmd;
|
|
|
|
int exit_status;
|
|
|
|
|
|
|
|
cmd = ptr;
|
|
|
|
if (cmd->executable != NULL && cmd->own_cmd == 0
|
2023-04-07 11:17:45 -04:00
|
|
|
&& cmd->pid != -1 && cmd->fd_in[0] != -2 && cmd->fd_out[0] != -2)
|
2023-04-07 11:12:48 -04:00
|
|
|
{
|
|
|
|
waitpid(cmd->pid, &exit_status, 0);
|
|
|
|
if (WIFSIGNALED(exit_status))
|
|
|
|
{
|
|
|
|
if (exit_status == 131)
|
|
|
|
{
|
2023-04-17 06:41:54 -04:00
|
|
|
ft_printf("Quit (core dumped)");
|
2023-04-07 11:12:48 -04:00
|
|
|
*ft_get_exit_code() = 131;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*ft_get_exit_code() = 130;
|
2023-04-17 06:41:54 -04:00
|
|
|
ft_printf("\n");
|
2023-04-07 11:12:48 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
*ft_get_exit_code() = WEXITSTATUS(exit_status);
|
|
|
|
}
|
2023-04-17 06:41:54 -04:00
|
|
|
signal(SIGINT, ft_ctrlc);
|
2023-04-17 08:16:41 -04:00
|
|
|
signal(SIGQUIT, SIG_IGN);
|
2023-04-07 11:12:48 -04:00
|
|
|
}
|