-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_nodes.c
More file actions
89 lines (78 loc) · 2.05 KB
/
prepare_nodes.c
File metadata and controls
89 lines (78 loc) · 2.05 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prepare_nodes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jklocker <jklocker@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/30 14:12:21 by jklocker #+# #+# */
/* Updated: 2023/01/31 13:20:06 by jklocker ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int get_node_count(t_info *info)
{
int count;
int i;
count = 0;
i = 0;
while (info->cmd_input[i])
{
if (info->cmd_input[i][0] == '|')
count++;
i++;
}
return (count);
}
int get_words_for_node(char **str, int i)
{
int ret;
ret = 0;
while (str[i] && str[i][0] != '|')
{
if (str[i][0] == '<' || str[i][0] == '>')
{
i = i + 2;
continue ;
}
i++;
ret++;
}
return (ret);
}
void malloc_nodes(t_info *info)
{
int node_count;
t_node *tmp;
tmp = malloc(sizeof(t_node));
if (!tmp)
call_perror_free(info);
info->head = tmp;
node_count = get_node_count(info);
while (node_count > 0)
{
tmp->next = malloc(sizeof(t_node));
if (!tmp->next)
call_perror_free(info);
tmp = tmp->next;
node_count--;
}
tmp->next = NULL;
}
void create_fd_out(t_info *info, int *i, t_node *cur)
{
int fd;
if (info->cmd_input[*i][1] == '\0')
fd = open(info->cmd_input[*i + 1], O_RDWR | O_CREAT, 0777);
else
fd = open(info->cmd_input[*i + 1], O_RDWR | O_CREAT | O_APPEND, 0777);
cur->out = fd;
*i = *i + 1;
}
void create_fd_in(t_info *info, int *i, t_node *cur)
{
int fd;
fd = open(info->cmd_input[*i + 1], O_RDWR);
cur->in = fd;
*i = *i + 1;
}