extract into helper for norminette
This commit is contained in:
parent
abdab3156c
commit
e366689f91
@ -12,6 +12,34 @@
|
|||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
static char *resolve_relative(t_minishell *msh, char *cmd)
|
||||||
|
{
|
||||||
|
char *executable;
|
||||||
|
|
||||||
|
if (access(cmd, F_OK) == 0)
|
||||||
|
{
|
||||||
|
executable = ft_strdup_safe(msh, cmd);
|
||||||
|
if (!executable)
|
||||||
|
return (NULL);
|
||||||
|
return (executable);
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *resolve_path(t_minishell *msh, char *path, char *cmd)
|
||||||
|
{
|
||||||
|
char *executable;
|
||||||
|
|
||||||
|
executable = malloc_safe(msh, ft_strlen(path) + ft_strlen(cmd) + 2);
|
||||||
|
ft_strlcpy(executable, path, ft_strlen(path) + 1);
|
||||||
|
ft_strlcat(executable, "/", ft_strlen(path) + 2);
|
||||||
|
ft_strlcat(executable, cmd, ft_strlen(path) + ft_strlen(cmd) + 2);
|
||||||
|
if (access(executable, F_OK) == 0)
|
||||||
|
return (executable);
|
||||||
|
free_safe(msh, (void **)&executable);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
char *executor_absolute_path(t_minishell *msh, char *cmd)
|
char *executor_absolute_path(t_minishell *msh, char *cmd)
|
||||||
{
|
{
|
||||||
char **path;
|
char **path;
|
||||||
@ -20,16 +48,7 @@ char *executor_absolute_path(t_minishell *msh, char *cmd)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (cmd[0] == '/' || cmd[0] == '.')
|
if (cmd[0] == '/' || cmd[0] == '.')
|
||||||
{
|
return (resolve_relative(msh, cmd));
|
||||||
if (access(cmd, F_OK) == 0)
|
|
||||||
{
|
|
||||||
executable = ft_strdup_safe(msh, cmd);
|
|
||||||
if (!executable)
|
|
||||||
return (NULL);
|
|
||||||
return (executable);
|
|
||||||
}
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
path_env = environment_get(msh, "PATH");
|
path_env = environment_get(msh, "PATH");
|
||||||
if (!path_env)
|
if (!path_env)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
@ -37,16 +56,9 @@ char *executor_absolute_path(t_minishell *msh, char *cmd)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while (path[i] != NULL)
|
while (path[i] != NULL)
|
||||||
{
|
{
|
||||||
executable = malloc_safe(msh, ft_strlen(path[i]) + ft_strlen(cmd) + 2);
|
executable = resolve_path(msh, path[i], cmd);
|
||||||
ft_strlcpy(executable, path[i], ft_strlen(path[i]) + 1);
|
if (executable)
|
||||||
ft_strlcat(executable, "/", ft_strlen(path[i]) + 2);
|
|
||||||
ft_strlcat(executable, cmd, ft_strlen(path[i]) + ft_strlen(cmd) + 2);
|
|
||||||
if (access(executable, F_OK) == 0)
|
|
||||||
{
|
|
||||||
ft_free_arr(path);
|
|
||||||
return (executable);
|
return (executable);
|
||||||
}
|
|
||||||
free_safe(msh, (void **)&executable);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (ft_free_arr(path), NULL);
|
return (ft_free_arr(path), NULL);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user