-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1mid_parent_son.c
77 lines (71 loc) · 2.5 KB
/
1mid_parent_son.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 1mid_parent_son.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: guilmira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/19 11:03:44 by guilmira #+# #+# */
/* Updated: 2021/12/06 10:58:03 by guilmira ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
//frees
void clean_memory(t_arguments *args)
{
if (args)
{
if (args)
;
}
}
/** PURPOSE : Executes forked process for all the mid commands
* 1. Sets truct of command. Identify its path.
* 2. Sets the proper inputs and outputs, redirecting the
* exits (like STDOUT_FILENO) to the correct pipe file descriptor.
* 3. Executes command by searching in path with execve. */
static void mid_son(int index, t_arguments *args)
{
t_command *command_struct;
command_struct = NULL;
command_struct = ft_lst_position(args->commands_lst, args->command_number);
if (!command_struct)
ft_shutdown(LST, 0, args);
if (dup2(args->fds[index - 2], STDIN_FILENO) == -1)
ft_shutdown(DUP_ERROR, 0, args);
close(args->fds[index - 2]);
if (dup2(args->fds[index + 1], STDOUT_FILENO) == -1)
ft_shutdown(DUP_ERROR, 0, args);
close(args->fds[index + 1]);
if (execve(command_struct->path, command_struct->command, NULL) == -1)
ft_shutdown(EXE_ERROR, 0, args);
}
/** PURPOSE : Mid process for all the commands that are not
* wither the first one or the last one.
* 1. Establish syntax and index.
* 2. Forks the process annd creates a single son.
* 2. Excutes mid_son and waits for it to finish. */
void mid_process(t_arguments *args)
{
int index;
int status;
int identifier;
args->command_number++;
index = args->command_number * 2;
if (pipe(&args->fds[index]) == -1)
ft_shutdown(MSG, 0, args);
identifier = fork();
if (identifier == 0)
{
close(args->fds[index]);
mid_son(index, args);
}
else if (identifier > 0)
{
wait(&status);
close(args->fds[index + 1]);
return ;
}
else
ft_shutdown(FORK_ERROR, 0, args);
}