-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution_redirect.c
More file actions
executable file
·112 lines (102 loc) · 2.73 KB
/
execution_redirect.c
File metadata and controls
executable file
·112 lines (102 loc) · 2.73 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execution_redirect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hyeyeom <hyeyeom@42student.gyeongsan.kr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/23 16:58:51 by yuhyoon #+# #+# */
/* Updated: 2025/04/14 00:38:35 by hyeyeom ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void sort_io(t_stack *in, t_stack *out, t_ready *rdy)
{
t_list *tmp;
t_redrct *rd;
int n;
n = rdy->rdrct_num;
tmp = rdy->rdrct;
while (n)
{
rd = (t_redrct *)(tmp)->content;
if (rd->type == IN_RD)
push(in, rd);
else if (rd->type == OUT_RD)
queue(out, rd);
n--;
tmp = (tmp)->next;
}
}
void set_io(t_ready *rdy, t_stack *in, t_stack *out)
{
(rdy)->rdrct_in = (t_redrct *)pop2(in);
if (rdy->rdrct_in)
{
if (rdy->rdrct_in->parts == 1)
rdy->rdrct_in->fd = open(rdy->rdrct_in->obj, O_RDONLY);
}
while (out->top)
{
(rdy)->rdrct_out = (t_redrct *)dequeue(out);
if (rdy->rdrct_out)
{
if (rdy->rdrct_out->parts == 1)
rdy->rdrct_out->fd = open(rdy->rdrct_out->obj, \
O_TRUNC | O_WRONLY | O_CREAT, 0644);
else if (rdy->rdrct_out->parts == 2)
rdy->rdrct_out->fd = open(rdy->rdrct_out->obj, \
O_APPEND | O_WRONLY | O_CREAT, 0644);
}
}
}
int prmssn_rdrct(t_redrct *rdrct)
{
int flg;
flg = 0;
if (rdrct == NULL)
return (flg);
if (rdrct->type == IN_RD && rdrct->parts == 1)
flg = access(rdrct->obj, R_OK);
else if (rdrct->type == OUT_RD && rdrct->parts == 2)
flg = access(rdrct->obj, W_OK);
if (flg != 0)
err_permission(1, rdrct->obj);
return (flg);
}
int find_rdrct(t_ready *rdy)
{
t_list *lst;
t_redrct *rdrct;
int n;
int acc_flg;
lst = rdy->rdrct;
errno = 0;
n = rdy->rdrct_num;
acc_flg = 0;
while (n && acc_flg == 0)
{
rdrct = (t_redrct *)(lst->content);
if (rdrct->type == IN_RD && rdrct->parts == 1)
acc_flg = access(rdrct->obj, F_OK);
lst = lst->next;
n--;
}
if (acc_flg != 0)
err_such(1, rdrct->obj);
return (acc_flg);
}
int redirect(t_ready *rdy)
{
t_stack in;
t_stack out;
if (rdy == NULL)
return (-1);
init_stack(&in, NULL, 0);
init_stack(&out, NULL, 0);
sort_io(&in, &out, rdy);
set_io(rdy, &in, &out);
del_stack(&in);
del_stack(&out);
return (0);
}