-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex.c
130 lines (119 loc) · 3.13 KB
/
pipex.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: melkholy <melkholy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/31 18:40:17 by melkholy #+# #+# */
/* Updated: 2022/09/12 23:56:46 by melkholy ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void ft_pipe_process(int num, char **cmds_path, char *envp[], int **pipes)
{
int count;
count = -1;
while (++count < 2)
{
if (num != count)
close(pipes[count][0]);
if (num + 1 != count)
close(pipes[count][1]);
}
dup2(pipes[num][0], 0);
close(pipes[num][0]);
dup2(pipes[num + 1][1], 1);
close(pipes[num + 1][1]);
if (cmds_path)
execve(cmds_path[0], cmds_path, envp);
else
return ;
}
int ft_infile_fd(char *argv[], int **pipes)
{
int infile;
int status;
infile = 0;
status = 0;
if (access(argv[1], F_OK | R_OK))
{
if (access(argv[1], F_OK))
status = 1;
ft_printf("%s: %s\n", strerror(errno), argv[1]);
}
else
{
infile = open(argv[1], O_RDONLY);
dup2(infile, pipes[0][0]);
close(infile);
}
return (status);
}
void ft_outfile_fd(char *argv[], int **pipes)
{
int outfile;
outfile = 0;
if (!access(argv[4], F_OK | W_OK))
outfile = open(argv[4], O_WRONLY | O_TRUNC);
else if (!access(argv[4], F_OK))
{
ft_printf("%s: %s\n", strerror(errno), argv[4]);
ft_free_pipes(pipes);
exit(1);
}
else
outfile = open(argv[4], O_RDWR | O_CREAT | O_TRUNC, 0666);
dup2(outfile, pipes[0][1]);
close(outfile);
}
int ft_fork_proc(char *argv[], char *envp[], int **pipefds, int *pid)
{
char **cmds_path;
int count;
int status;
count = -1;
status = 0;
while (++count < 2)
{
cmds_path = ft_check_path(argv[count + 2], envp);
pid[count] = fork();
if (count && pid[count] == 0)
ft_last_proc(count, cmds_path, envp, pipefds);
else if (pid[count] == 0)
ft_pipe_process(count, cmds_path, envp, pipefds);
else if (pid[count] < 0)
perror("Creating a new process failed");
if (cmds_path)
ft_free_pth(cmds_path);
else if (!cmds_path && count)
status = 1;
}
ft_close_pipes(pipefds, count);
return (status);
}
int main(int argc, char *argv[], char *envp[])
{
int *pid;
int **pipes;
int count;
int procs;
int status;
count = -1;
status = 0;
if (argc != 5)
exit(1);
procs = argc - 3;
pipes = ft_create_pipes(procs);
status = ft_infile_fd(argv, pipes);
ft_outfile_fd(argv, pipes);
pid = ft_create_pid(2);
if (ft_fork_proc(argv, envp, pipes, pid))
status = 127;
while (++count < procs)
waitpid(pid[count], NULL, 0);
ft_free_pipes(pipes);
unlink("./temp.txt");
free(pid);
return (status);
}