-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_forks.c
More file actions
100 lines (92 loc) · 2.65 KB
/
handle_forks.c
File metadata and controls
100 lines (92 loc) · 2.65 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_forks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jklocker <jklocker@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/30 14:12:01 by jklocker #+# #+# */
/* Updated: 2023/01/30 15:57:51 by jklocker ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void handle_forks(t_info *info)
{
int pid;
t_node *cur;
int cur_in;
pid = 0;
cur = info->head;
cur_in = STDIN_FILENO;
if (!cur->next)
{
if (check_builtin(cur, info) == 1)
return ;
}
handle_signals(2);
loop_forks(info, cur, pid, cur_in);
if (g_status != 0 && g_status != 1 && g_status != 2)
g_status = 127;
}
void loop_forks(t_info *info, t_node *cur, int pid, int cur_in)
{
while (cur)
{
pipe(info->fd);
node_check_path(cur);
pid = fork();
if (pid == -1)
call_perror_free(info);
if (pid == 0)
{
dup2(cur_in, 0);
if (cur->next)
dup2(info->fd[WRITE_END], 1);
close(info->fd[READ_END]);
check_builtin_fork(cur, info);
}
close(info->fd[WRITE_END]);
if (cur_in != 0)
close(cur_in);
cur_in = info->fd[READ_END];
cur = cur->next;
}
pid = -1;
while (pid++ < get_node_count(info))
wait(&g_status);
close(info->fd[READ_END]);
}
void handle_executer(t_info *info, t_node *cur)
{
dup2(cur->in, 0);
dup2(cur->out, 1);
executer(info, cur);
}
void node_check_path(t_node *cur)
{
if (!cur->full_path && is_builtin(cur) == 0)
{
ft_printf("%s: command not found\n", cur->full_cmd[0]);
g_status = 127;
}
}
int is_builtin(t_node *node)
{
int i;
i = ft_strlen(node->full_cmd[0]);
if (ft_strcmp(node->full_cmd[0], "echo") == 0 && i == 4)
return (1);
else if (ft_strcmp(node->full_cmd[0], "pwd") == 0 && i == 3)
return (1);
else if (ft_strcmp(node->full_cmd[0], "env") == 0 && i == 3)
return (1);
else if (ft_strcmp(node->full_cmd[0], "export") == 0 && i == 6)
return (1);
else if (ft_strcmp(node->full_cmd[0], "unset") == 0 && i == 5)
return (1);
else if (ft_strcmp(node->full_cmd[0], "exit") == 0 && i == 4)
return (1);
else if (ft_strcmp(node->full_cmd[0], "cd") == 0 && i == 2)
return (1);
return (0);
}