-
Notifications
You must be signed in to change notification settings - Fork 0
/
help_with_cmd_bonus.c
75 lines (66 loc) · 1.15 KB
/
help_with_cmd_bonus.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
#include "header/pipex_bonus.h"
char **ft_find_path(char **envp)
{
char **arr;
int i;
i = 0;
while (envp[i++] != NULL)
if (ft_strncmp(envp[i], "PATH", 4) == 0)
break ;
arr = ft_split(envp[i] + 5, ':');
if (arr == NULL)
ft_call_exit();
return (arr);
}
char **ft_delenie_cmd(char *argv)
{
char **cmd;
cmd = ft_split(argv, ' ');
if (cmd == NULL)
return (NULL);
return (cmd);
}
char **ft_check_file(char **arr, char **cmd)
{
int i;
char *tmp;
i = 0;
while (arr[i++] != NULL)
{
tmp = ft_strjoin(ft_strjoin(arr[i], "/"), cmd[0]);
if (tmp == NULL)
ft_call_com_not_found();
if (access(tmp, 0))
free(tmp);
else
{
free(cmd[0]);
cmd[0] = tmp;
return (cmd);
}
}
return (NULL);
}
char **ft_first_check_file(char **cmd)
{
if (access(cmd[0], 0) == 0)
return (cmd);
else
return (NULL);
}
char **ft_change_cmd(char **cmd, char **envp)
{
char **path;
char **cmd1;
char **cmd2;
if (cmd[0] == NULL)
ft_call_com_not_found();
path = ft_find_path(envp);
cmd1 = ft_first_check_file(cmd);
if (cmd1 != NULL)
return (cmd1);
cmd2 = ft_check_file(path, cmd);
if (cmd2 == NULL)
ft_call_com_not_found();
return (cmd2);
}