-
Notifications
You must be signed in to change notification settings - Fork 0
/
here_docs.c
executable file
·83 lines (76 loc) · 1.37 KB
/
here_docs.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
#include "minishell.h"
int check_end(char *input, char *delimiter)
{
int size;
int equal;
equal = 0;
size = 0;
if (!input)
return (1);
if (g_status == 130)
return (1);
if (!ft_strncmp(input, delimiter, ft_strlen(delimiter)))
equal = 1;
if (ft_strlen(delimiter) == (ft_strlen(input) - 1))
size = 1;
if (size && equal)
return (1);
return (0);
}
char *return_free(char *input)
{
if (input)
free(input);
return (NULL);
}
char *get_input(char *delimiter)
{
char *returned_str;
char *input;
char *temp_str;
returned_str = ft_calloc(1, 1);
input = ft_calloc(1, 1);
while (!check_end(input, delimiter))
{
temp_str = returned_str;
returned_str = ft_strjoin(returned_str, input);
freetwo_ptrs(&temp_str, &input);
input = readline(">");
if (!input)
{
printf("%s (wanted `%s\')\n", HDOC_WARN, delimiter);
break ;
}
temp_str = input;
input = ft_strjoin(input, "\n");
free(temp_str);
}
ft_free(&input);
if (g_status == 130)
return_free(returned_str);
return (returned_str);
}
int here_docs(char *delimiter)
{
int fd[2];
char *input;
input = NULL;
g_status = 0;
if (pipe(fd) == -1)
{
ft_perror(1, NULL, PIPERR);
return (-1);
}
input = get_input(delimiter);
if (g_status == 130)
{
close(fd[0]);
close(fd[1]);
return (-1);
}
write(fd[1], input, ft_strlen(input));
if (input)
free(input);
close(fd[1]);
return (fd[0]);
}