-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_path.c
92 lines (83 loc) · 2.01 KB
/
check_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
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aelsayed <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/14 17:27:22 by aelsayed #+# #+# */
/* Updated: 2025/01/30 20:05:51 by aelsayed ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
char *free_paths(char ***paths, char **cmd)
{
int i;
i = 0;
if (cmd && *cmd)
{
free(*cmd);
*cmd = NULL;
}
if (*paths)
{
while ((*paths)[i])
{
free((*paths)[i]);
(*paths)[i] = NULL;
i++;
}
free(*paths);
*paths = NULL;
}
return (NULL);
}
int globale_variable(int nb)
{
static int amine;
amine = 0;
if (nb == -1)
return (amine);
else
{
amine = nb;
return (amine);
}
}
int path_index(char **envp)
{
int i;
i = 0;
while (envp[i])
{
if (!strncmp("PATH=", envp[i], 5))
return (i);
i++;
}
globale_variable(1);
return (0);
}
char *check_path(char **envp, char *cmd)
{
char **paths;
char *checker;
char **cmds;
int i;
i = 0;
paths = ft_split(envp[path_index(envp)], ':');
cmds = ft_split(cmd, ' ');
if (access(cmds[0], X_OK) == 0)
return (cmds[0]);
while (paths && paths[i])
{
checker = ft_strjoin2(paths[i], cmds[0]);
if (access(checker, X_OK) == 0)
return (free_paths(&paths, NULL), free_paths(&cmds, NULL), checker);
free(checker);
i++;
}
free_paths(&paths, NULL);
free_paths(&cmds, NULL);
globale_variable(127);
return (exit_error(-1, 2, 'c', cmd), NULL);
}