-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms_pipe_and_redirect.c
70 lines (65 loc) · 2.06 KB
/
ms_pipe_and_redirect.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_pipe_and_redirect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlana <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 19:41:25 by obeedril #+# #+# */
/* Updated: 2022/03/14 16:08:57 by dlana ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ms_redirect(t_cmd *cmd)
{
if (cmd->fd[1] != 0)
{
dup2 (cmd->fd[1], 1);
close (cmd->fd[1]);
}
if (cmd->fd[0] != 0)
{
dup2 (cmd->fd[0], 0);
close (cmd->fd[0]);
}
return (0);
}
static void ms_pipe_2(t_data *data, int last, int i)
{
if (last != -1 && (data->cmd[i].redir[last] == 3
|| data->cmd[i].redir[last] == 4))
return ;
if (pipe(data->fd_pipe) == -1)
perror("fd[1]");
if (dup2(data->fd_pipe[1], 1) == -1)
perror("fd[1]");
if (close(data->fd_pipe[1]) == -1)
perror("fd[1]");
}
void ms_pipe(t_data *data, int i)
{
int last;
last = -1;
if (i > 0)
last = data->cmd[i - 1].last_redir;
if (i > 0 && !data->cmd[i].redir_born[0])
{
if (last != -1 && (data->cmd[i - 1].redir[last] == 3
|| data->cmd[i - 1].redir[last] == 4))
{
data->cmd[i].fd[0] = open("tmp", O_CREAT | O_RDONLY
| O_TRUNC, 0644);
if (dup2(data->cmd[i].fd[0], 0) == -1)
perror("fd[0]");
if (close(data->cmd[i].fd[0]) == -1)
perror("fd[0]");
return ;
}
if (dup2(data->fd_pipe[0], 0) == -1)
perror("fd[0]");
if (close(data->fd_pipe[0]) == -1)
perror("fd[0]");
}
if (i < data->num_cmd - 1 && !data->cmd[i].redir_born[1])
ms_pipe_2(data, last, i);
}