-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex_bonus.c
145 lines (132 loc) · 3.34 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asioud <asioud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/31 21:22:29 by asioud #+# #+# */
/* Updated: 2023/01/02 19:16:03 by asioud ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/includes/libft.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
char *get_cmd_path(char *cmd, char **envp);
void execute(char *argv, char **envp);
void child_process(char *argv, char **envp);
void here_doc(int argc, char *delimiter);
int main(int argc, char **argv, char **envp);
int main(int argc, char **argv, char **envp)
{
int nb_cmd;
int fd[2];
if (argc >= 5)
{
if (ft_strncmp(argv[1], "here_doc", 8) == 0)
{
nb_cmd = 3;
fd[1] = open_file(argv[argc - 1], 0);
here_doc(argc, argv[2]);
}
else
{
nb_cmd = 2;
fd[1] = open_file(argv[argc - 1], 1);
fd[0] = open_file(argv[1], 2);
dup2(fd[0], STDIN_FILENO);
}
while (nb_cmd < argc - 2)
child_process(argv[nb_cmd++], envp);
dup2(fd[1], STDOUT_FILENO);
execute(argv[argc - 2], envp);
}
display_error("\e[1;31mInput Error\n", 1);
}
char *get_cmd_path(char *cmd, char **envp)
{
int i;
char *path;
char **paths;
i = 0;
while (ft_strnstr(envp[i], "PATH", 4) == 0)
i++;
paths = ft_split(envp[i], ':');
i = 0;
while (paths[i])
{
path = ft_strjoin_free(paths[i], ft_strjoin("/", cmd));
if (access(path, F_OK) == 0)
return (path);
free(path);
i++;
}
return (NULL);
}
void execute(char *argv, char **envp)
{
char **cmd;
char *path;
cmd = ft_split(argv, ' ');
path = get_cmd_path(cmd[0], envp);
if (path == NULL)
{
free_2d(cmd);
display_error("\e[1;31mPath Error", 1);
}
if (execve(path, cmd, envp) == -1)
display_error("\e[1;31mExec Error", 1);
}
void child_process(char *argv, char **envp)
{
pid_t pid;
int fd[2];
if (pipe(fd) == -1)
display_error("\e[1;31mChildp Pipe Error", 1);
pid = fork();
if (pid == -1)
display_error("\e[1;31mChildp Fork Error", 1);
if (pid == 0)
{
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
execute(argv, envp);
}
else
{
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
waitpid(pid, NULL, 0);
}
}
void here_doc(int argc, char *delimiter)
{
pid_t pid;
int fd[2];
char *line;
if (argc < 6)
display_error("\e[1;31mHere_doc wrong arguments", 1);
if (pipe(fd) == -1)
display_error("\e[1;31mHere_doc Pipe Error", 1);
pid = fork();
if (pid == 0)
{
close(fd[0]);
while (ft_fgets(&line))
{
if (ft_strncmp(line, delimiter, ft_strlen(delimiter)) == 0)
exit(EXIT_FAILURE);
write(fd[1], line, ft_strlen(line));
}
}
else
{
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
wait(NULL);
}
}
// ./pipex_bonus here_doc eof cat "grep main" outfile
// << eof cat | "grep main" | >> outfile