-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_binary.c
112 lines (101 loc) · 2.72 KB
/
find_binary.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_binary.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ggiannit <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 10:24:53 by ggiannit #+# #+# */
/* Updated: 2023/04/17 21:53:44 by ggiannit ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *ft_getenv(char *to_get, char **env)
{
int i;
int len;
i = 0;
len = ft_strlen(to_get);
while (env[i])
{
if (!ft_strncmp(to_get, env[i], len) && env[i][len] == '=')
break ;
i++;
}
if (!env[i])
return (NULL);
return (&env[i][len + 1]);
}
char *ft_getpath(char *full_path, t_mish *meta, t_cmd *node)
{
int i;
char **path_mat;
char *exec_path;
(void) meta;
i = 0;
if (!access(node->pot[0], X_OK))
return (NULL);
path_mat = ft_split(full_path, ':');
while (path_mat[i])
{
full_path = ft_strjoin("/", node->pot[0]);
exec_path = ft_strjoin(path_mat[i], full_path);
ft_free_null(&full_path);
if (!access(exec_path, X_OK))
break ;
ft_free_null(&exec_path);
i++;
}
if (!path_mat[i])
exec_path = ft_strjoin("/bin/", node->pot[0]);
ft_free_matrix(&path_mat);
return (exec_path);
}
void ft_getcmd(t_mish *meta, t_cmd *node)
{
char *full_path;
full_path = ft_getenv("PATH", meta->env);
if (!full_path)
return ;
full_path = ft_getpath(full_path, meta, node);
if (!ft_strncmp(node->pot[0], "ls", 3))
node->pot = ft_matrixadd(node->pot, ft_strdup("--color=tty"));
if (full_path)
{
ft_free((void **) &(node->pot[0]));
node->pot[0] = full_path;
}
}
int ft_endpath(char *str)
{
int i;
i = ft_strlen(str);
while (str && --i)
{
if (str[i] == '/')
return (i + 1);
}
return (0);
}
int ft_execbin(t_mish *meta, t_cmd *node)
{
int pid;
ft_getcmd(meta, node);
pid = fork();
if (!pid)
{
execve(node->pot[0], node->pot, meta->env);
if (WEXITSTATUS(EXIT_FAILURE) != 1)
{
ft_putstr_fd(&node->pot[0][ft_endpath(node->pot[0])], 2);
ft_putstr_fd(": command not found\n", 2);
ft_free_shell(meta);
exit(127);
}
perror(&node->pot[0][ft_endpath(node->pot[0])]);
ft_free_shell(meta);
exit(EXIT_FAILURE);
}
waitpid(pid, &meta->exit_code, 0);
return (WEXITSTATUS(meta->exit_code));
}