-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
109 lines (100 loc) · 2.23 KB
/
main.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
#include "pipex.h"
static int access_checker(char *path)
{
if (!access(path, F_OK))
{
if (!access(path, X_OK))
return (0);
return (-2);
}
return (-1);
}
static char *pathdefiner(char *prog)
{
char **path;
char *bpath;
int acs;
int i;
i = -1;
while (environ[++i])
if (ft_strnstr(environ[i], "PATH=", ft_strlen("PATH=")))
break ;
if (i == arrsize(environ))
return (NULL);
path = ft_split(environ[i] + ft_strlen("PATH="), ':');
i = arrsize(path);
while (--i - 1)
{
bpath = ft_strjoin(path[i - 1], prog);
acs = access_checker(bpath);
if (!acs && eraser((void **)path))
return (bpath);
free(bpath);
if (acs == -2 && eraser((void **)path))
return (NULL);
}
eraser((void **)path);
return (NULL);
}
static int executor(int argn, char **argv)
{
char **flags;
char *buf;
char *path;
flags = ft_split(argv[argn], ' ');
if (flags && ft_strchr(flags[0], '/'))
{
path = flags[0];
flags[0] = ft_strrchr(flags[0], '/') + 1;
return (error_handler(argn, argv, execve(path, flags, environ)));
}
else if (!flags)
return (error_handler(argn, argv, -1));
buf = ft_strjoin("/", flags[0]);
path = pathdefiner(buf);
if (!path)
return (error_handler(argn, argv, -1));
else
return (error_handler(argn, argv, execve(path, flags, environ)));
exit(1);
}
static int forker(int argn, char **argv, int rightpfd[2], int infd)
{
int leftpfd[2];
int stat_loc;
pid_t pid;
leftpfd[0] = 0;
leftpfd[1] = 0;
if (argn == 1)
return (0);
if (argn != 2)
error_handler(0, argv, pipe(leftpfd));
forker(argn - 1, argv, leftpfd, infd);
if (argn != 2)
error_handler(argn, argv, close(leftpfd[1]));
pid = error_handler(0, argv, fork());
if (pid > 0)
{
error_handler(argn, argv, waitpid(-1, &stat_loc, WUNTRACED));
if (argn != 2)
error_handler(argn, argv, close(leftpfd[0]));
return (WEXITSTATUS(stat_loc));
}
mounter(leftpfd, rightpfd, infd, argv);
return (executor(argn, argv));
}
int main(int argc, char **argv)
{
int fd;
int nullfd[2];
if (argc != 5)
{
ft_putstr_fd(ft_strrchr(argv[0], '/') + 1, 1);
ft_putstr_fd(": Stricly 4 args: input_file cmd1 cmd2 output_file\n", 1);
return (1);
}
nullfd[0] = 0;
nullfd[1] = 0;
fd = file_check(1, argv, R_OK);
return (forker(argc - 2, argv, nullfd, fd));
}