Repository navigation
Expand file tree
/
Copy pathexecutor_path.c
More file actions
80 lines (71 loc) · 2.27 KB
/
Copy pathexecutor_path.c
File metadata and controls
80 lines (71 loc) · 2.27 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
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* executor_path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kraksana <kraksana@student.42bangkok.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/09/20 00:00:00 by kraksana #+# #+# */
/* Updated: 2026/09/20 18:50:33 by kraksana ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *join_command_path(char *directory, char *command)
{
char *temporary;
char *full_path;
temporary = ft_strjoin(directory, "/");
if (!temporary)
return (NULL);
full_path = ft_strjoin(temporary, command);
free(temporary);
return (full_path);
}
static int path_is_file(char *path)
{
struct stat info;
if (stat(path, &info) < 0)
return (0);
return (!S_ISDIR(info.st_mode));
}
static char *search_path_directories(char **directories, char *command)
{
char *full_path;
char *blocked_path;
size_t i;
i = 0;
blocked_path = NULL;
while (directories[i])
{
full_path = join_command_path(directories[i], command);
if (!full_path)
return (free(blocked_path), NULL);
if (path_is_file(full_path) && access(full_path, X_OK) == 0)
return (free(blocked_path), full_path);
if (!blocked_path && path_is_file(full_path))
blocked_path = full_path;
else
free(full_path);
i++;
}
return (blocked_path);
}
char *resolve_command_path(t_shell *shell, char *command)
{
char *path;
char **directories;
char *result;
if (!command || !*command)
return (NULL);
if (ft_strchr(command, '/'))
return (ft_strdup(command));
path = env_get(shell->env, "PATH");
if (!path)
return (ft_strdup(command));
directories = split_path(path);
if (!directories)
return (NULL);
result = search_path_directories(directories, command);
env_free(directories);
return (result);
}