-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipex.c
More file actions
137 lines (126 loc) · 3.4 KB
/
pipex.c
File metadata and controls
137 lines (126 loc) · 3.4 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dhuss <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 12:05:42 by dhuss #+# #+# */
/* Updated: 2024/07/19 12:28:21 by dhuss ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void command_one(int fd[2], char *argv[], char *envp[])
{
char *path;
char **cmd;
int infile_fd;
if (access(argv[1], F_OK) != 0)
error_custom(errno, argv[1]);
infile_fd = open(argv[1], O_RDONLY);
if (infile_fd == -1)
error_custom(errno, NULL);
if (dup2(infile_fd, STDIN_FILENO) == -1)
error_custom(errno, NULL);
if (close(fd[0]) == -1)
error_custom(errno, NULL);
if (dup2(fd[1], STDOUT_FILENO) == -1)
error_custom(errno, NULL);
close_it(fd[1], infile_fd);
cmd = split_cmd_and_para(argv[2]);
path = get_path(cmd[0], envp);
if (path == NULL)
path = cmd[0];
if (execve(path, cmd, envp))
{
clear_all(cmd);
error_custom(127, argv[2]);
}
}
void command_two(int fd[2], char *argv[], char *envp[])
{
char *path;
char **cmd;
int outfile_fd;
check_outfile(5, argv);
outfile_fd = open(argv[4], O_WRONLY);
if (outfile_fd == -1)
error_custom(errno, NULL);
if (dup2(outfile_fd, STDOUT_FILENO) == -1)
error_custom(errno, NULL);
if (close(fd[1]) == -1)
error_custom(errno, NULL);
if (dup2(fd[0], STDIN_FILENO) == -1)
error_custom(errno, NULL);
close_it(fd[0], outfile_fd);
cmd = split_cmd_and_para(argv[3]);
path = get_path(cmd[0], envp);
if (path == NULL)
path = cmd[0];
if (execve(path, cmd, envp) == -1)
{
clear_all(cmd);
error_custom(127, argv[3]);
}
}
int finish_it(pid_t pid1, pid_t pid2)
{
t_process exit;
int status1;
int status2;
exit.exit_code = 0;
exit.w1 = waitpid(pid1, &status1, 0);
if (exit.w1 == pid1)
{
if (WIFEXITED(status1))
exit.exit_code = WEXITSTATUS(status1);
}
exit.w2 = waitpid(pid2, &status2, 0);
if (exit.w2 == pid2)
{
if (WIFEXITED(status2))
exit.exit_code = WEXITSTATUS(status2);
}
return (exit.exit_code);
}
int fork_it(int fd[2], char *argv[], char *envp[])
{
t_process process;
process.pid1 = fork();
if (process.pid1 < 0)
error_custom(errno, NULL);
else if (process.pid1 == 0)
command_one(fd, argv, envp);
else
{
process.pid2 = fork();
if (process.pid2 == -1)
error_custom(errno, NULL);
else if (process.pid2 == 0)
command_two(fd, argv, envp);
else
{
if (close(fd[0]) == -1)
error_custom(errno, NULL);
if (close(fd[1]) == -1)
error_custom(errno, NULL);
return (finish_it(process.pid1, process.pid2));
}
}
return (0);
}
int main(int argc, char *argv[], char *envp[])
{
int fd[2];
int exit_code;
exit_code = 0;
if (argc != 5)
error_custom(E_CUSTOM_ARGC, NULL);
if (argc == 5)
{
if (pipe(fd) == -1)
error_custom(errno, NULL);
exit_code = fork_it(fd, argv, envp);
}
return (exit_code);
}