-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_utils2.c
92 lines (81 loc) · 1.97 KB
/
exec_utils2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: btaha <btaha@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/02 17:13:56 by btaha #+# #+# */
/* Updated: 2023/01/02 17:13:58 by btaha ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/minishell.h"
char *get_path(char *cmd)
{
char *path;
path = ft_strjoin("/bin/", cmd);
if (!path)
return (free(path), NULL);
return (path);
}
size_t get_len(t_list *args)
{
t_list *tmp;
int i;
i = 0;
tmp = args;
while (tmp)
{
i++;
tmp = tmp->next;
}
return (i + 1);
}
char **get_args(t_list *cmd)
{
char **args;
int i;
args = ft_calloc(get_len(cmd), sizeof(char **));
if (!args)
return (free(args), NULL);
i = 0;
while (cmd)
{
args[i++] = cmd->content;
cmd = cmd->next;
}
return (args);
}
int exe_exec(char *path, t_list *cmd)
{
char **args;
char **env;
env = get_env();
if (!env)
return (-1);
args = get_args(cmd);
execve(path, args, env);
free_str(env);
return (-1);
}
int exec_path(char *path_env, char *path, t_list *cmd)
{
char **tab;
int i;
int acces;
tab = ft_split(path_env, ':');
i = 0;
acces = 1;
while (tab[i])
{
exe_exec(ft_strjoin(tab[i++], path), cmd);
if (errno == EACCES)
acces = 0;
else if (errno == ENOENT || errno == ENOTDIR)
continue ;
break ;
}
if (!acces)
errno = EACCES;
return (free(tab), -1);
}