-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio-redir.c
More file actions
71 lines (65 loc) · 2.01 KB
/
Copy pathio-redir.c
File metadata and controls
71 lines (65 loc) · 2.01 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* io-redir.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: moabed <moabed@student.42amman.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/02 09:30:53 by moabed #+# #+# */
/* Updated: 2026/05/16 12:55:59 by moabed ### ########.fr */
/* */
/* ************************************************************************** */
#include "headers/executionpart.h"
void output_handle(t_redir *red, t_cmd **current_cmd, int option)
{
int fd;
fd = -1;
if (option == 1)
fd = open(red->filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (option == 2)
fd = open(red->filename, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (fd == -1)
{
perror(red->filename);
check_fds(*current_cmd);
(*current_cmd)->fd_out = -1;
}
else
{
if ((*current_cmd)->fd_out > 2)
close((*current_cmd)->fd_out);
(*current_cmd)->fd_out = fd;
}
}
void input_handle(t_redir *red, t_cmd **current_cmd)
{
int fd;
fd = open(red->filename, O_RDONLY);
if (fd == -1)
{
perror(red->filename);
check_fds(*current_cmd);
(*current_cmd)->fd_in = -1;
}
else
{
if ((*current_cmd)->fd_in > 2)
close((*current_cmd)->fd_in);
(*current_cmd)->fd_in = fd;
}
}
void redir_handle(t_redir *redir, t_cmd **cmd)
{
while (redir)
{
if (redir->type == INPUT)
input_handle(redir, cmd);
else if (redir->type == TRUNC)
output_handle(redir, cmd, 1);
else if (redir->type == APPEND)
output_handle(redir, cmd, 2);
if ((*cmd)->fd_in == -1 || (*cmd)->fd_out == -1)
return ;
redir = redir->next;
}
}