Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Minishell-42
Path: blob/main/parsing/parsing_utils2.c
1407 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* parsing_utils2.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: ssabbaji <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/07/26 18:17:10 by yabtaour #+# #+# */
9
/* Updated: 2022/10/03 14:24:02 by ssabbaji ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "../minishell.h"
14
15
int ft_get_in(int *fd, int *red, int red_num)
16
{
17
int fd_in;
18
int i;
19
20
fd_in = 0;
21
i = 0;
22
while (i < red_num)
23
{
24
if (red[i] == 3)
25
fd_in = fd[i];
26
i++;
27
}
28
return (fd_in);
29
}
30
31
int ft_get_out(int *fd, int *red, int red_num)
32
{
33
int fd_out;
34
int i;
35
36
fd_out = 1;
37
i = 0;
38
while (i < red_num)
39
{
40
if (red[i] == 1 || red[i] == 2)
41
fd_out = fd[i];
42
i++;
43
}
44
return (fd_out);
45
}
46
47
int *ft_fill_red(t_data *data, int red_num)
48
{
49
t_lexer *lexer_clone;
50
int *red;
51
int i;
52
53
i = 0;
54
red = malloc(sizeof(int) * red_num);
55
if (!red)
56
exit (1);
57
lexer_clone = data->lst_lexer;
58
while (lexer_clone && lexer_clone->type != PIPE)
59
{
60
if (ft_strcmp(lexer_clone->val, ">") == 0)
61
red[i++] = 1;
62
if (ft_strcmp(lexer_clone->val, ">>") == 0)
63
red[i++] = 2;
64
if (ft_strcmp(lexer_clone->val, "<") == 0)
65
red[i++] = 3;
66
lexer_clone = lexer_clone->next;
67
}
68
return (red);
69
}
70
71
int ft_fill_fd(t_data *data, char *name, int red)
72
{
73
int fd;
74
75
(void)data;
76
if (red == 1)
77
fd = open(name, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0664);
78
if (red == 2)
79
fd = open(name, O_RDWR | O_CREAT | O_APPEND | O_CLOEXEC, 0664);
80
if (red == 3)
81
{
82
fd = open(name, O_RDONLY | O_CLOEXEC, 0777);
83
if (fd == -1)
84
{
85
printf("No such file or directory\n");
86
data->rerror_f = 1;
87
g_vars.g_exit_stat = 1;
88
fd = -69;
89
}
90
}
91
return (fd);
92
}
93
94
int ft_red_num(t_data *data)
95
{
96
int red_num;
97
t_lexer *lexer_clone;
98
99
red_num = 0;
100
lexer_clone = data->lst_lexer;
101
while (lexer_clone && lexer_clone->type != PIPE)
102
{
103
if (lexer_clone->type == REDIRECTION
104
&& ft_strcmp(lexer_clone->val, "<<"))
105
red_num++;
106
lexer_clone = lexer_clone->next;
107
}
108
return (red_num);
109
}
110
111