-
Notifications
You must be signed in to change notification settings - Fork 1
/
fork_pipes.c
71 lines (61 loc) · 1.15 KB
/
fork_pipes.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
# include "minishell.h"
int get_last_filename(t_filename *tmp, int i)
{
int fd;
fd = -99;
while (tmp)
{
if (i == 0)
fd = open(tmp->filename, O_CREAT | O_WRONLY | O_APPEND, 0644);
else
fd = open(tmp->filename, O_RDONLY);
if (tmp->next == NULL)
return (fd);
tmp = tmp->next;
}
return (fd);
}
void both_red(t_filename *tmp)
{
int fd_input;
int fd_out;
fd_input = open(tmp->filename, O_RDONLY);
tmp = tmp->next;
fd_out = open(tmp->filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd_out == -1)
exit(1);
if (fd_input == -1)
exit(1);
dup2(fd_input, 0);
close(fd_input);
dup2(fd_out, 1);
close(fd_out);
}
void red_open_normal(int write_fd, t_filename *tmp)
{
int file;
file = get_last_filename(tmp, 1);
dup2(file, 0);
dup2(write_fd, 1);
close(file);
}
void red_read (int read_fd, t_filename *tmp)
{
int file;
file = get_last_filename(tmp, 0);
if (file == -1)
exit(1);
dup2(file, 1);
dup2(read_fd, 0);
close(file);
}
void red_open_append(int read_fd, t_filename *tmp)
{
int file;
file = open(tmp->filename, O_CREAT | O_WRONLY | O_APPEND, 0644);
if (file == -1)
exit(1);
dup2(file, 1);
dup2(read_fd, 0);
close(file);
}