forked from MaroIsLife/Minishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathred_open.c
79 lines (73 loc) · 1.39 KB
/
red_open.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
# include "minishell.h"
void print_errno(char *filename)
{
char *err;
err = strerror(errno);
write(2, "minishell: ", 11);
write(2, filename, ft_strlen(filename));
write(2, " ", 1);
write(2, err, ft_strlen(err));
write(2, "\n", 1);
g_global.return_value = 1;
exit(0);
}
void red_open_pipe_two(t_filename *p, int *fd, int *fd2)
{
g_global.fd1 = 0;
g_global.fd2 = 0;
if (p->fd_r_c == 50)
{
dup2((*fd2), 0);
dup2((*fd), 1);
}
else if (p->c == 94 || p->c == '>')
dup2((*fd), 1);
else if (p->c == '<')
dup2((*fd2), 0);
}
void red_open_pipe(t_filename *tmp)
{
int fd;
int fd2;
t_filename *p;
p = tmp;
while (1)
{
if (p->c == 94)
fd = open(p->filename, O_RDWR | O_APPEND | O_CREAT, 0777);
else if (p->c == '>')
fd = open(p->filename, O_RDWR | O_TRUNC | O_CREAT, 0777);
else if (p->c == '<')
fd2 = open(p->filename, O_RDONLY);
if (fd == -1 || fd2 == -1)
print_errno(p->filename);
if (p->next == NULL)
break ;
p = p->next;
}
red_open_pipe_two(p, &fd, &fd2);
}
void red_open_two(t_filename *p, int *fd, int *fd2, t_source *src)
{
if (src->fd_r_c == 50)
{
dup2((*fd2), 0);
dup2((*fd), 1);
}
else if (src->fd_r_c == 51)
{
dup2(src->tmp, 0);
dup2((*fd), 1);
}
else if (p->c == 127)
{
dup2(src->tmp, 0);
}
else if (p->c == 94 || p->c == '>')
dup2((*fd), 1);
else if (p->c == '<')
dup2((*fd2), 0);
close((*fd));
close((*fd2));
close(src->tmp);
}