-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.c
94 lines (86 loc) · 2.39 KB
/
setup.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* :::::::: */
/* setup.c :+: :+: */
/* +:+ */
/* By: jvan-hal <[email protected]> +#+ */
/* +#+ */
/* Created: 2023/03/14 14:05:18 by jvan-hal #+# #+# */
/* Updated: 2023/03/23 11:09:02 by jvan-hal ######## odam.nl */
/* */
/* ************************************************************************** */
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include"pipex.h"
static void init_comm_state(t_info *state, int argc)
{
state->comm_paths = (char **)ft_calloc(argc - 2, sizeof(char *));
if (!state->comm_paths)
{
write(2, "not enough memory available\n", 28);
free_state(state);
exit(EXIT_FAILURE);
}
state->comm_argv = (char ***)ft_calloc(argc - 2, sizeof(char **));
if (!state->comm_argv)
{
write(2, "not enough memory available\n", 28);
free_state(state);
exit(EXIT_FAILURE);
}
}
t_info *init_state(char **argv, char **envp, int argc)
{
t_info *state;
state = (t_info *)ft_calloc(1, sizeof(t_info));
if (!state)
{
write(2, "not enough memory available\n", 28);
exit(EXIT_FAILURE);
}
state->index = 0;
state->offset = 2;
state->argv = argv;
state->paths = get_paths(envp);
init_comm_state(state, argc);
return (state);
}
static void write_to_heredoc(char *delim, int fd)
{
char *line;
line = get_next_line(0);
while (line)
{
if (ft_strncmp(line, delim, ft_strlen(delim) + 1) == 0)
{
free(line);
break ;
}
write(fd, line, ft_strlen(line));
free(line);
line = get_next_line(0);
}
}
int read_heredoc(t_info *state)
{
int tube[2];
char *delim;
if (pipe(tube) == -1)
{
perror("File descriptor");
free_state(state);
exit(EXIT_FAILURE);
}
delim = ft_strjoin(state->argv[2], "\n");
write_to_heredoc(delim, tube[1]);
free(delim);
if (close(tube[1]) == -1)
{
perror("File descriptor");
free_state(state);
exit(EXIT_FAILURE);
}
return (tube[0]);
}