-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.c
132 lines (121 loc) · 2.95 KB
/
core.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* core.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mtacnet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/08/18 11:04:46 by mtacnet #+# #+# */
/* Updated: 2017/08/31 11:37:01 by mtacnet ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void list_path(t_elem **lst_path, char **path)
{
int i;
i = 0;
while (path[i] && path[i][0] != '\0')
{
push_elem(lst_path, path[i]);
i++;
}
free_tab(path);
}
void get_elem(t_elem **lst_path, char **env, char *elem)
{
char **tmp_tab;
char *tmp;
size_t i;
size_t j;
size_t name_len;
tmp = NULL;
i = 0;
j = 0;
name_len = 0;
while (env[i] && env[i][0] != '\0')
{
if (ft_strncmp(env[i], elem, 4) == 0)
j = i;
i++;
}
if (j == 0)
return ;
tmp_tab = ft_strsplit(env[j], ':');
name_len = ft_strlen(tmp_tab[0]);
tmp = ft_strsub(tmp_tab[0], 5, name_len);
ft_strdel(&tmp_tab[0]);
tmp_tab[0] = ft_strsub(tmp, 0, name_len);
list_path(lst_path, tmp_tab);
ft_strdel(&tmp);
}
void process_core(char *line, char **tab_arg, t_elem **lst_env,
char **env)
{
char **tab;
int i;
t_elem *lst_path;
tab = NULL;
i = 0;
lst_path = new_list();
get_elem(&lst_path, env, "PATH");
modif_line(&line);
tab_arg = ft_strsplit(line, 040);
if (tab_arg[0] != NULL)
{
if (access(tab_arg[0], F_OK | X_OK) == 0)
{
tab = list_to_tab(lst_env);
if_path(tab_arg[0], tab_arg, tab);
if (tab != NULL)
free(tab);
}
else if ((i = parsing_cmd(tab_arg[0])) != 0)
check_cmd(i, lst_env, tab_arg, &lst_path);
else
go_bin(&lst_path, lst_env, tab_arg);
}
freelst(&lst_path);
free_tab(tab_arg);
}
void if_path(char *valid_path, char **tab_arg, char **env)
{
pid_t child_pid;
int status;
child_pid = fork();
if (child_pid == -1)
{
ft_putstr_fd("ERROR WITH CHILD PROCESSUS", 2);
exit(EXIT_FAILURE);
}
else if (child_pid == 0)
{
execve(valid_path, tab_arg, env);
exit(0);
}
else
waitpid(child_pid, &status, WUNTRACED);
}
void get_line(char **env)
{
char *line;
char **tab_arg;
t_elem *lst_env;
int ret;
line = NULL;
tab_arg = NULL;
lst_env = new_list();
tab_to_list(&lst_env, env);
ft_putstr("$> ");
while (1)
{
while ((ret = get_next_line(0, &line)) > 0)
{
if (verif_line(line) == 1)
ft_putstr("$> ");
else
process_core(line, tab_arg, &lst_env, env);
ft_putstr("$> ");
free(line);
}
}
}