-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.c
79 lines (71 loc) · 1.69 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jheo <jheo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/13 20:01:02 by jheo #+# #+# */
/* Updated: 2024/07/24 19:40:48 by jheo ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void error_control(char *s)
{
errno ;
perror(s);
exit(EXIT_FAILURE);
}
int find_path(char *envp)
{
char *path;
int i;
path = "PATH";
i = 0;
while (path[i])
{
if (envp[i] != path[i])
return (-1);
i++;
}
return (1);
}
char *command_path_check(t_data *t, char **cmd)
{
int i;
char *command;
char *temp;
i = 0;
if (cmd[0][0] != '/')
{
while (t->path[i])
{
temp = ft_strjoin(t->path[i], "/");
command = ft_strjoin(temp, cmd[0]);
free(temp);
if (access(command, F_OK | X_OK) == 0)
{
return (command);
}
free(command);
i++;
}
}
return (NULL);
}
void path_split(t_data *t, char *envp[])
{
int i;
char *path;
i = 0;
while (envp[i])
{
if (find_path(envp[i]) == 1)
{
path = (envp[i] + 5);
break ;
}
i++;
}
t->path = ft_split(path, ':');
}