-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexec.c
96 lines (86 loc) · 2.32 KB
/
exec.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmachado <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 15:51:23 by bmachado #+# #+# */
/* Updated: 2022/02/23 13:16:59 by bmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int can_execute(char *cmd)
{
struct stat sb;
if (stat(cmd, &sb) != 0)
return (0);
return (1);
}
static int exec_error(int ret, char **path, char *cmd, char *path_item)
{
if (ret != 0 || !path || !path_item)
{
printf("minishell: command not found: %s\n", cmd);
return (1);
}
return (0);
}
static char **find_path_env(t_shell *table)
{
int i;
t_vars *item;
char *tmp;
i = get_position_env(table, "PATH");
item = table->env_vars[i];
if (item)
{
tmp = item->value;
g_shell.path = ft_split(tmp, ':');
}
return (g_shell.path);
}
static int cmd_location(char **path, char **cmd)
{
int i;
char *tmp;
i = 0;
if (!path)
return (0);
while (path[i])
{
tmp = ft_strjoin(path[i], "/");
path[i] = ft_strjoin(tmp, cmd[0]);
free(tmp);
if (can_execute(path[i]))
break ;
i++;
}
return (i);
}
int execute_shell(t_shell *tab, char **cmds)
{
int i;
static int ret;
find_path_env(tab);
minishell_env(tab);
i = cmd_location(g_shell.path, cmds);
tab->proc.pid = fork();
tab->proc.child++;
exec_signal();
if (tab->proc.pid == 0)
{
if (!cmds[0])
exit(0);
else if (can_execute(cmds[0]))
ret = execve(cmds[0], cmds, g_shell.env);
if (g_shell.path && g_shell.path[i])
ret = execve(g_shell.path[i], cmds, g_shell.env);
if (exec_error(ret, g_shell.path, cmds[0], g_shell.path[i]))
exit_process(tab);
_exit(ret);
}
free_cmd_lst(g_shell.path);
free_cmd_lst(g_shell.env);
return (1);
}