-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipex_bonus.c
114 lines (104 loc) · 3.27 KB
/
pipex_bonus.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aelsayed <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/27 17:22:22 by aelsayed #+# #+# */
/* Updated: 2025/02/15 16:43:25 by aelsayed ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
int here_doc(char *stopper)
{
char *line;
int fd;
int input;
char *trim;
fd = open(".here_doc_tmp", O_CREAT | O_RDWR | O_TRUNC, 0644);
input = open(".here_doc_tmp", O_CREAT | O_RDWR | O_TRUNC, 0644);
if (fd < 0)
{
printfd(2, "%s : Error creating temporary file\n", strerror(errno));
exit(EXIT_FAILURE);
}
while (1)
{
printfd(1, "pipex here_doc> ");
line = get_next_line(0);
if (!line)
break ;
trim = ft_strtrim(line, "\n/");
if (!ft_strncmp(trim, stopper, ft_strlen(line) + 1))
return (ft_free("11", trim, line), get_next_line(-1), input);
printfd(fd, "%s", trim);
ft_free("11", trim, line);
}
return (get_next_line(-1), input);
}
static void bfork_doc(t_var *vars, char **av, char **envp, int ac)
{
int i;
i = 3;
if (dup2(vars->fdi, 0) == -1)
exit_error(-1, 0, 'o', NULL);
unlink(".here_doc_tmp");
while (i < ac - 2)
{
pipe(vars->pipefd);
vars->pid1 = fork();
if (vars->pid1 == 0)
{
close(vars->pipefd[0]);
if (dup2(vars->pipefd[1], 1) == -1)
exit_error(-1, 0, 'o', NULL);
vars->path1 = check_path(envp, av[i]);
if (execve(vars->path1, trim(ft_split(av[i], ' ')), envp) == -1)
check(vars, av[i]);
}
else
redirect(vars);
i++;
}
bfork_parent(vars, av, envp, ac - 2);
}
void ft_bonus(int ac, char **av, char **envp, t_var *vars)
{
if (ac >= 6)
{
vars->fdi = here_doc(av[2]);
vars->fdo = file_opener(NULL, av[ac -1], 'o', 1);
if (!envp || !*envp || !path_index(envp))
exit(bpath_err(av, ac));
if (vars->fdi == -1)
vars->fdi = open("/dev/null", O_RDONLY);
bfork_doc(vars, av, envp, ac);
unlink(".here_doc_tmp");
exit(0);
}
printfd(2, "Invalid input for arguments:\nExpected Format:\n");
printfd(2, "\t./pipex here_doc LIMITER cmd1 ... cmdn <outfile>\n");
printfd(2, "\t./pipex <infile> cmd1 ... cmdn <outfile>\n");
exit (1);
}
void print_err(void)
{
printfd(2, "Invalid input for arguments:\nExpected Format:\n");
printfd(2, "\t./pipex <infile> cmd1 ... cmdn <outfile>\n");
printfd(2, "\t./pipex here_doc LIMITER cmd1 ... cmdn <outfile>\n");
}
int main(int ac, char **av, char **envp)
{
t_var vars;
if (!envp || !*envp || !path_index(envp))
printfd(2, "Invalid PATH variable!\n");
if (ac < 5)
return (print_err(), 1);
if (!ft_strncmp(av[1], "here_doc", ft_strlen(av[1])))
ft_bonus(ac, av, envp, &vars);
open_pipe(&vars, av, ac);
bfork_child(&vars, av, envp, ac);
return (globale_variable(-1));
}
//here_doc