-
Notifications
You must be signed in to change notification settings - Fork 2
/
execute_command_jobs.c
93 lines (86 loc) · 2.42 KB
/
execute_command_jobs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute_command_jobs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkong <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/18 16:33:05 by jkong #+# #+# */
/* Updated: 2022/06/24 20:58:50 by jkong ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "safe_io.h"
#include "safe_mem.h"
#include "generic_list.h"
#include <unistd.h>
#include <sys/wait.h>
static int _is_regular_fd(int fd)
{
if (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO)
return (0);
return (1);
}
void do_piping(int pipe_in, int pipe_out, int pipe_next)
{
if (pipe_in != NO_PIPE)
{
if (dup2(pipe_in, STDIN_FILENO) < 0)
exit_fail("dup");
if (_is_regular_fd(pipe_in))
close(pipe_in);
}
if (pipe_out != NO_PIPE)
{
if (dup2(pipe_out, STDOUT_FILENO) < 0)
exit_fail("dup");
if (_is_regular_fd(pipe_out))
close(pipe_out);
}
if (pipe_next != NO_PIPE)
close(pipe_next);
}
pid_t make_child(t_shell *sh)
{
pid_t pid;
t_list_process *next;
pid = fork();
if (pid < 0)
exit_fail("fork");
if (pid == 0)
{
list_walk((void *)sh->pid_list, free_safe);
sh->pid_list = NULL;
set_signal_handler(-1);
}
else
{
next = calloc_safe(1, sizeof(*next));
next->pid = pid;
list_append((void *)&sh->pid_list, (void *)next);
}
return (pid);
}
int wait_for(t_shell *sh, pid_t pid)
{
pid_t got_pid;
t_list_process *next;
int status;
status = 0;
sh->pid_list = (void *)list_reverse((void *)sh->pid_list);
set_signal_handler(0);
got_pid = 0;
while (sh->pid_list && got_pid != pid)
{
next = sh->pid_list->next;
got_pid = waitpid(sh->pid_list->pid, &status, 0);
if (got_pid < 0)
exit_fail("waitpid");
free(sh->pid_list);
sh->pid_list = next;
}
if (pid != -1 && WIFSIGNALED(status))
on_signal();
set_signal_handler(1);
return (get_exit_status(status));
}