54 lines
1.6 KiB
C
54 lines
1.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* subshell_wait.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
|
/* Updated: 2021/11/18 23:09:46 by hulamy ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "minishell.h"
|
|
|
|
static int handle_wait_error(void);
|
|
|
|
void wait_subshell(pid_t last_cmd_pid, int *last_exit_status)
|
|
{
|
|
int wstatus;
|
|
int ret;
|
|
|
|
wstatus = 0;
|
|
if (last_cmd_pid > 0)
|
|
{
|
|
if (waitpid(last_cmd_pid, &wstatus, 0) == -1)
|
|
perror("waitpid()");
|
|
if (WIFEXITED(wstatus))
|
|
*last_exit_status = WEXITSTATUS(wstatus);
|
|
if (WIFSIGNALED(wstatus))
|
|
{
|
|
write(STDIN_FILENO, "\n", 1);
|
|
*last_exit_status = 128 + WTERMSIG(wstatus);
|
|
}
|
|
}
|
|
ret = 0;
|
|
while (ret != -1)
|
|
{
|
|
ret = wait(&wstatus);
|
|
if (ret == -1)
|
|
ret = handle_wait_error();
|
|
}
|
|
}
|
|
|
|
static int handle_wait_error(void)
|
|
{
|
|
if (errno == ECHILD)
|
|
return (-1);
|
|
else if (errno == EINTR)
|
|
return (0);
|
|
else
|
|
perror("wait()");
|
|
return (-1);
|
|
}
|