-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredirect_utils.c
69 lines (63 loc) · 2.14 KB
/
redirect_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirect_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ggiannit <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/13 14:53:26 by ggiannit #+# #+# */
/* Updated: 2023/04/14 14:47:47 by ggiannit ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_red_error(t_mish *meta)
{
ft_putstr_fd("duckshell:\
syntax error near unexpected token `newline'\n", 2);
meta->exit_code = 2;
return (2);
}
void ft_in_heredoc_child(t_mish *meta, char *delimiter)
{
char *nline;
size_t delimiter_size;
meta->infile = open("/tmp/.heredoc", O_WRONLY | O_CREAT, 0644);
delimiter_size = ft_strlen(delimiter) + 1;
delimiter = ft_strjoin(delimiter, "\n");
ft_putstr_fd("heredoc> ", 1);
nline = get_next_line(0);
while (ft_strncmp(delimiter, nline, delimiter_size))
{
ft_putstr_fd(nline, meta->infile);
ft_free_null(&nline);
ft_putstr_fd("heredoc> ", 1);
while (!nline)
nline = get_next_line(0);
}
get_next_line(-42);
ft_free_null(&nline);
ft_free_null(&delimiter);
ft_free_shell(meta);
exit(1);
}
int ft_in_heredoc(t_mish *meta, char *delimiter)
{
int pid;
ft_magic_heredoc(0);
signal(SIGINT, ft_sign_handler_heredoc);
unlink("/tmp/.heredoc");
if (!delimiter[0])
return (0);
if (meta->infile != -2 && meta->infile != -1)
close(meta->infile);
pid = fork();
ft_magic_heredoc(pid);
if (!pid)
ft_in_heredoc_child(meta, delimiter);
waitpid(pid, &pid, 0);
signal(SIGINT, ft_sign_handler_exec);
if (!WEXITSTATUS(pid))
return (0);
meta->infile = open("/tmp/.heredoc", O_RDONLY);
return (1);
}