-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmmd_path.c
executable file
·84 lines (76 loc) · 1.88 KB
/
cmmd_path.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmmd_path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: potero <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/11 09:27:12 by potero #+# #+# */
/* Updated: 2022/08/04 12:27:47 by potero-d ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmmd_addarg(char **new_path, char *path, char *arg)
{
char *aux;
int ac;
ac = access(arg, X_OK);
if (ac == 0)
{
*new_path = ft_strdup(arg);
return (0);
}
aux = ft_strjoin(path, "/");
*new_path = ft_strjoin(aux, arg);
ac = access(*new_path, X_OK);
free(aux);
if (ac == 0)
return (0);
else
return (1);
}
void not_cmmd(char *str)
{
ft_putstr_fd("command not found: ", 2);
ft_putstr_fd(str, 2);
write(2, "\n", 1);
}
char *cmmd_find_path(char **envp)
{
int i;
i = 0;
while (envp[i])
{
if (ft_strncmp(envp[i], "PATH", 3) == 0)
return (&envp[i][5]);
i++;
}
return (0);
}
char *cmmd_path(char **path, char *arg)
{
int i;
char *new_path;
if (path == 0)
{
i = access(arg, X_OK);
if (i == 0)
{
new_path = ft_strdup(arg);
return (new_path);
}
else
return (0);
}
new_path = NULL;
i = 0;
while (path[i])
{
if (cmmd_addarg(&new_path, path[i], arg) == 0)
return (new_path);
free(new_path);
i++;
}
new_path = 0;
return (new_path);
}