-
Notifications
You must be signed in to change notification settings - Fork 0
/
fork.c
88 lines (81 loc) · 2.47 KB
/
fork.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fork.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mleclair <mleclair@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/10 17:55:44 by mleclair #+# #+# */
/* Updated: 2017/03/27 11:20:39 by mleclair ### ########.fr */
/* */
/* ************************************************************************** */
#include "chell.h"
void ft_child2(t_env *env, char **input, char *pwd, char *tmp)
{
struct stat buf[INPUT_SIZE];
size_t t;
t = ft_strlen(*input);
while (t != 0 && (*input)[t] != '/')
--t;
if (tmp && tmp[0] == '/' && (*input += t + 1))
execve(tmp, input, env->ev);
*input = tmp;
if (ft_cmpspec(*input, "./") == 1)
{
tmp = ft_strjoin(pwd + 4, *input + 1);
execve(tmp, input, env->ev);
if (lstat(tmp, buf) == -1)
error(-1, NULL, NULL);
else if (!(buf->st_mode & S_IXUSR) || !(buf->st_mode & S_IROTH))
error(-5, *input, NULL);
else
error(-4, *input, NULL);
free(tmp);
exit(env->lastret);
}
error(-2, *input, NULL);
env_free(env);
exit(env->lastret);
}
void ft_child(t_env *env, char **input, char *pwd)
{
int i;
char *tmp;
char *tmp2;
if ((i = find_param(env->ev, "PATH")) != -1)
env->path = ft_strsplit(env->ev[i] + 5, ':');
else if (env->savev && (i = find_param(env->savev, "PATH")) != -1)
env->path = ft_strsplit(env->savev[i] + 5, ':');
i = 0;
while (env->path && env->path[i] && ft_strchr(*input, '/') == 0)
{
tmp = ft_strjoin(env->path[i], "/");
tmp2 = ft_strjoin(tmp, *input);
execve(tmp2, input, env->ev);
free(tmp);
free(tmp2);
++i;
}
tmp = *input;
ft_child2(env, input, pwd, tmp);
}
void ft_fork(t_env *env, char **input)
{
int status;
size_t t;
char pwd[INPUT_SIZE + 4];
getpwd(pwd);
t = 0;
env->booljob = 1;
env->i = fork();
if (env->i == 0)
ft_child(env, input, pwd);
else
{
if (waitpid(env->i, &status, WUNTRACED) == -1)
error(-3, NULL, NULL);
env->booljob = 0;
retvalue_into_loc(env, WEXITSTATUS(status));
env->i = 1;
}
}