-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms_launch_cmd.c
101 lines (93 loc) · 2.76 KB
/
ms_launch_cmd.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
94
95
96
97
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_launch_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: flormich <flormich@student.42wolfsburg.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/12 11:26:13 by flormich #+# #+# */
/* Updated: 2022/01/03 17:19:24 by pnuti ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell_libs.h"
static void exec_child(t_struct *st, int tr, int *next_fd)
{
if (st->arr[tr].limiter != NULL)
read_till_limiter(st, tr);
set_red_shell(st, st->tr, next_fd);
if (st->arr[tr].fd_out != 1)
dup2(st->arr[tr].fd_out, STDOUT_FILENO);
execve(st->arr[tr].cmd[0], st->arr[tr].cmd, st->env);
}
static void manage_fd(t_struct *st, int *next_fd)
{
if (st->tr < st->nb_cmd)
dup2(next_fd[READ], st->fd[READ]);
close(next_fd[READ]);
close(next_fd[WRITE]);
close(st->fd[WRITE]);
}
static int launch_pipe(t_struct*st)
{
pid_t pid;
int status;
int next_fd[2];
if (pipe(next_fd) == -1)
return (-1);
status = 0;
pid = fork();
if (pid < 0)
perror ("Failed to create Child");
if (pid == 0)
exec_child(st, st->tr, next_fd);
manage_fd(st, next_fd);
if (waitpid(pid, &status, 0) < 0)
return (-1);
if (WIFEXITED(status))
g_exit_value = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
{
g_exit_value = 128 + WTERMSIG(status);
if (WTERMSIG(status) == SIGQUIT)
ft_putendl_fd("Quit (core dumped)", 2);
}
return (0);
}
static int launch_builtin(t_struct *st)
{
int next_fd[2];
if (pipe(next_fd) < 0)
return (1);
dup2(next_fd[WRITE], st->fd[WRITE]);
dup2(next_fd[READ], st->fd[READ]);
close(next_fd[WRITE]);
close(next_fd[READ]);
if (st->arr[st->tr].f_ptr == &run_echo
&& st->tr + 1 != st->nb_cmd && st->arr[st->tr + 1].f_ptr == &run_echo
&& !st->arr[st->tr].logical)
return (0);
if (st->tr + 1 == st->nb_cmd || st->arr[st->tr].logical)
dup2(st->arr[st->tr].fd_out, st->fd[WRITE]);
g_exit_value = st->arr[st->tr].f_ptr(st, &(st->arr[st->tr]));
return (0);
}
int launch_cmd(t_struct *st)
{
if (pipe(st->fd) == -1)
return (-1);
while (st->tr < st->nb_cmd)
{
if (if_launch_cmd(st) == 1)
{
if (st->arr[st->tr].cmd_type == BUILTIN)
launch_builtin(st);
else
{
if (launch_pipe(st) == -1)
return (-1);
}
}
st->tr++;
}
return (0);
}