-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.c
More file actions
70 lines (64 loc) · 1.94 KB
/
path.c
File metadata and controls
70 lines (64 loc) · 1.94 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elavrich <elavrich@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/17 08:25:00 by elavrich #+# #+# */
/* Updated: 2025/06/21 01:04:14 by elavrich ### ########.fr */
/* */
/* ************************************************************************** */
#include "Minishell.h"
char *get_cmd_path(char *cmd, t_shell *shell)
{
char **paths;
char *full_path;
int i;
char **envp;
envp = shell->env_var;
full_path = NULL;
while (*envp && ft_strncmp(*envp, "PATH=", 5))
envp++;
if (!*envp)
return (NULL);
paths = ft_split(*envp + 5, ':');
if (!paths)
return (NULL);
i = -1;
while (paths[++i])
{
full_path = join_path(paths[i], cmd);
if (full_path && access(full_path, X_OK) == 0)
return (free_array(paths), full_path);
free(full_path);
}
return (free_array(paths), NULL);
}
char *join_path(char *dir, char *cmd)
{
char *tmp;
char *full_path;
tmp = ft_strjoin(dir, "/");
if (!tmp)
return (NULL);
full_path = ft_strjoin(tmp, cmd);
free(tmp);
if (!full_path)
return (NULL);
return (full_path);
}
char *get_right_path(char *cmd, void *either_shell, int is_pipe)
{
if (ft_strchr(cmd, '/'))
{
if (access(cmd, X_OK) == 0)
return (ft_strdup(cmd));
else
return (NULL);
}
if (is_pipe)
return (get_path_in(cmd, (t_shell *)either_shell));
else
return (get_cmd_path(cmd, (t_shell *)either_shell));
}