2023-03-28 09:55:08 -04:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* ft_get_executable.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/03/27 13:39:48 by cchauvet #+# #+# */
|
2023-04-05 11:32:04 -04:00
|
|
|
/* Updated: 2023/04/05 15:09:29 by alouis-j ### ########.fr */
|
2023-03-28 09:55:08 -04:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
#include "./utils.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-03-20 10:31:39 -04:00
|
|
|
char *ft_get_executable_with_path(t_data *data, const char *name)
|
2023-03-10 06:32:39 -05:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
|
2023-03-10 07:34:23 -05:00
|
|
|
if (access(name, F_OK) != 0)
|
2023-03-10 06:32:39 -05:00
|
|
|
{
|
2023-04-05 11:32:04 -04:00
|
|
|
*data->exit_code = 127;
|
2023-03-30 07:05:38 -04:00
|
|
|
ft_eprintf("minishell: %s: No such file or directery\n", name);
|
2023-03-10 06:32:39 -05:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (access(name, X_OK) != 0)
|
|
|
|
{
|
2023-04-05 11:32:04 -04:00
|
|
|
*data->exit_code = 126;
|
2023-03-10 06:32:39 -05:00
|
|
|
ft_eprintf("minishell: %s: permission denied\n", name);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
path = ft_strdup(name);
|
|
|
|
if (path == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (path);
|
|
|
|
}
|
|
|
|
|
2023-03-28 09:55:08 -04:00
|
|
|
static char **ft_get_paths(t_data *data, const char *name)
|
2023-03-10 06:32:39 -05:00
|
|
|
{
|
|
|
|
char *paths;
|
2023-03-28 09:55:08 -04:00
|
|
|
char **tab;
|
2023-03-10 06:32:39 -05:00
|
|
|
|
2023-03-20 10:31:39 -04:00
|
|
|
paths = get_value_by_key("PATH", data->env);
|
2023-03-10 06:32:39 -05:00
|
|
|
if (paths == NULL)
|
2023-03-20 07:41:40 -04:00
|
|
|
{
|
2023-04-05 11:32:04 -04:00
|
|
|
*data->exit_code = 127;
|
2023-03-20 07:41:40 -04:00
|
|
|
ft_eprintf("minishell: %s: command not found\n", name);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2023-03-10 06:32:39 -05:00
|
|
|
tab = ft_split(paths, ':');
|
|
|
|
if (tab == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|
2023-03-28 09:55:08 -04:00
|
|
|
return (tab);
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:07:57 -04:00
|
|
|
static char *ft_file_is_executable(const char *path, const char *name)
|
|
|
|
{
|
|
|
|
char *out;
|
|
|
|
|
|
|
|
out = ft_strmerger(3, path, "/", name);
|
|
|
|
if (out == NULL)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: malloc failed\n");
|
|
|
|
free(out);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (access(out, X_OK) == 0)
|
|
|
|
return (out);
|
|
|
|
free(out);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2023-03-28 09:55:08 -04:00
|
|
|
static char *ft_get_executable_without_path(t_data *data, const char *name)
|
|
|
|
{
|
|
|
|
char **paths;
|
|
|
|
char *path;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
paths = ft_get_paths(data, name);
|
|
|
|
if (paths == NULL)
|
|
|
|
return (NULL);
|
|
|
|
path = NULL;
|
2023-03-10 06:32:39 -05:00
|
|
|
i = 0;
|
2023-03-28 09:55:08 -04:00
|
|
|
while (paths[i] != NULL)
|
2023-03-10 06:32:39 -05:00
|
|
|
{
|
2023-03-29 13:07:57 -04:00
|
|
|
path = ft_file_is_executable(paths[i], name);
|
|
|
|
if (path != NULL)
|
2023-03-22 09:59:19 -04:00
|
|
|
break ;
|
2023-03-10 06:32:39 -05:00
|
|
|
i++;
|
|
|
|
}
|
2023-03-28 09:55:08 -04:00
|
|
|
ft_freer_tab_ultimate(1, paths);
|
2023-03-10 06:32:39 -05:00
|
|
|
if (path == NULL)
|
2023-03-20 10:31:39 -04:00
|
|
|
{
|
2023-04-05 11:32:04 -04:00
|
|
|
*data->exit_code = 127;
|
2023-03-10 06:32:39 -05:00
|
|
|
ft_eprintf("minishell: %s: command not found\n", name);
|
2023-03-20 10:31:39 -04:00
|
|
|
}
|
2023-03-10 06:32:39 -05:00
|
|
|
return (path);
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:31:39 -04:00
|
|
|
char *ft_get_executable(t_data *data, const char *name)
|
2023-03-10 06:32:39 -05:00
|
|
|
{
|
|
|
|
char *path;
|
2023-03-22 09:59:19 -04:00
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
if (name[0] == '.' || name[0] == '/')
|
2023-03-20 10:31:39 -04:00
|
|
|
path = ft_get_executable_with_path(data, name);
|
2023-03-10 06:32:39 -05:00
|
|
|
else
|
2023-03-20 10:31:39 -04:00
|
|
|
path = ft_get_executable_without_path(data, name);
|
2023-03-10 06:32:39 -05:00
|
|
|
return (path);
|
|
|
|
}
|